MyBatis Spring Integrated Configuration Mapping interface classes and mapping XML files

Source: Internet
Author: User

Spring Consolidated MyBatis is used in mybatis-spring, and mapperscannerconfigurer is typically used when configuring MyBatis mapping files. Mapperscannerconfigurer will automatically scan the Basepackage specified package, locate the mapping interface class and map the XML file, and inject it. The configuration is as follows:

[HTML]View PlainCopy
  1. <!--data Source-
  2. <Bean id= "dataSource" class="Com.mchange.v2.c3p0.ComboPooledDataSource " destroy-method="Close">
  3. <property name= "driverclass" value="${${database.type}.jdbc.driverclassname}"/ >
  4. <property name="Jdbcurl" value="${${database.type}.jdbc.url}"/>
  5. <property name="Properties" ref="datasourceproperties"/>
  6. <property name="Autocommitonclose" value="true"/>
  7. <property name="checkouttimeout" value="${cpool.checkouttimeout}"/>
  8. <property name="initialpoolsize" value="${cpool.minpoolsize}"/>
  9. <property name="minpoolsize" value="${cpool.minpoolsize}"/>
  10. <property name="maxpoolsize" value="${cpool.maxpoolsize}"/>
  11. <property name="MaxIdleTime" value="${cpool.maxidletime}"/>
  12. <property name="acquireincrement" value="${cpool.acquireincrement}"/>
  13. <property name="maxidletimeexcessconnections" value= "${ cpool.maxidletimeexcessconnections} "/>
  14. </Bean>
  15. <!--annotation-based transaction management-
  16. <Bean id= "transactionmanager" class=" Org.springframework.jdbc.datasource.DataSourceTransactionManager ">
  17. <property name= "dataSource" ref="DataSource"/>
  18. </Bean>
  19. <tx:annotation-driven transaction-manager="TransactionManager"/>
  20. <Bean id= "lazysqlsessionfactory" class="Org.mybatis.spring.SqlSessionFactoryBean " >
  21. <property name= "dataSource" ref="DataSource"/>
  22. <property name= "configlocation" value="Classpath:mybatis/mybatis-config.xml"/ >
  23. </Bean>
  24. <!--scan MyBatis Map interface class--
  25. <Bean class="Org.mybatis.spring.mapper.MapperScannerConfigurer">
  26. <property name= "basepackage" value="COM.TEST.DSM"/>
  27. <property name= "sqlsessionfactorybeanname" value="lazysqlsessionfactory"/ >
  28. </Bean>

The prerequisite for this configuration is that the mapping interface class file (. java) and the mapping XML file (. xml) need to be placed under the same package (COM.TEST.DSM)

What if the MyBatis map XML file and the mapping interface file are not placed under the same package?

If you are under a different package, you will need to manually configure the path to the XML file, only need to modify the Sqlsessionfactorybean configuration:

[HTML]View PlainCopy
  1. <Bean id= "lazysqlsessionfactory" class="Org.mybatis.spring.SqlSessionFactoryBean">
  2. <property name= "dataSource" ref="DataSource"/>
  3. <property name= "configlocation" value="Classpath:mybatis/mybatis-config.xml"/>
  4. <!--when the MyBatis XML file and the Mapper interface are not under the same package, you need to specify the path to the XML file with the Mapperlocations property.
  5. * is a wildcard, representing all the files, * * On behalf of all directories-->
  6. <property name= "mapperlocations" value="Classpath:com/test/mapper/mysql/**/*.xml " />
  7. </Bean>


Add a Mapperlocations property that specifies the path to the XML file to load.

Classpath: Indicates the search in the classes directory;

*: wildcard denotes all files;

* *: denotes all directories;

MyBatis Official website description is as follows: http://mybatis.github.io/spring/factorybean.html

Properties

Sqlsessionfactory has a single required property, the JDBC DataSource . This can is any DataSource and should is configured just like any other Spring database connection.

One common property is configlocation which are used to specify the location of the MyBatis XML configuration file . One case where this is needed are if the base MyBatis configuration needs to be changed. Usually this'll be <settings> or <typeAliases> sections.

Note that this config file does not need to is a complete MyBatis config. Specifically, any environments, data sources and MyBatis transaction managers would beignored . Sqlsessionfactorybean creates its own, custom MyBatis environment with these values set as required.

Another reason to require a config file is if the MyBatis mapper XML files be not in the same classpath location as the M Apper classes. With this configuration, there is the options. This first was to manually specify the classpath of the XML files using a <mappers> sections in the MyBatis C Onfig file. A second option is to use themapperlocations property of the factory bean.

The mapperlocations property takes a list of resource locations. This property can is used to specify the location of MyBatis XML mapper files. The value can contain ant-style patterns to load all files in a directory or to recursively search all paths from a base L Ocation. For example:

<bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >  <property name= " DataSource "ref=" DataSource "/>  <property name=" mapperlocations "value=" classpath*:sample/config/mappers /**/*.xml "/></bean>

This would load all the MyBatis mapper XML files on the Sample.config.mappers package and its sub-packages from the Classpa Th.

MyBatis Spring Integrated Configuration Mapping interface classes and mapping XML files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.