spring整合JDBC
spring提供了很多模板整合Dao技术

spring中提供了一个可以操作数据库的对象,对象封装了jdbc技术
JDBCTemplate----JDBC模板技术
与DButils中的QueryRunner非常相似
package com.jdbcdemo;
import java.beans.PropertyVetoException;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JDBCDemo {
@Test
public void method() throws PropertyVetoException{
//创建连接池对象
ComboPooledDataSource dataSource =new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql:///spring_demo?characterEncoding=utf-8");
dataSource.setUser("root");
dataSource.setPassword("123456");
//创建JDBC模板对象
JdbcTemplate jdbcTemplate=new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
//书写sql语句并执行
String sql="insert into s_user(name) values('李四')";
jdbcTemplate.update(sql);
}
}
步骤:导包:4个基础包+2个日志包+spring-test、spring-aop、junit4类库+c3p0连接池、spring-tx事务
准备数据库
书写Dao层
package com.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.domain.User;
public class UserDao extends JdbcDaoSupport {
// private JdbcTemplate jdbcTemplate;
//
// public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
// this.jdbcTemplate = jdbcTemplate;
// }
public void save(User user) {
String sql = "insert into s_user(name) values(?)";
getJdbcTemplate().update(sql, user.getName());
}
public void update(User user) {
String sql = "update s_user set name=? where id=?";
getJdbcTemplate().update(sql, user.getName(), user.getId());
}
public void delete(Integer id) {
String sql = "delete from s_user where id=?";
getJdbcTemplate().update(sql, id);
}
// 单个对象查询
public User findById(Integer id) {
String sql = "select * from s_user where id=?";
return getJdbcTemplate().queryForObject(sql, new RowMapper<User>() {
public User mapRow(ResultSet rs, int arg1) throws SQLException {
// TODO Auto-generated method stub
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
return user;
}
}, id);
}
// 查询单个值
public int getCount() {
String sql = "select count(*) from s_user";
return getJdbcTemplate().queryForObject(sql, Integer.class);
}
// 查询List<User>
public List<User> getAll() {
String sql = "select * from s_user";
return getJdbcTemplate().query(sql, new RowMapper<User>() {
public User mapRow(ResultSet rs, int arg1) throws SQLException {
// TODO Auto-generated method stub
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
return user;
}
});
}
}
spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- datasource -->
<context:property-placeholder location="classpath:db.properties"/>
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- jdbcTemplate -->
<!-- <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>-->
<!-- UserDao -->
<bean name="userDao" class="com.dao.UserDao">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- AccountDao -->
<bean name="accountDao" class="com.dao.AccountDao">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean name="accountService" class="com.service.AccountService">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 注解配置事务 -->
<tx:annotation-driven/>
<!-- XML配置事务 -->
<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.service.*Service.*(..))" id="txpc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txpc"/>
</aop:config>-->
</beans>
测试
package com.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.dao.UserDao;
import com.domain.User;
import com.service.AccountService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
// 引用注入
@Autowired // 自动装配
@Qualifier("userDao") // 使用Qualifier注解告诉spring容器自动装配哪个名称的对象
private UserDao userDao;
@Autowired // 自动装配
@Qualifier("accountService")
private AccountService accountService;
@Test
public void method1() {
User user = new User();
user.setName("qwerty");
userDao.save(user);
}
@Test
public void method2() {
accountService.transfer(1, 2, 2000d);
}
}
进阶内容
JDBCDaoSupport



读取外部的properties配置
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql:///spring_demo?characterEncoding=utf-8 jdbc.user=root jdbc.password=123456
spring中的aop事务
事务:事务特性:acid
事务并发问题:脏读、不可重复读、幻读
事务的隔离级别:1 读未提交 2 读已提交 4 可重复读 8 串行化
spring封装了事务管理代码
事务操作:打开事务、提交事务、回滚事务
事务操作对象:因为在不同平台,操作事务的代码各不相同,spring提供了一个接口
PlatformTransactionManager接口:DataSourceTransactionManager
HibernateTransitionmanager
注:在spring中玩事务管理,最为核心的对象就是TransactionManager
spring管理事务的属性介绍:事务的隔离级别:1 读未提交 2 读已提交 4 可重复读 8 串行化
是否只读:true 只读 false 可操作
事务的传播行为

spring管理事务的方式
XML配置(aop)
注解配置(aop)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- datasource -->
<context:property-placeholder location="classpath:db.properties"/>
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- jdbcTemplate -->
<!-- <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>-->
<!-- UserDao -->
<bean name="userDao" class="com.dao.UserDao">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- AccountDao -->
<bean name="accountDao" class="com.dao.AccountDao">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean name="accountService" class="com.service.AccountService">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 注解配置事务 -->
<tx:annotation-driven/>
<!-- XML配置事务 -->
<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.service.*Service.*(..))" id="txpc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txpc"/>
</aop:config>-->
</beans>