10

Springboot 中同时使用mybatis注解和springbean-xml配置方式

 3 years ago
source link: https://zxs.io/article/1222
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Springboot 中同时使用mybatis注解和springbean-xml配置方式
当前位置:XINDOO > 编程 > 正文

  因为自己新建了一个应用,为了开发的速度,直接选用了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完成后,具体配置代码如下。

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,排查问题方便。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK