好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

spring 整合JDBC和AOP事务的方法

spring 整合jdbc

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

 

orm持久化技术 模板类
jdbc org.springframework.jdbc.core.jdbctemplate
hibernate3.0 org.springframework.orm.hiberate3.hibernatetemplate
ibatis(mybatis) org.springframework.orm.sqlmapclienttemplate
jpa org.springframework.orm.jpa.jpatemplate

 

spring中提供了一个可以操作数据库的对象.对象封装了jdbc技术.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

// jdbctemplate => jdbc模板对象

// 与dbutils中的queryrunner非常相似.

// 0 准备连接池

combopooleddatasource datasource = new combopooleddatasource();

datasource.setdriverclass( "com.mysql.jdbc.driver" );

datasource.setjdbcurl( "jdbc:mysql:///hibernate_32" );

datasource.setuser( "root" );

datasource.setpassword( "1234" );

// 1 创建jdbc模板对象

jdbctemplate jt = new jdbctemplate();

jt.setdatasource(datasource);

// 2 书写sql,并执行

string sql = "insert into t_user values(null,'rose') " ;

jt.update(sql);

步骤

1.导包

基础包4 + 2 spring-test spring-aop junit4类库 新增c3p0连接池jdbc驱动包 spring-jdbc包 spring-tx 事务 包

2.准备数据库

本地数据库和表

3.书写dao

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

// 使用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.spring配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

<!-- 指定spring读取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 .将jdbctemplate放入spring容器 -->

<bean name= "jdbctemplate" class = "org.springframework.jdbc.core.jdbctemplate" >

   <property name= "datasource" ref= "datasource" ></property>

</bean>

 

<!-- 3 .将userdao放入spring容器 -->

<bean name= "userdao" class = "cn.zhli13.jdbctemplate.userdaoimpl" >

   <property name= "jt" ref= "jdbctemplate" ></property>

</bean>

 

// 若userdao类继承了jdbcdaosupport,则无需将jdbctemplate注入到容器中,省略第二步,第三步改为

  <!-- 3 .将userdao放入spring容器 -->

<bean name= "userdao" class = "cn.zhli13.jdbctemplate.userdaoimpl" >

   <property name= "datasource" ref= "datasource" ></property>

</bean>

spring中aop事务

事务

事务特性:acid

事务并发问题:脏读、不可重复读、幻读

事务的隔离级别:1 读未提交、2 读已提交、4 可重复读、8 串行化

spring封装了事务管理代码

事务操作:1、打开事务、2、提交事务、3、回滚事务

事务操作对象

因为在不同平台,操作事务的代码各不相同.spring提供了一个接口

platformtransactionmanager 接口

datasourcetransactionmanager hibernatetransitionmanager

注意:在spring中玩事务管理.最为核心的对象就是transactionmanager对象

spring管理事务的属性介绍

事务的隔离级别:1 读未提交、2 读已提交、4 可重复读、8 串行化

是否只读: true只读 false可操作

事务的传播行为:决定业务方法之间调用,事务应该如何处理

* 保证同一个事务中
propagation_required 支持当前事务,如果不存在 就新建一个(默认、推荐99.99%用这种)
propagation_supports 支持当前事务,如果不存在,就不使用事务
propagation_mandatory 支持当前事务,如果不存在,抛出异常

* 保证没有在同一个事务中
propagation_requires_new 如果有事务存在,挂起当前事务,创建一个新的事务
propagation_not_supported 以非事务方式运行,如果有事务存在,挂起当前事务
propagation_never 以非事务方式运行,如果有事务存在,抛出异常
propagation_nested 如果当前事务存在,则嵌套事务执行

spring管理事务方式

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

// 编码式

// 1.将核心事务管理器配置到spring容器

<!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 -->

<bean name= "transactionmanager"        class = "org.springframework.jdbc.datasource.datasourcetransactionmanager" >

   <property name= "datasource" ref= "datasource" ></property>

</bean>

// 2.配置transactiontemplate模板

<!-- 事务模板对象 -->

<bean name= "transactiontemplate" class = "org.springframework.transaction.support.transactiontemplate" >

   <property name= "transactionmanager" ref= "transactionmanager" >  </property>

</bean>

// 3.将事务模板注入service

<!-- 3 .service-->

<bean name= "accountservice" class = "cn.itcast.service.accountserviceimpl" >

   <property name= "ad" ref= "accountdao" ></property>

   <property name= "tt" ref= "transactiontemplate" ></property>

</bean> 

xml配置(aop)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

// 1.导包

  // aop、aspect、aopliance、aspect.weaver

  // 2.导入新的约束(tx)

  // beans: 最基本、context:读取properties配置、aop:配置aop、tx:配置事务通知

  // 3.配置通知

  <!-- 配置事务通知 -->

<tx:advice id= "txadvice" transaction-manager= "transactionmanager" >

   <tx:attributes>

     <!-- 以方法为单位,指定方法应用什么事务属性

       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(* cn.zhli13.service.*serviceimpl.*(..))" id= "txpc" />

   <!-- 配置切面 : 通知+切点

        advice-ref:通知的名称

        pointcut-ref:切点的名称

    -->

   <aop:advisor advice-ref= "txadvice" pointcut-ref= "txpc" />

</aop:config>

注解配置(aop)

?

1

2

3

4

5

6

7

8

9

10

11

// 1.导包(如xml配置一样)

// 2.导入约束(同上)

// 3.开启注解管理事务

<!-- 开启使用注解管理aop事务 -->

<tx:annotation-driven/>

// 4.使用注解

@transactional (isolation=isolation.repeatable_read,propagation=propagation.required,readonly= true )

public class accountserviceimpl implements accountservice {

 

@transactional (isolation=isolation.repeatable_read,propagation=propagation.required,readonly= false )

public void transfer( final integer from, final integer to, final double money) {

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://segmentfault.com/a/1190000015822794

查看更多关于spring 整合JDBC和AOP事务的方法的详细内容...

  阅读:39次