Spring 框架对 JDBC 进行封装,使用 JdbcTemplate 方便实现对数据库操作
pom.xml
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>5.3.1version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.3.1version>
dependency>
jdbc.properties(之前创建过)
jdbc.driver = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
jdbc.username = root
jdbc.password = yuxingxuan
spring-JdbcTemplate.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 https://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.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="druidDataSource"/>
bean>
beans>
JdbcTemplateTest.java
package com.atguigu.JdbcTemplate.test;
import com.atguigu.mybatis.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
//设置当前测试类的运行环境-->在spring的测试环境中运行,就可以通过注入的方式直接获取IOC容器中的bean
@RunWith(SpringJUnit4ClassRunner.class)
//设置Spring测试环境的配置文件
@ContextConfiguration("classpath:spring-JdbcTemplate.xml")
public class JdbcTemplateTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void testInsert(){
String sql = "insert into t_user values(null,?,?,?,?,?)";
//添加一条数据
jdbcTemplate.update(sql,"test","123",22,"女","[email protected]");
}
@Test
public void testSelect(){
String sql = "select * from t_user where id = ?";
//查询id为1用户信息(导入mybatis的pojo目录下的User)
User user = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(User.class),27);
System.out.println(user);
}
@Test
public void testGetAllUser(){
String sql = "select * from t_user where id > 25";
//获取多条信息
List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
list.forEach(System.out::println);
}
@Test
public void testGetCount(){
String sql = "select count(*) from t_user";
//获取总记录数
Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
System.out.println("表中记录数为:"+count);
}
}
INFO 02-02 22:43:23,124 {dataSource-1} inited (DruidDataSource.java:794)
表中记录数为:15
User{id=26, username='admin1', password='123', age=20, gender='女', email='[email protected]'}
User{id=27, username='test', password='123', age=22, gender='女', email='[email protected]'}
User{id=28, username='test', password='123', age=22, gender='女', email='[email protected]'}
User{id=27, username='test', password='123', age=22, gender='女', email='[email protected]'}
INFO 02-02 22:43:24,758 {dataSource-1} closed (DruidDataSource.java:1543)