Spring系列教程——14Spring数据库操作讲解
上一篇:Spring系列教程——13AspectJ讲解
下一篇:Spring系列教程——15Spring事务管理器
文章目录
一.JdbcTemplate的使用
1.简介
jdbcTemplate类似DBUtils,用于操作Jdbc的工具类,它需要依赖于连接池DataSource(数据源)
相关概念:
1.ODBC(Open Database Connectivity,ODBC)开放数据库连接,是微软公司开提供了一组对数据库访问的标准API(应用程序编程接口)
2.DBCP(DataBase Connection Pool)数据库连接池,是java数据库连接池的一种,由Apache开发
3.C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。目前使用它的开源项目有Hibernate,Spring等。
c3p0与dbcp区别:
1.c3p0有自动回收空闲连接功能dbcp没有自动回收空闲连接的功能
。
2.c3p0提供最大空闲时间,DBCP提供最大连接数
。
3.c3p0当连接超过最大空闲连接时间时,当前连接就会被断掉。DBCP当连接数超过最大连接数时,所有连接都会被断开
2.使用讲解
在前面章节的基础上我们需要导入下面jar包:
链接:https://pan.baidu.com/s/1V-eAEG4X-3W3_20hxr-c_w
提取码:ckbn
1.准备表数据
:
CREATE DATABASE SpringFrameWork;
USE SpringFrameWork;
CREATE TABLE t_user(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50),
PASSWORD VARCHAR(32)
);
INSERT INTO t_user(username,PASSWORD) VALUES('jack','520');
INSERT INTO t_user(username,PASSWORD) VALUES('rose','521');
2.测试
//创建数据源(使用dbcp连接池)
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/springframework");
dataSource.setUsername("root");
dataSource.setPassword("123456");
//创建jdbcTemplate
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update("update t_user set username = ? where id = ?", "tom","1");
三.Spring数据源配置讲解
1.使用dbcp的配置
将前面章节讲的UserServiceImpl
里面添加下面内容:
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
private JdbcTemplate jdbcTemplate;
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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="password" value="123456"></property>
<property name="username" value="root"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/SpringFrameWork"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userService" class="service.UserServiceImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>
同时addUser()
修改为:
public void addUser(User user) {
jdbcTemplate.update("insert into t_user (username,password) values(?,?)",user.getUsername(),user.getPassword());
}
测试代码为:
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
IUserService userService = (IUserService) context.getBean("userService");
User user = new User();
user.setUsername("jerry");
user.setPassword("666");
userService.addUser(user);
2.使用c3p0的配置
仅需在dbcp配置的基础上将
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="password" value="123456"></property>
<property name="username" value="root"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/SpringFrameWork"></property>
</bean>
改为
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="password" value="123456"></property>
<property name="user" value="root"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/SpringFrameWork"></property>
</bean>
三.JdbcDaoSupport使用讲解
1.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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="password" value="123456"></property>
<property name="user" value="root"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/SpringFrameWork"></property>
</bean>
<bean id="userService" class="service.UserServiceImpl`在这里插入代码片`">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
2.修改addUser()内容
public void addUser(User user) {
getJdbcTemplate().update("insert into t_user (username,password) values(?,?)",user.getUsername(),user.getPassword());
}
测试代码:
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
IUserService userService = (IUserService) context.getBean("userService");
User user = new User();
user.setUsername("king");
user.setPassword("888");
userService.addUser(user);
四.properties文件简化dataSource配置
1.db.properties
文件内容:
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/SpringFrameWork
user=root
password=123456
2.接下来在beans.xml
文件里面配置
<!--获取db.properties里的属性-->
<context:property-placeholder location="db.properties"></context:property-placeholder>
3.dataSource
配置相关改为
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="password" value="${password}"></property>
<property name="user" value="${user}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
</bean>
再来测试一次:
上一篇:Spring系列教程——13AspectJ讲解
下一篇:Spring系列教程——15Spring事务管理器