SSM框架-Spring框架
spring提供了很多模板整合Dao技术
1.SpringTemplate
1.1.SpringTemplate概述
spring中提供了一个可以操作数据库的对象.对象封装了jdbc技术.
JDBCTemplate => JDBC模板对象
与DBUtils中的QueryRunner非常相似.
1.2.入门案例
1.2.1.添加功能
User对象入参
步骤:
导入jar包
基本jar包 4 2
aop包
操作jdbc
数据库
总体jar包图
插入数据
@Test public void fun1() throws PropertyVetoException { ComboPooledDataSource cbd = new ComboPooledDataSource(); cbd.setDriverClass("com.mysql.jdbc.Driver"); cbd.setJdbcUrl("jdbc:mysql://localhost:3306/demo"); cbd.setUser("root"); cbd.setPassword("12345"); // 创建模板对象 JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(cbd); // 编写sql String sql = "INSERT INTO tb_user VALUES(null,'杰克');"; jdbcTemplate.update(sql); } |
---|
1.2.2.查询
1.2.2.1.根据id查询
1.2.2.2.查询返回list集合
1.2.2.3.查询统计记录数
1.2.3.修改
User对象入参
1.2.4.删除
根据id删除
2.Spring整合jdbc
2.1.思路分析
连接池
Jdbc模板
dao
2.2.编写dao层
2.2.1.编写UserDao接口
public interface UserDao { // 添加用户 void addUser(User user); //删除用户 public void deleteUser(Integer id); // 改 public void updateUser(User id); // 根据id查询单个用户 public User findUserById(Integer id); //返回用户的记录数 public Integer getUserCount(); //返回user列表 public ListgetUserList(); } |
---|
2.2.2.编写UserDaoImpl
public class UserDaoImpl implements UserDao { private JdbcTemplate jd;public void setJd(JdbcTemplate jd) { this.jd = jd; } public JdbcTemplate getJd() { return jd; } @Override public void addUser(User user) { String sql = "INSERT INTO tb_user VALUES(null,?)"; jd.update(sql, user.getName()); } @Override public void deleteUser(Integer id) { String sql = "delete from tb_user where id=?"; jd.update(sql, id); } @Override public void updateUser(User user) { String sql = "UPDATE tb_user SET `name` = ? WHERE id =?"; jd.update(sql, user.getName(), user.getId()); } @Override public User findUserById(Integer id) { String sql = "select * from tb_user where id=?"; User user = jd.queryForObject(sql, new RowMapper() { @Override public User mapRow(ResultSet rs, int arg1) throws SQLException { User user = new User(); user.setId(rs.getInt("id")); user.setName(rs.getString("name")); return user; } }, id); return user; } @Override public ListgetUserList() { String sql = "select * from tb_user"; Listlist = jd.query(sql,new RowMapper() { @Override public User mapRow(ResultSet rs, int arg1) throws SQLException { User user = new User(); user.setId(rs.getInt("id")); user.setName(rs.getString("name")); return user; } }); return list; }@Overridepublic Integer getUserCount() { String sql = "select count(*) from tb_user"; Integer integer = jdbcTemplate.queryForObject(sql, Integer.class); return integer;}} |
---|
2.2.3.编写Spring核心配置文件
xml version="1.0" encoding="UTF-8"?><< span="">beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><< span="">bean name="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> << span="">property name="driverClass" value="com.mysql.jdbc.Driver">property> << span="">property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo">property> << span="">property name="user" value="root">property> << span="">property name="password" value="12345">property>bean> << span="">bean name="jdbcTemplete" class="org.springframework.jdbc.core.JdbcTemplate"> << span="">property name="DataSource" ref="datasource">property>bean><< span="">bean name="userDao" class="com.shop.dao.UserDaoImpl"> << span="">property name="jd" ref="jdbcTemplete">property>bean>beans> |
---|
2.2.4.编写测试代码
@Test public void fun2() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userdao = (UserDao) ac.getBean("userDao"); User user = new User(); user.setName("lucy"); userdao.addUser(user); } |
---|
3.Spring整合事务操作
3.3.事务案例介绍
转账操作
3.4.事务案例准备
3.4.1.Dao层
package com.shop.dao;public interface AaccountDao { // 加钱 void jiaqian(Integer id,double money); // 减钱 void jianqian(Integer id,double money);} |
---|
public class AccountDaoImpl implements AaccountDao { private JdbcTemplate jd; @Override public void jiaqian(Integer id, double money) { String sql = "update tb_account set money=money ? where id=?"; jd.update(sql,money,id); } @Override public void jianqian(Integer id, double money) { String sql = "update tb_account set money=money-? where id=?"; jd.update(sql, money,id); } public JdbcTemplate getJd() { return jd; } public void setJd(JdbcTemplate jd) { this.jd = jd; }} |
---|
3.4.2.service层
package com.shop.service;public interface AccountService { void zhuanzhang(Integer from,Integer to,double money);} |
---|
package com.shop.service;import com.shop.dao.AaccountDao;public class AccountServiceImpl implements AccountService{ private AaccountDao dao; @Override public void zhuanzhang(Integer from, Integer to, double money) { //减钱 dao.jianqian(from, money); //加钱 dao.jiaqian(to, money); } public AaccountDao getDao() { return dao; } public void setDao(AaccountDao dao) { this.dao = dao; }} |
---|
3.5.Spring核心配置文件
<< span="">bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> << span="">property name="driverClass" value="com.mysql.jdbc.Driver">property> << span="">property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo">property> << span="">property name="user" value="root">property> << span="">property name="password" value="12345">property>bean><< span="">bean name="jdbcTemplete" class="org.springframework.jdbc.core.JdbcTemplate"> << span="">property name="DataSource" ref="dataSource">property>bean> << span="">bean name="accountDao" class="com.shop.dao.AccountDaoImpl"> << span="">property name="jd" ref="jdbcTemplete">property>bean><< span="">bean name="accountService" class="com.shop.service.AccountServiceImpl"> << span="">property name="dao" ref="accountDao">property>bean> |
---|
3.6.测试
public class Demo1 {@Test public void fun1(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService as = (AccountService) ac.getBean("accountService"); as.zhuanzhang(1, 2, 100d);}} |
---|
3.7.测试没有事务问题
4.Spring整合事务
4.0.事务回顾
事务特性:acid
事务并发问题
脏读
不可重复读
幻读
事务的隔离级别
1 读未提交
2 读已提交
4 可重复读
8 串行化
4.1.Spring中事务介绍
spring封装了事务管理代码
事务操作
打开事务
提交事务
回滚事务
事务操作对象
因为在不同平台,操作事务的代码各不相同.spring提供了一个接口
PlatformTransactionManager 接口
事务传播行为:
4.2.Spring整合事务方式
方式1 模板方式
1.将核心事务管理器配置到spring容器
<< span="">bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> << span="">property name="dataSource" ref="dataSource">property>bean> |
---|
2.配置TransactionTemplate模板
<< span="">bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> << span="">property name="transactionManager" ref="transactionManager">property>bean> |
---|
3.将事务模板注入Service
<< span="">bean name="accountService" class="com.shop.service.AccountServiceImpl"> << span="">property name="accountDao" ref="accountDao">property> << span="">property name="tt" ref="transactionTemplate">property>bean> |
---|
完整xml配置参考:
<< span="">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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> << span="">bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> << span="">property name="driverClass" value="com.mysql.jdbc.Driver">property> << span="">property name="jdbcUrl" value="jdbc:mysql://localhost:3306/gz1960">property> << span="">property name="user" value="root">property> << span="">property name="password" value="12345">property> bean> << span="">bean name="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> << span="">property name="dataSource" ref="dataSource">property> bean> << span="">bean name="template" class="org.springframework.transaction.support.TransactionTemplate"> << span="">property name="transactionManager" ref="dataSourceTransactionManager">property> bean> << span="">bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> << span="">property name="dataSource" ref="dataSource">property> bean> << span="">bean name="accountDao" class="com.shop.dao.impl.AccountDaoImpl"> << span="">property name="jdbcTemplate" ref="jdbcTemplate">property> bean> << span="">bean name="accountService" class="com.shop.service.impl.AccountServiceImpl"> << span="">property name="accountDao" ref="accountDao">property> << span="">property name="tt" ref="template">property> bean>beans> |
---|
4.在Service中调用模板
public class AccountServiceImpl implements AccountService{ private AccountDao accountDao; private TransactionTemplate tt; @Override public void transfer(Integer from, Integer to, double money) { tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus arg0) { //先减掉张三的钱 accountDao.lostMoney(from, money); int i = 1/0; //再把李四的钱加上 accountDao.addMoney(to, money); } }); } public AccountDao getAccountDao() { return accountDao; } public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } public TransactionTemplate getTt() { return tt; } public void setTt(TransactionTemplate tt) { this.tt = tt; }} |
---|
5.测试
方式2 xml方式
在Spring 中 xml方式实现 事务操作其本质也是AOP 操作
1)回顾aop操作(aop事务)
2)导入包 (4 2 1)
3)再导入事务所需要的包
最终jar包的图解:
4)导入新的约束
5)配置事务通知
<< span="">bean name="jdbcTemplete" class="org.springframework.jdbc.core.JdbcTemplate"> << span="">property name="DataSource" ref="dataSource">property>bean> << span="">tx:advice id="txAdvice" transaction-manager="transactionManager"> << span="">tx:attributes> << span="">tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> tx:attributes>tx:advice><< span="">bean name="accountDao" class="com.shop.dao.AccountDaoImpl"> << span="">property name="jd" ref="jdbcTemplete">property>bean> |
---|
6)配置织入
<< span="">aop:config> << span="">aop:pointcut expression="execution(* com.shop.service.AccountServiceImpl.transfer(..))" id="txPC"/> << span="">aop:advisor advice-ref="txAdvice" pointcut-ref="txPC"/>aop:config> |
---|
完整xml配置参考:
xml version="1.0" encoding="UTF-8"?><< span="">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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> << span="">bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> << span="">property name="driverClass" value="com.mysql.jdbc.Driver">property> << span="">property name="jdbcUrl" value="jdbc:mysql://localhost:3306/gz1960">property> << span="">property name="user" value="root">property> << span="">property name="password" value="12345">property> bean> << span="">bean name="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> << span="">property name="dataSource" ref="dataSource">property> bean> << span="">tx:advice id="txadvice" transaction-manager="dataSourceTransactionManager"> << span="">tx:attributes> << span="">tx:method name="zhuanzhang" isolation="READ_COMMITTED" propagation="REQUIRED" read-only="false"/> tx:attributes> tx:advice> << span="">aop:config> << span="">aop:pointcut id="pc" expression="execution(* com.shop.service.impl.AccountServiceImpl.zhuanzhang(..))">aop:pointcut> << span="">aop:advisor advice-ref="txadvice" pointcut-ref="pc">aop:advisor> aop:config> << span="">bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> << span="">property name="dataSource" ref="dataSource">property> bean> << span="">bean name="accountDao" class="com.shop.dao.impl.AccountDaoImpl"> << span="">property name="jdbcTemplate" ref="jdbcTemplate">property> bean> << span="">bean name="accountService" class="com.shop.service.impl.AccountServiceImpl"> << span="">property name="accountDao" ref="accountDao">property> bean>beans> |
---|
方式3 注解方式
1)导包
跟xml 方式一样
2)添加新约束
跟xml方式一样
3)使用注解
将事务添加到方法上
private AccountDao accountDao; private TransactionTemplate tt; @Override @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false) public void transfer(Integer from, Integer to, double money) { //先减掉张三的钱 accountDao.lostMoney(from, money); int i = 1/0; //再把李四的钱加上 accountDao.addMoney(to, money); } |
---|
将事务添加到类上
问题:如果在service层有很多业务方法,莫非都需要添加一个??
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)public class AccountServiceImpl implements AccountService{ private AccountDao accountDao; private TransactionTemplate tt; @Override public void transfer(Integer from, Integer to, double money) { //先减掉张三的钱 accountDao.lostMoney(from, money); int i = 1/0; //再把李四的钱加上 accountDao.addMoney(to, money); } |
---|
事务同时添加在类和方法
再思考,有没有问题?如果某个业务方法事务不一样呢?
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)public class AccountServiceImpl implements AccountService{ private AccountDao accountDao; private TransactionTemplate tt; @Override @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false) public void transfer(Integer from, Integer to, double money) { //先减掉张三的钱 accountDao.lostMoney(from, money); int i = 1/0; //再把李四的钱加上 accountDao.addMoney(to, money); } |
---|
4) 开启注解事务支持
xml version="1.0" encoding="UTF-8"?><< span="">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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> << span="">bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> << span="">property name="driverClass" value="com.mysql.jdbc.Driver">property> << span="">property name="jdbcUrl" value="jdbc:mysql://localhost:3306/gz1960">property> << span="">property name="user" value="root">property> << span="">property name="password" value="12345">property> bean> << span="">bean name="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> << span="">property name="dataSource" ref="dataSource">property> bean> << span="">tx:annotation-driven transaction-manager="dataSourceTransactionManager">tx:annotation-driven> << span="">bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> << span="">property name="dataSource" ref="dataSource">property> bean> << span="">bean name="accountDao" class="com.shop.dao.impl.AccountDaoImpl"> << span="">property name="jdbcTemplate" ref="jdbcTemplate">property> bean> << span="">bean name="accountService" class="com.shop.service.impl.AccountServiceImpl"> << span="">property name="accountDao" ref="accountDao">property> bean>beans> |
---|