发表于: 2018-01-23 11:17:18
1 581
今日完成:MyBatis和Spring的集成
链接:http://www.importnew.com/23949.html
MyBatis和Spring的集成工作是由MyBatis团队完成的。所以我们首先要先引入MyBatis和Spring的集成依赖包。这里我用的是Gradle,如果使用其他构建工具,将下面的语句改为相应的。
1 | compile group: 'org.mybatis' , name: 'mybatis-spring' , version: '1.3.1' |
声明Spring Bean
集成包中包含了org.mybatis.spring.SqlSessionFactoryBean
类,这是一个工厂类,可以方便的创建MyBatis的SqlSessionFactory
。所有属性均可以通过该类进行设置。如果希望使用传统的XML配置的话,也可以直接设置configLocation
属性为MyBatis配置文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <!--MyBatis的SqlSessionFactory--> < bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > < property name = "dataSource" ref = "dataSource" /> < property name = "configLocation" value = "configuration.xml" /> </ bean > <!--数据源--> < bean id = "dataSource" class = "com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource" > < property name = "user" value = "root" /> < property name = "password" value = "12345678" /> < property name = "url" value = "jdbc:mysql://localhost:3306/test" /> < property name = "useSSL" value = "false" /> </ bean > |
事务管理
MyBatis是一个轻量级的框架,没有自己的事务管理器。我们直接使用JDBC事务管理器即可。其它地方和别的事务配置方法差不多,就不详细介绍了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <!--MyBatis使用JDBC的事务管理器--> < bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name = "dataSource" ref = "dataSource" /> </ bean > < tx:advice id = "txAdvice" transaction-manager = "transactionManager" > < tx:attributes > < tx:method name = "get*" read-only = "true" /> < tx:method name = "*" /> </ tx:attributes > </ tx:advice > <!--使用AOP设置事务管理--> < aop:config > < aop:pointcut id = "dao" expression = "execution(* yitian.study.dao.*.*(..))" /> < aop:advisor advice-ref = "txAdvice" pointcut-ref = "dao" /> </ aop:config > |
使用SqlSession
MyBatis还提供了一个MyBatisTemplate
类,会将SqlSession
交由Spring管理。我们只要声明该对象,并注入到代码中使用即可。
1 2 3 4 5 6 | <!--MyBatis的SqlSession模板,封装了SqlSession--> < bean id = "sqlSessionTemplate" class = "org.mybatis.spring.SqlSessionTemplate" > < constructor-arg type = "org.apache.ibatis.session.SqlSessionFactory" ref = "sqlSessionFactory" /> </ bean > |
或者还可以继承SqlSessionDaoSupport
类,它提供了getSqlSession()
方法,可以直接获取当前由Spring管理的SqlSession。
注入映射器
MyBatis的映射器可以通过MapperFactoryBean
工厂类来注册。注册之后,我们就可以直接注入到DAO中使用,连MyBatisTemplate
都免了。
1 2 3 4 | < bean id = "userMapper" class = "org.mybatis.spring.mapper.MapperFactoryBean" > < property name = "mapperInterface" value = "org.mybatis.spring.sample.mapper.UserMapper" /> < property name = "sqlSessionFactory" ref = "sqlSessionFactory" /> </ bean > |
如果有多个映射器的话,我们甚至可以利用MyBatis提供的命名空间直接扫描所有映射器。
1 2 | <!--自动搜索Mapper--> < mybatis:scan base-package = "yitian.study.mapper" /> |
然后我们的代码就变得非常干净。
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 | @Repository public class AuthorDaoImpl implements AuthorDao { private SqlSessionTemplate template; private MultiMapper mapper; @Autowired public AuthorDaoImpl(SqlSessionTemplate template, MultiMapper mapper) { this .template = template; //模板对象甚至都没使用 this .mapper = mapper; } @Override public void add(Author author) { mapper.insertAuthor(author); } @Override public Author getById( int id) { return mapper.selectAuthorById(id); } @Override public Author getByName(String username) { return mapper.selectAuthorByName(username); } @Override public void update(Author author) { mapper.updateAuthor(author); } @Override public void delete(Author author) { mapper.deleteAuthor(author); } } |
封装异常
Spring可以将Hibernate、JPA等不同技术的异常,统一封装为Spring自己的异常层次。这样我们就能在Spring程序中统一处理异常了。
首先我们的DAO类需要@Repository注解。
1 2 3 4 5 6 7 8 9 10 11 12 | @Repository public class AuthorDaoImpl implements AuthorDao { private SqlSessionTemplate template; private MultiMapper mapper; @Autowired public AuthorDaoImpl(SqlSessionTemplate template, MultiMapper mapper) { this .template = template; this .mapper = mapper; } //其他代码省略了 } |
然后我们需要声明下面两个Spring Bean。
1 2 3 4 5 6 | <!--异常拦截器,用于将MyBatis封装为Spring异常--> < bean class = "org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> < bean class = "org.mybatis.spring.MyBatisExceptionTranslator" > < constructor-arg type = "javax.sql.DataSource" ref = "dataSource" /> < constructor-arg type = "boolean" value = "false" /> </ bean > |
这样代码中抛出的JDBC异常都会被翻译成Spring的异常。由于Spring中可以对@Repository注解的类进行常见JDBC异常的封装,所以即使没有上面的声明,我们得到的异常也是Spring的DataAccessException。一开始我以为Spring没有进行异常转换,后来我发现原来由于Spring贴心的打印了原异常,把我搞晕了。如果捕获异常然后查看一下异常类型,就会发现已经是Spring的异常了。
评论