不知不觉,我在乐字节已经学了13天了,什么时候我能变成大手呢,唉还是一步步来吧,把基础打好~
今天学的是Spring JDBC
主要内容
Spring 整合 JDBC 环境
Spring 框架除了提供 IOC 与 AOP 核心功能外,同样提供了基于JDBC 的数据访问功能,使得访问持久层数据更加方便。使用 Spring JDBC 环境,首先需要一套 Spring 整合 JDBC 的环境。
添加依赖坐标
org.springframework spring-context 5.2.4.RELEASE org.springframework spring-test 5.2.4.RELEASE test org.aspectj aspectjweaver 1.9.5 org.springframework spring-jdbc 5.2.4.RELEASE org.springframework spring-tx 5.2.4.RELEASE mysql mysql-connector-java 8.0.19 com.mchange c3p0 0.9.5.5 添加 jdbc 配置文件 在src/main/resources目录下新建jdbc.properties配置文件,并设置对应的配置信息驱动名
jdbc.driver=com.mysql.cj.jdbc.Driver
数据库连接
jdbc.url=jdbc:mysql://localhost:3306/(数据库名称)?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
数据库用户名称
jdbc.user=(数据库账号)
数据库用户密码
jdbc.password=(数据库密码)
以下为可选配置
指定连接池的初始化连接数。取值应在minPoolSize 与 maxPoolSize 之间.Default:3
initialPoolSize=20
指定连接池中保留的最大连接数. Default:15
maxPoolSize=100
指定连接池中保留的最小连接数
minPoolSize=10
最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0
maxIdleTime=600
当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3
acquireIncrement=5
JDBC的标准,用以控制数据源内加载的PreparedStatements数量。
maxStatements=5
每60秒检查所有连接池中的空闲连接.Default:0
idleConnectionTestPeriod=60
修改 spring 配置文件
<context:property-placeholder location=“jdbc.properties” />
spring.xml
<!-- Spring扫描注解的配置 -->
<context:component-scan base-package="com.xxxx" />
<!-- 加载properties 配置文件 -->
<context:property-placeholder location="jdbc.properties" />
配置数据源 由于建立数据库连接是一个非常耗时耗资源的行为,所以通过连接池预先同数据库建立一些连接,放在内存中,应用程序需要建立数据库连接时直接到连接池中申请一个就行,用完后再放回去。
C3P0 与 DBCP 二选一即可
DBCP(DataBase connection pool),数据库连接池。是 apache 上的一个 java 连接池项目,也是 tomcat 使用的连接池组件。单独使用dbcp需要2个包:commons-dbcp.jar,commons-pool.jar dbcp,没有自动回收空闲连接的功能。
C3P0是一个开源的JDBC连接池,它实现了数据源,支持JDBC3规范和JDBC2的标准扩展。目前使用它的开源项目有Hibernate,Spring等。c3p0有自动回收空闲连接功能。
C3P0 数据源配置
C3P0 其他额外配置(对应的值在jdbc.properties文件中指定) DBCP 数据源配置 模板类配置 Spring把 JDBC 中重复的操作建立成了一个模板类:org.springframework.jdbc.core.JdbcTemplate 。 JDBC 测试 创建指定数据库 选择连接,右键选择"新建数据库",设置数据库的名称和编码格式创建数据表
使用 JUnit 测试
通过 junit 测试 jdbcTemplate bean 是否获取到
JUnit 测试
public class SpringJdbcTest01 {
@Test
public void testQueryCount() {
// 获取spring上下文环境
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
// 得到模板类 JdbcTemplate对象
JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
// 定义sql语句
String sql = "select count(1) from tb_account";
// 执行查询操作(无参数)
Integer total= jdbcTemplate.queryForObject(sql, Integer.class);
System.out.println("总记录数:" + total);
}
@Test
public void testQueryCountByUserId() {
// 获取spring上下文环境
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
// 得到模板类 JdbcTemplate对象
JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
// 定义sql语句
String sql = " select count(1) from tb_account where user_id = ?";
// 执行查询操作(有参数)
Integer total = jdbcTemplate.queryForObject(sql, Integer.class, 1);
System.out.println("总记录数:" + total);
}
}
简单封装
public class SpringJdbcTest02 {
private JdbcTemplate jdbcTemplate;
@Before
public void init() {
// 得到Spring上下文环境
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
// 得到模板类 JdbcTemplate对象
jdbcTemplate = (JdbcTemplate) ac.getBean("jdbcTemplate");
}
@Test
public void testQueryCount() {
// 定义sql语句
String sql = "select count(1) from tb_account";
// 执行查询操作(无参数)
Integer total= jdbcTemplate.queryForObject(sql, Integer.class);
System.out.println("总记录数:" + total);
}
@Test
public void testQueryCountByUserId() {
// 定义sql语句
String sql = " select count(1) from tb_account where user_id = ?";
// 执行查询操作(有参数)
Integer total = jdbcTemplate.queryForObject(sql, Integer.class, 1);
System.out.println("总记录数:" + total);
}
}
注解封装
@RunWith
就是一个运行器
@RunWith(JUnit4.class) 就是指用JUnit4来运行
@RunWith(SpringJUnit4ClassRunner.class) 让测试运行于Spring测试环境
@ContextConfiguration
Spring整合JUnit4测试时,使用注解引入多个配置文件
@ContextConfiguration(Locations=“classpath:applicationContext.xml”)
@ContextConfiguration(locations = {“classpath