1.spring整合JDBC
1>spring内部提供了很多模板整合Dao的技术
spring中提供了一个可以操作数据库的对象,该对象封住了jdbc技术:ComboPooledDataSource/JdbcTemplate与DBUtils中的QueryRunner非常的相似
使用JdbcTemplate模板操作数据库
//0 准备连接池
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql:///web35_hibernate");
dataSource.setUser("root");
dataSource.setPassword("123456");
//1 创建JDBC模板对象
JdbcTemplate jt = new JdbcTemplate();
jt.setDataSource(dataSource);
//2 书写sql,并执行
String sql = "insert into t_user values(null,'rose') ";
jt.update(sql);
2>spring整合JDBC:
1.导包:4+2,spring-test spring-aop junit4类库,c3p0连接词 JDBC驱动,spring-jdbc spring-tx事务
2.准备数据库
3.书写Dao(UserDao/UserDaoImpl)
//使用JDBC模板实现增删改查
public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
@Override
public void save(User u) {
String sql = "insert into t_user values(null,?) ";
super.getJdbcTemplate().update(sql, u.getName());
}
@Override
public void delete(Integer id) {
String sql = "delete from t_user where id = ? ";
super.getJdbcTemplate().update(sql,id);
}
@Override
public void update(User u) {
String sql = "update t_user set name = ? where id=? ";
super.getJdbcTemplate().update(sql, u.getName(),u.getId());
}
@Override
public User getById(Integer id) {
String sql = "select * from t_user where id = ? ";
return super.getJdbcTemplate().queryForObject(sql,new RowMapper<User>(){
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}}, id);
}
@Override
public int getTotalCount() {
String sql = "select count(*) from t_user ";
Integer count = super.getJdbcTemplate().queryForObject(sql, Integer.class);
return count;
}
@Override
public List<User> getAll() {
String sql = "select * from t_user ";
List<User> list = super.getJdbcTemplate().query(sql, new RowMapper<User>(){
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}});
return list;
}
4.applicationContext.xml配置
依赖关系:
<!-- 读取db.properties配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 1.将连接池放入spring容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 2.将UserDao放入spring容器 -->
<bean name="userDao" class="com.imwj.a_jdbcTemplate.UserDaoImpl">
<!-- <property name="jt" ref="jdbcTemplate" ></property> -->
<property name="dataSource" ref="dataSource"></property>
</bean>
5.测试使用
@Test
public void fun1(){
User user = new User();
user.setId(2);
user.setName("jack");
userDao.save(user);
}
进阶:
读取外部的Properties配置:
<!-- 读取db.properties配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
2.spring中的aop事务
1>事务
事务的特性:acid(原子性,隔离性,一致性,持久性)
事务的并发问题:脏读、不可重复读、幻读
事务的隔离级别:1 读未提交、2 读已提交、4 可重复读、8 串行化
2>封装了事务的管理代码
事务操作:打开事务、提交事务、回滚事务
事务操作对象:不同平台操作事务的代码也各不相同,spring提供了一个接口
PlatformTransactionManager 接口:DataSourceTransactionManager、HibernateTransitionmanager在spring中事务管理.最为核心的对象就是TransactionManager对象
spring管理事务的属性介绍:
事务的隔离级别:1 读未提交、2 读已提交、4 可重复读、8 串行化
是否只读:true 只读、false 可操作
事务的传播行为:一个service方法调用另一个service方法时,遵循那边的事务(REQUIRED常用 / 默认)
REQUIRED:支持当前事务,如果不存在就新建一个(默认/常用)
3>spring管理事务的方式
xml配置(aop)
1.导包
4+2,aop aspect,aop联盟 weaving织入包
2.导入新的约束
beans: 最基本
context:读取properties配置
aop:配置aop
tx:配置事务通知
3.配置通知:事务核心管理器,封装了所有事务操作. 依赖于连接池(DataSourceTransactionManager)
<!--事务核心管理器和事务模板对象:-->
<!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource" ></property>
</bean>
<!-- 事务模板对象 -->
<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
<property name="transactionManager" ref="transactionManager" ></property>
</bean>
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<!-- 以方法为单位,指定方法应用什么事务属性
name:方法名(*通配符,只要是以saver开头的方法...)
isolation:隔离级别
propagation:传播行为
read-only:是否只读
-->
<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
<!-- 测试事务方法(方法名自己写全名) -->
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
</tx:attributes>
</tx:advice>
4.将通知织入目标对象
<!-- 配置织入 -->
<aop:config >
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(* com.imwj.service.*ServiceImpl.*(..))" id="txPc"/>
<!-- 配置切面 : 通知+切点
advice-ref:通知的名称
pointcut-ref:切点的名称
-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
</aop:config>
注解配置事务(aop)
1.导包(同上)
2.导入新的约束(同上)
3.开启注解管理事务(也要<!--事务核心管理器和事务模板对象:-->)
<!-- 开启使用注解管理aop事务 -->
<tx:annotation-driven/>
4.使用注解:注解也可以放在类名上(对整个类都执行事务)
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
public void transfer(final Integer from,final Integer to,final Double money) {
//减钱
ad.decreaseMoney(from, money);
int i = 1/0;
//加钱
ad.increaseMoney(to, money);
}