20.SSM整合-全注解开发

全注解开发

  1.将SpringMVC改为注解

    修改spring-mvc.xml

  2.将Spring改为注解

      将Service改为注解,完成Dao的注入

      将事务以注解方式织入到Service

        1.修改spring-tx.xml,只负责事务的开启     

1 <!-- 配置事务管理器 -->
2     <!-- 开启Spring中的事务管理(声明式的事务管理) xml-->
3     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
4         <property name="dataSource" ref="dataSource"></property>
5     </bean>
6     
7     <!-- 事务注解 驱动,使用注解进行事务管理时 需要注册 -->
8     <tx:annotation-driven transaction-manager="transactionManager"/>

        2.在service中使用注解织入事务 

1 @Transactional     //声明式的事务管理
2     public void add(Student student) {
3         studentDao.insert(student);    
4     }

  3.将Mybatis 改为注解(删除mapper文件)(一般Mybatis不使用注解)

  

1 public interface StudentDao {
2     //一般 情况下,为了 性能考虑, mybatis是不进行 注解的 
3     @Insert(value="insert into student (sname) values (#{sname})")
4     void insert(Student student);
5 }
原文地址:https://www.cnblogs.com/xuzekun/p/7418044.html