Spring框架对JDBC进行封装,使用JdbcTemplate方便对数据库操作。
1.搭建模块
2.引入依赖
<dependencies>
<!-- spring jdba 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.33</version>
</dependency>
<!-- 数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.15</version>
</dependency>
</dependencies>
3.创建配置文件
jdbc.properties
jdbc.user=root
jdbc.password=Mitaowulong
jdbc.url=jdbc:mysql://localhost:3306/yogurt
jdbc.driver=com.mysql.cj.jdbc.Driver
bean.xml
<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"></context:property-placeholder>
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 创建jdbcTemplate对象,注入数据-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="druidDataSource"></property>
</bean>
</beans>
4. 创建数据库
create table t_emp(
id int 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
5.创建测试类
package com.yogurt.spring6.jdbc;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
/**
* ClassName: JdbcTemplateTest
* Package: IntelliJ IDEA
* Description:
*
* @Author 半生瓜
* @Create 2023/11/8 22:17
* @Version 1.0
*/
@SpringJUnitConfig(locations = "classpath:bean.xml")
public class JdbcTemplateTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void testUpdate(){
}
}
6.使用JdbaTemplate实现对数据的CRUD
添加、修改、删除
//添加 修改 删除
@Test
public void testUpdate(){
/*
//1.添加操作
//第一步 编写sql语句
String sql = "insert into t_emp values(null,?,?,?)";
//调用jdbcTemplate的方法,传入相关参数
// Object[] params = {"李一", 20, "未知"};
// int rows = jdbcTemplate.update(sql,params);
int rows = jdbcTemplate.update(sql,"李三", 18, "女");
System.out.println(rows);
*/
/*
//修改操作
String sql = "update t_emp set name = ? where id = ?";
int rows = jdbcTemplate.update(sql, "周一", 2);
System.out.println(rows);
*/
//删除操作
String sql = "delete from t_emp where id = ?";
int rows = jdbcTemplate.update(sql, 2);
System.out.println(rows);
}
查询
1.建实体类
public class Emp {
private Integer id;
private String name;
private Integer age;
private String sex;
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
2.测试
@SpringJUnitConfig(locations = "classpath:bean.xml")
public class JdbcTemplateTest {
@Autowired
private JdbcTemplate jdbcTemplate;
//查询:返回对象
@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
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 testSelectValue(){
String sql = "select count(*) from t_emp";
Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
System.out.println(count);
}