码迷,mamicode.com
首页 > 数据库 > 详细

spring的jdbcTemplate

时间:2019-11-10 19:07:31      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:pen   int   持久层   lap   dep   out   artifact   文件   http   

pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.mepu</groupId>
    <artifactId>day05_spring_jdbcTemplate</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

    </dependencies>


</project>

 

实体类:

package cn.mepu.domain;

import java.io.Serializable;

/**
 * 实体类
 * @author shkstart
 * @create 2019-11-10 15:47
 */
public class Account implements Serializable {
    private Integer id;
    private String name;
    private float money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getMoney() {
        return money;
    }

    public void setMoney(float money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ", money=" + money +
                ‘}‘;
    }
}

 

dao:

package cn.mepu.dao.imp;

import cn.mepu.dao.IAccountDao;
import cn.mepu.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.util.List;

/**
 * @author shkstart
 * @create 2019-11-10 16:09
 */
public class AccountDaoImp extends JdbcDaoSupport implements IAccountDao {


    @Override
    public Account findAccountById(Integer accountId) {
        List<Account> accounts =  super.getJdbcTemplate().query(" select * from account where id=? ",new BeanPropertyRowMapper<Account>(Account.class),accountId);
        return accounts.isEmpty()?null:accounts.get(0);
    }

    @Override
    public Account findAccountByName(String accountName) {
        List<Account> accounts =  super.getJdbcTemplate().query(" select * from account where name=? ",new BeanPropertyRowMapper<Account>(Account.class),accountName);
        if (accounts.isEmpty()){
            return null;
        }else if(accounts.size()>1){
            throw new RuntimeException("结果集大于一");
        }
        return accounts.get(0);
    }

    @Override
    public void updateAccount(Account account) {
        super.getJdbcTemplate().update(" update account set name=? money=? where id=? ",account.getName(),account.getMoney(),account.getId());
    }


}

 

bean配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 配置账户的持久层-->
    <bean id="accountDao" class="cn.mepu.dao.imp.AccountDaoImp">
        <!--<property name="jdbcTemplate" ref="jdbcTemplate"></property>-->
    <property name="dataSource" ref="dataSource"></property>
</bean>

<!--&lt;!&ndash;配置JdbcTemplate  直接注入dataSource
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
</bean>-->

    <!-- 配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/javaee"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
</beans>

 

测试类:

package cn.mepu.jdbcTemplate;

import cn.mepu.dao.IAccountDao;
import cn.mepu.domain.Account;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author shkstart
 * @create 2019-11-10 15:49
 * JdbcTemplate最基本用法
 */
public class JdbcTemplateDome1 {
    public static void main(String[] args) {
        //1.获取容器
        ApplicationContext as = new ClassPathXmlApplicationContext("bean.xml");

        //2.获取对象
        IAccountDao dao = as.getBean("accountDao", IAccountDao.class);

        //3.执行方法
        Account accountById = dao.findAccountById(1);
        System.out.println(accountById);
    }
}

 

spring的jdbcTemplate

标签:pen   int   持久层   lap   dep   out   artifact   文件   http   

原文地址:https://www.cnblogs.com/aikang525/p/11831087.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!