Springboot 中同时使用mybatis注解和springbean-xml配置方式
2017-12-21 21:17
1186 查看
因为自己新建了一个应用,为了开发的速度,直接选用了springboot,但后来发现大部分读库的代码和同事已有的代码重复, 索性直接拿过来用。但问题是我已有的代码是通过纯注解的方式使用mybatis,同事代码是spring+xml来使用mybatis,经过几天的探索,发现一种两种方式结合使用的方法。
我们在spring中用@Autowired获取mybatis mapper时,其实是Spring通过mybatis的sqlSessionFactory获取到的,mybatis-starter在启动过程中会在spring容器中注册好sqlSessionFactory, 但这starter并不会读取xml中配置的mapper。但如果你先让spring通过bean xml注册了sqlSessionFactory并读取了xml中的mapper配置,就无法注册mybatis-stater中的autoconfigure就会失败,你用纯注解写的那个mapper就加载不上了。
所以先让springboot在启动时候先执行完成mybatis-starter中的MybatisAutoConfiguration,这时候在spring容器中sqlSessionFactory已经注册好了,然后把关于mapper的springbean xml配置文件读取并配置,配置过程中spring会先尝试注册sqlSessionFactory,其实已经有了,就会用这个factory把xml中mapper再加载一遍,因为spring中默认都是单例, 所以不会重建mybatis-starter中创建的sqlSessionFactory, 这里非常关键的一点就是加载xml必须在MybatisAutoConfiguration完成后,具体配置代码如下。
application-bean.xml里就是用来注册mybatis mapper的spring bean配置文件。
application-bean.xml如下
把你的mapper.xml文件放到config目录下就可以了,这样你就可以xml或者注解随意切换了。 我的感觉简单sql用注解,配置简单迅速。 复杂sql可以用xml,排查问题方便。
我们在spring中用@Autowired获取mybatis mapper时,其实是Spring通过mybatis的sqlSessionFactory获取到的,mybatis-starter在启动过程中会在spring容器中注册好sqlSessionFactory, 但这starter并不会读取xml中配置的mapper。但如果你先让spring通过bean xml注册了sqlSessionFactory并读取了xml中的mapper配置,就无法注册mybatis-stater中的autoconfigure就会失败,你用纯注解写的那个mapper就加载不上了。
所以先让springboot在启动时候先执行完成mybatis-starter中的MybatisAutoConfiguration,这时候在spring容器中sqlSessionFactory已经注册好了,然后把关于mapper的springbean xml配置文件读取并配置,配置过程中spring会先尝试注册sqlSessionFactory,其实已经有了,就会用这个factory把xml中mapper再加载一遍,因为spring中默认都是单例, 所以不会重建mybatis-starter中创建的sqlSessionFactory, 这里非常关键的一点就是加载xml必须在MybatisAutoConfiguration完成后,具体配置代码如下。
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @Configuration @ImportResource(locations={"classpath:application-bean.xml"}) @AutoConfigureAfter(MybatisAutoConfiguration.class) //这里就是保证xml在MybatisAutoConfiguration完成配置的核心 public class ApplicationConfig { private void test(){ } }
application-bean.xml里就是用来注册mybatis mapper的spring bean配置文件。
application-bean.xml如下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <bean id="dataSource" class="here is your datasource class" init-method="init"> <property name="user" value="xxx"/> <property name="passwd" value="xxxx"/> </bean> <bean id="mysqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--数据源配置,必须--> <property name="dataSource" ref="dataSource"/> <!-- mybatis的一些基本属性的配置 --> <property name="configLocation" value="classpath:mybatisConfig.xml"/> <!-- 如果mapper文件与mapper类放在相同的路劲下,则不需要配置路径 --> <property name="mapperLocations" value="classpath*:/config/*mapper.xml"/> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类,并帮你自动生成相关bean --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="me.xindoo.dao" /> <property name="sqlSessionFactoryBeanName" value="mysqlSessionFactory"></property> </bean> </beans>
把你的mapper.xml文件放到config目录下就可以了,这样你就可以xml或者注解随意切换了。 我的感觉简单sql用注解,配置简单迅速。 复杂sql可以用xml,排查问题方便。
相关文章推荐
- springboot使用之二:整合mybatis(xml方式)并添加PageHelper插件
- spring注解与xml同时使用:bean命名问题及冲突
- spring aop的使用(注解方式以及基于xml配置方式)
- Spring(三):IoC容器装配Bean(xml配置方式和注解方式)
- Spring Boot中使用MyBatis注解配置详解
- Spring学习(二十二) Bean配置的三种方式(XML、注解、Java类)介绍与对比
- Spring Boot中使用MyBatis注解配置详解
- Spring学习(九)使用ioc注解方式配置bean
- Spring+SpringMVC+Mybatis使用注解方式配置双数据源
- Spring Boot中使用MyBatis注解配置详解
- 8 -- 深入使用Spring -- 4...6 AOP代理:基于注解的XML配置文件的管理方式
- spring3 混合使用 XML 与注解(Annotation)进行 Bean 的配置
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- 当Spring同时使用了XML配置和注解
- Spring Boot中使用MyBatis注解配置详解
- spring学习笔记(12)——使用注解方式配置bean
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- spring框架,可以没有spring.xml(即配置<beans>的文件吗?即不使用xml方式配置,而是只使用注解的方式配置
- 简化Spring的XML配置(二)——使用注解装配bean