Spring学习04----JDBCTemplate和事务控制

本文详细介绍了Spring中的JdbcTemplate,包括对象创建、增删改查操作的实现,以及在DAO层的使用。此外,还深入探讨了Spring的事务控制,包括基本介绍、事务基础知识、事务控制API,以及基于XML和注解的声明式事务配置实例。
摘要由CSDN通过智能技术生成

1.Spring中的JdbcTemplate

  • JdbcTemplate是 spring 框架中提供的一个对象,是对原始 Jdbc API 对象的简单封装

1.1 JdbcTemplate对象的创建

  • 首先在项目中的pom.xml中导入以下坐标:
		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
  • 配置数据源:
  • DBUtils中的QueryRunner对象类似,JdbcTemplate对象在执行sql语句时也需要一个数据源,这个数据源可以使用C3P0或者DBCP,也可以使用Spring的内置数据源DriverManagerDataSource,这里我们使用DriverManagerDataSource数据源。
<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/student"></property>
            <property name="username" value="root"></property>
            <property name="password" value="ljt074517"></property>
</bean>
  • 创建JdbcTemplate对象
    • JdbcTemplate中注入数据源创建对象,在bean.xml中配置如下:
<bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="datasource"></property>
</bean>

1.2使用JdbcTemplate实现增删改查的操作

  • 首先创建相应的数据库和表
//创建数据库:
create database spring_day02;
use spring_day02;
//创建表:
create table account(
id int primary key auto_increment,
name varchar(40),
money float
)character set utf8 collate utf8_general_ci;
  • DBUtils十分类似,JdbcTemplate的增删改操作使用其update("SQL语句", 参数...)方法
  • 增加操作:
public static void main(String[] args) {
   
    //1.获取Spring容器
    ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    //2.根据id获取JdbcTemplate对象
    JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate");
    //3.执行增加操作
    jt.update("insert into account(name,money)values('eee',3400)");
}

  • 删除操作
public static void main(String[] args) {
   
    //1.获取Spring容器
    ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    //2.根据id获取JdbcTemplate对象
    JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate");
    //3.执行删除操作
    jt.update("delete from account where id = ?",2);
}

  • 更新操作
public static void main(String[] args) {
   
	//1.获取Spring容器
    ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    //2.根据id获取JdbcTemplate对象
    JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate");
    //3.执行更新操作
	jt.update("update account set name =?,money = ? where id = ?","wanglaowu",5000,1);
}

  • 查询操作:
  • DBUtils十分类似,JdbcTemplate的查询操作使用其query()方法,其参数如下:
    • String sql: SQL语句
    • RowMapper<T> rowMapper: 指定如何将查询结果ResultSet对象转换为T对象.
    • @Nullable Object... args: SQL语句的参数
  • 其中RowMapper类类似于DBUtilsResultSetHandler类,可以自己写一个实现类,但常用Spring框架内置的实现类BeanPropertyRowMapper<T>(T.class)
  • 1.查询所有:
public List<Account> findAllAccounts() {
   
    List<Account> accounts = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
    return accounts;
}
  • 2.查询一条记录
public Account findAccountById(Integer accountId) {
   
    List<Account> accounts = jdbcTemplate.query("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class), accountId);
    return accounts.isEmpty() ? null : accounts.get(0);
}
  • 3.聚合查询:
    • JdbcTemplate中执行聚合查询的方法为queryForObject()方法,其参数如下:
      • String sql:SQL语句
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值