我是南城余!阿里云开发者平台专家博士证书获得者!
欢迎关注我的博客!一同成长!
一名从事运维开发的worker,记录分享学习。
专注于AI,运维开发,windows Linux 系统领域的分享!
本章节对应知识库
https://www.yuque.com/nanchengcyu/java
本内容来自尚硅谷课程,此处在知识库做了个人理解
————————————————
7、事务
7.1、JdbcTemplate
7.1.1、简介
Spring 框架对 JDBC 进行封装,使用 JdbcTemplate 方便实现对数据库操作
7.1.2、准备工作
①搭建子模块
搭建子模块:spring-jdbc-tx
②加入依赖
<dependencies> <!--spring jdbc Spring 持久化层支持jar包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>6.0.2</version> </dependency> <!-- MySQL驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.30</version> </dependency> <!-- 数据源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.15</version> </dependency> </dependencies>
③创建jdbc.properties
jdbc.user=root jdbc.password=root jdbc.url=jdbc:mysql://localhost:3306/spring?characterEncoding=utf8&useSSL=false jdbc.driver=com.mysql.cj.jdbc.Driver
④配置Spring的配置文件
beans.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 导入外部属性文件 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 配置数据源 --> <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="url" value="${jdbc.url}"/> <property name="driverClassName" value="${jdbc.driver}"/> <property name="username" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置 JdbcTemplate --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!-- 装配数据源 --> <property name="dataSource" ref="druidDataSource"/> </bean> </beans>
⑤准备数据库与测试表
CREATE DATABASE `spring`; use `spring`; CREATE TABLE `t_emp` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL COMMENT '姓名', `age` int(11) DEFAULT NULL COMMENT '年龄', `sex` varchar(2) DEFAULT NULL COMMENT '性别', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
7.1.3、实现CURD
①装配 JdbcTemplate
创建测试类,整合JUnit,注入JdbcTemplate
package com.atguigu.spring6; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; @SpringJUnitConfig(locations = "classpath:beans.xml") public class JDBCTemplateTest { @Autowired private JdbcTemplate jdbcTemplate; }
②测试增删改功能
@Test //测试增删改功能 public void testUpdate(){ //添加功能 String sql = "insert into t_emp values(null,?,?,?)"; int result = jdbcTemplate.update(sql, "张三", 23, "男"); //修改功能 //String sql = "update t_emp set name=? where id=?"; //int result = jdbcTemplate.update(sql, "张三atguigu", 1); //删除功能 //String sql = "delete from t_emp where id=?"; //int result = jdbcTemplate.update(sql, 1); }
③查询数据返回对象
public class Emp { private Integer id; private String name; private Integer age; private String sex; //生成get和set方法 //...... @Override public String toString() { return "Emp{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + '}'; } }
//查询:返回对象 @Test public void testSelectObject() { //写法一 // String sql = "select * from t_emp where id=?"; // Emp empResult = jdbcTemplate.queryForObject(sql, // (rs, rowNum) -> { // Emp emp = new Emp(); // emp.setId(rs.getInt("id")); // emp.setName(rs.getString("name")); // emp.setAge(rs.getInt("age")); // emp.setSex(rs.getString("sex")); // return emp; // }, 1); // System.out.println(empResult); //写法二 String sql = "select * from t_emp where id=?"; Emp emp = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Emp.class),1); System.out.println(emp); }
④查询数据返回list集合
@Test //查询多条数据为一个list集合 public void testSelectList(){ String sql = "select * from t_emp"; List<Emp> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Emp.class)); System.out.println(list); }
⑤查询返回单个的值
@Test //查询单行单列的值 public void selectCount(){ String sql = "select count(id) from t_emp"; Integer count = jdbcTemplate.queryForObject(sql, Integer.class); System.out.println(count); }