05.Spring框架的JdbcTemplate

JdbcTemplate简介

  • JdbcTemplate是Spring框架中的一个对象,其对原始的Jdbc API对象做了简单的封装,处理了资源的建立和释放,开发者只需提供sql语句及提取结果即可
  • JdbcTemplate提供了默认构造函数、DataSource参数的有参构造函数、set方法

JdbcTemplate配置

  • 相关依赖
		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>
  • xml文件的配置
<?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="com.itheima.dao.impl.AccountDaoImpl">
        <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.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/springlearn?serverTimezone=UTC"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123"></property>
    </bean>
</beans>
  • JdbcTemplate一般使用语句
    1. execute:没有返回值,可以执行所有SQL语句,一般用于执行DDL语句
    2. update:返回的是一个int值,影响的行数, 用于执行INSERT、UPDATE、DELETE等DML语句。增删改都只是使用到了一个方法: update(sql,Object…args)
    3. queryXxx:用于DQL数据查询语句
  • queryXxx相关的常用方法(改编自JdbcTemplate
方法名作用
queryForObject(sql,数据类型.class)查询单个对象
queryForMap(sql,参数)查询单个对象,返回一个Map对象
queryForObject(sql,new RowMapper(),参数)查询单个对象,返回单个实体类对象
queryForList(sql,参数)查询多个对象,返回一个List对象,List对象存储是Map对象
query(sql,new BeanPropertyRowMapper< T >(T.class),参数 )查询多个对象,返回的是一个List对象,List对象存储是实体类
  • 下面来一段伪代码来演示一下查询
// 查询字段money大于500的用户account,要新建AccountRowMapper()对象来取得字段与实体类的关系
List<Account> accounts = jt.query("select * from account where money > ? ",new AccountRowMapper(), 500); 
// 查询字段money大于500的用户account,不用指定实体类的关系,数据库字段要与实体类的属性名相同
List<Account> accounts = jt.query("select * from account where money > ? ",new BeanPropertyRowMapper<Account>(Account.class), 500); 

// 查询一个的方法(不常用)
Account account = jt.query("select * from account where id = ?",new AccountResultSetExtractor(),3); 

// 查询返回一行一列:使用聚合函数,在不使用 group by 字句时,都是返回一行一列,最常用的 就是分页中获取总记录条数
Integer total = jt.queryForObject("select count(*) from account where money > ? ",Integer.class,500); 

// AccountRowMapper()就像对象关系映射,将数据库的字段和实体类的javabean对应起来
public class AccountRowMapper implements RowMapper<Account>{  
	@Override  
	public Account mapRow(ResultSet rs, int rowNum) throws SQLException {   
		Account account = new Account();   
		account.setId(rs.getInt("id"));   
		account.setName(rs.getString("name")); 
  		account.setMoney(rs.getFloat("money"));   
  		return account;  
  	} 

在dao层(持久层)中定义 JdbcTemplate

  • 若通过xml文件配置且有很多个dao类时,每个类都要注入JdbcTemplate 的bean,那么会有很多重复的代码,如下
private JdbcTemplate jdbcTemplate;    
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {   
	this.jdbcTemplate = jdbcTemplate;  
} 
  • 解决方法
    1. 创建一个公共类存放上面重复的代码,是dao类继承这个父类
    2. dao类直接继承Spring自带的JdbcDaoSupport类,这个类就像公共类一样,里面有set方法,当方法要用时用 getJdbcTemplate()获取JdbcTemplate对象即可,在xml文件中,但这个方法在纯注解的项目上不适用,因为JdbcTemplate只提供了默认构造函数、DataSource参数的有参构造函数、set方法,没有给以注解的方式注入JdbcTemplate(@Autowired)
	<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

      换成只传入数据源即可

	<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"/>
    </bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值