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
- <!--data Source-
- <Bean id= "dataSource" class="Com.mchange.v2.c3p0.ComboPooledDataSource " destroy-method="Close">
- <property name= "driverclass" value="${${database.type}.jdbc.driverclassname}"/ >
- <property name="Jdbcurl" value="${${database.type}.jdbc.url}"/>
- <property name="Properties" ref="datasourceproperties"/>
- <property name="Autocommitonclose" value="true"/>
- <property name="checkouttimeout" value="${cpool.checkouttimeout}"/>
- <property name="initialpoolsize" value="${cpool.minpoolsize}"/>
- <property name="minpoolsize" value="${cpool.minpoolsize}"/>
- <property name="maxpoolsize" value="${cpool.maxpoolsize}"/>
- <property name="MaxIdleTime" value="${cpool.maxidletime}"/>
- <property name="acquireincrement" value="${cpool.acquireincrement}"/>
- <property name="maxidletimeexcessconnections" value= "${ cpool.maxidletimeexcessconnections} "/>
- </Bean>
- <!--annotation-based transaction management-
- <Bean id= "transactionmanager" class=" Org.springframework.jdbc.datasource.DataSourceTransactionManager ">
- <property name= "dataSource" ref="DataSource"/>
- </Bean>
- <tx:annotation-driven transaction-manager="TransactionManager"/>
- <Bean id= "lazysqlsessionfactory" class="Org.mybatis.spring.SqlSessionFactoryBean " >
- <property name= "dataSource" ref="DataSource"/>
- <property name= "configlocation" value="Classpath:mybatis/mybatis-config.xml"/ >
- </Bean>
- <!--scan MyBatis Map interface class--
- <Bean class="Org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name= "basepackage" value="COM.TEST.DSM"/>
- <property name= "sqlsessionfactorybeanname" value="lazysqlsessionfactory"/ >
- </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
- <Bean id= "lazysqlsessionfactory" class="Org.mybatis.spring.SqlSessionFactoryBean">
- <property name= "dataSource" ref="DataSource"/>
- <property name= "configlocation" value="Classpath:mybatis/mybatis-config.xml"/>
- <!--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.
- * is a wildcard, representing all the files, * * On behalf of all directories-->
- <property name= "mapperlocations" value="Classpath:com/test/mapper/mysql/**/*.xml " />
- </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