Spring JDBCTemplate

1. JDBCTemplate 概述

它是 spring 框架中提供的一个对象,是对原始 JDBC API 对象的简单封装。

spring 框架为我们提供了很多的操作模板类。

操作关系型数据的: JdbcTemplate HibernateTemplate

操作 nosql 数据库的: RedisTemplate

操作消息队列的: JmsTemplate

2. JDBCTemplate的使用

2.1 在DAO接口中定义JDBCTemplate

持久层接口和实现类:

/**
* 接口
*/
public interface IAccountDao {
    /**
    * 根据 id 查询账户信息
    * @param id
    * @return
    */
    Account findAccountById(Integer id);
    /**
    * 根据名称查询账户信息
    * @return
    */
    Account findAccountByName(String name);
    /**
    * 更新账户信息
    * @param account
    */
    void updateAccount(Account account);
}
/**
* 账户的持久层实现类
* 此版本的 dao,需要给 dao 注入 JdbcTemplate
*/
public class AccountDaoImpl implements IAccountDao {
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    	this.jdbcTemplate = jdbcTemplate;
    }
    @Override
    public Account findAccountById(Integer id) {
        List<Account> list = jdbcTemplate.query("select * from account where id = ?",new AccountRowMapper(),id);
    	return list.isEmpty()?null:list.get(0);
    }
    @Override
    public Account findAccountByName(String name) {
    	List<Account> list = jdbcTemplate.query("select * from account where name= ? ",new AccountRowMapper(),name);
        if(list.isEmpty()){
        	return null;
        }
        if(list.size()>1){
        	throw new RuntimeException("结果集不唯一,不是只有一个账户对象");
        }
        return list.get(0);
    }
    @Override
    public void updateAccount(Account account) {
        jdbcTemplate.update("update account set money = ? where id = ?",account.getMoney(),account.getId());
    }
}

spring配置文件信息:

<?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">
<!-- 配置一个 dao -->
    <bean id="accountDao" class="com.linfuxian.dao.impl.AccountDaoImpl">
        <!-- 注入 jdbcTemplate -->
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    <!-- 配置一个数据库的操作模板:JdbcTemplate -->
    <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:///test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="1234"></property>
    </bean>
</beans>

2.2 让 dao 继承 JdbcDaoSupport

持久层接口和实现类:

/**
* 账户的接口
*/
public interface IAccountDao {
    /**
    * 根据 id 查询账户信息
    * @param id
    * @return
    */
    Account findAccountById(Integer id);
    /**
    * 根据名称查询账户信息
    * @return
    */
    Account findAccountByName(String name);
    /**
    * 更新账户信息
    * @param account
    */
    void updateAccount(Account account);
}
/**
* 账户的持久层实现类
* 此版本 dao,只需要给它的父类注入一个数据源
*/
public class AccountDaoImpl2 extends JdbcDaoSupport implements IAccountDao {
@Override
    public Account findAccountById(Integer id) {
        //getJdbcTemplate()方法是从父类上继承下来的。
        List<Account> list = getJdbcTemplate().query("select * from account where id = ? ",new AccountRowMapper(),id);
    }
    @Override
    public Account findAccountByName(String name) {
        //getJdbcTemplate()方法是从父类上继承下来的。
        List<Account> list = getJdbcTemplate().query("select * from account where name = ? ",new AccountRowMapper(),name);
        if(list.isEmpty()){
        	return null;
    	}
        if(list.size()>1){
        	throw new RuntimeException("结果集不唯一,不是只有一个账户对象");
        }
        return list.get(0);
    }
    @Override
    public void updateAccount(Account account) {
        //getJdbcTemplate()方法是从父类上继承下来的。
        getJdbcTemplate().update("update account set money = ? where id = ?",account.getMoney(),account.getId());
    }
}

spring配置文件:

<?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">
<!-- 配置 dao2 -->
    <bean id="accountDao2" class="com.linfuxian.dao.impl.AccountDaoImpl2">
        <!-- 注入 dataSource -->
        <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:///test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="1234"></property>
    </bean>
</beans>

2.3 两种方式的区别

第一种在 Dao 类中定义 JdbcTemplate 的方式,适用于所有配置方式(xml 和注解都可以)。

第二种让 Dao 继承 JdbcDaoSupport 的方式,只能用于基于 XML 的方式,注解用不了。

posted @ 2021-02-24 13:10  渺渺孤烟起  阅读(92)  评论(0)    收藏  举报