Spring integration MyBatis configuration instructions

MyBatis-Spring configuration is simple to understand

[SqlSessionFactoryBean](https://mybatis.github.io/spring/zh/factorybean.html)配置

In basic MyBatis, session factories can be created using SqlSessionFactoryBuilder. In MyBatis-Spring, SqlSessionFactoryBean is used instead.

Example

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="mapperLocations">
    <array>
      <value>classpath:mapper/*.xml</value>
    </array>
  </property>
  <property name="typeAliasesPackage" value="com.isea533.mybatis.model"/>
  <property name="plugins">
    <array>
      <bean class="com.github.pagehelper.PageHelper">
        <property name="properties">
          <value>
            dialect=hsqldb
            reasonable=true
          </value>
        </property>
      </bean>
      <bean class="com.github.abel533.mapperhelper.MapperInterceptor">
        <property name="properties">
          <value>
            mappers=com.github.abel533.mapper.Mapper
            IDENTITY=MYSQL
            notEmpty=true
          </value>
        </property>
      </bean>
    </array>
  </property>
</bean>
  • 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

dataSourceAttributes

This property must be configured. There are multiple data sources, and multiple data sources dataSourceneed to be configured sqlSessionFactoryto correspond.

mapperLocationsAttributes

After configuring this property, sqlSessionFactoryall files under this path will be automatically scanned and parsed.

This path supports multiple and can ,;\t\nbe divided by using.

Each path can be either a direct package path, or an Ant-style expression.

configLocationAttributes

This property is not used in the above example. When the SqlSessionFactoryBeanprovided configuration cannot meet the requirements, you can use the mybatis-config.xmlconfiguration file to configure other properties, and then configLocationspecify the path of the configuration through the property , and SqlSessionFactoryBeanthe configuration file will be used to create it Configuration.

typeAliasesPackageAttributes

This property can register an alias for a class in the package, and the class name can be used directly after registration, instead of using the fully qualified class name (that is, without including the package name).

This property can be configured multiple times, and can be used ,;\t\nto divide. But Ant-style paths are not supported .

pluginsAttributes

This property can configure the interceptor of MyBatis, and the configuration order of the interceptor will affect the execution order of the interceptor.

Interceptors from top to bottom, the actual execution order is this, the first interceptor will be executed last, and the last one will be executed first.

然后出拦截器的顺序和配置的顺序一致,第一个最先返回,最后一个最后返回。

就以上面的配置为例,一个简单的执行顺序图如下:

执行顺序

这些拦截器执行的顺序都是环绕型,不要简单理解为简单的顺序执行。

从配置顺序来看,第一个配置的在最里面,后面的依次环绕上一个拦截器。

MapperScannerConfigurer 配置

为了代替手工使用 SqlSessionDaoSupport 或 SqlSessionTemplate 编写数据访问对象 (DAO)的代码,MyBatis-Spring 提供了一个动态代理的实现:MapperFactoryBean。这个类 可以让你直接注入数据映射器接口到你的 service 层 bean 中。当使用映射器时,你仅仅如调 用你的 DAO 一样调用它们就可以了,但是你不需要编写任何 DAO 实现的代码,因为 MyBatis-Spring 将会为你创建代理。

示例

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.isea533.mybatis.mapper"/>
    <property name="annotationClass" value="XXX"/>
    <property name="sqlSessionFactoryBeanName" ref="sqlSessionFactory"/>
</bean>
  • 1
  • 2
  • 3
  • 4
  • 5

一般用过的最多也就是这3个属性,实际情况下最常用的只有basePackage

basePackage属性

basePackage可以写多个,可以用,;\t\n进行分割。

每一个路径都可以用直接的包路径,或者Ant风格的表达式。

annotationClass属性

该属性实际上就是起到一个过滤的作用,如果设置了该属性,那么MyBatis的接口只有包含该注解,才会被扫描进去。

sqlSessionFactoryBeanName属性

这个属性一般都用不到,只有当你配置多数据源的时候,这是会有多个sqlSessionFactory,你就需要通过该属性来指定哪一个sqlSessionFactory(值为SqlSessionFactoryBean <bean>配置中的id属性)。

Ant通配符

  • ? 匹配任何单字符
  • * 匹配0或者任意数量的字符
  • ** 匹配0或者更多的目录

举例

  • /project/*.a 匹配项目根路径下所有在project路径下的.a文件
  • /project/p?ttern 匹配项目根路径下 /project/pattern 和 /app/pXttern,但是不包括/app/pttern
  • /**/example 匹配项目根路径下 /project/example, /project/foow/example, 和 /example
  • /project/**/dir/file.* 匹配项目根路径下/project/dir/file.jsp, /project/foow/dir/file.html
  • /**/*.jsp 匹配项目根路径下任何的.jsp 文件

最长匹配原则(has more characters)

URL请求/project/dir/file.jsp,现在存在两个路径匹配模式/**/*.jsp和/project/dir/*.jsp,那么会根据模式/project/dir/*.jsp来匹配

https://blog.csdn.net/isea533/article/details/45640319

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324892091&siteId=291194637