Spring框架对jdbc经行封装,使用jdbcTemplate 对数据库实现增删改查
1.搭建子模块:sprig-jdbc
2.导入依赖
<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>
3.创建jdbc配置文件文件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
4.配置spring的配置文件bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 引用外部配置文件-->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<!--配置数据源-->
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></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>
5.准备数据库与测试表
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;
6.实现CRUD
- (1)、创建测试类、整合Junit,注入jdbcTemplate
@SpringJUnitConfig(locations = "classpath:bean.xml") @Component public class JDBCTemplateTest { @Autowired private JdbcTemplate jdbcTemplate;
}
- (2)、测试增删改功能
增删改都是调用jdbcTemplate的update()方法实现
```java
@Test
public void testAdd(){
String sql = "insert into t_emp values(null,?,?,?);";
int update = jdbcTemplate.update(sql, "lwj", 22, "男");
System.out.println(update);
}
@Test
public void testModify(){
String sql = "update t_emp set age=? where id=?";
int update = jdbcTemplate.update(sql, 18, 1);
System.out.println(update);
}
@Test
public void testDelete(){
String sql = "delete from t_emp where id=?";
int update = jdbcTemplate.update(sql, 2);
System.out.println(update);
}
- (3).测试查询数据返回对象
- 1、声明对象
package com.lwj.spring6.jdbc;
public class Emp {
Integer id;
String name;
String age;
String sex;
//get set 方法
@Override
public String toString() {
return “Emp{” +
“id=” + id +
“, name='” + name + ”’ +
“, age='” + age + ”’ +
“, sex='” + sex + ”’ +
‘}’;
}
}
- 2.查询返回对象
查询单个对象使用queryForObject方法
```java
@Test
public void testQueryObject(){
String sql = "select * from t_emp where id=?";
Emp emp = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Emp.class), 1);
System.out.println(emp);
}
- (4).测试查询方法放回List数组
- 查询list数组使用query方法
@Test public void testQueryForList(){ String sql = "select * from t_emp"; List<Emp> emps = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Emp.class)); System.out.println(emps); }(5).测试查询方法返回单个值
@Test public void testQuerySingle(){ String sql = "select count(id) from t_emp"; Integer i = jdbcTemplate.queryForObject(sql, Integer.class); System.out.println(i); }
真不错
哈哈哈