Pick upmybatis-spring Use summary.
First, Mybatis-spring-boot-starter would:
- AutoDetect an existing DataSource.
- Would create and register an instance of a Sqlsessionfactorybean passing that DataSource As an input.
- Would create and register an instance of a sqlsessiontemplate got out of the sqlsessionfactor Ybean.
- Autoscan your mappers, link them to the sqlsessiontemplate and registers them to Spring context so the Y can is injected into your beans.
Source: http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/index.html
That is, after you have used the starter, you only need to define a DataSource, which automatically creates Sqlsessionfactorybean and sqlsessiontemplate that use the datasource. Automatically scans your mappers, connects to Sqlsessiontemplate, and registers into the spring context. Mybatis-spring-boot-application configuration parameters are also saved in the Application.properties file, using the prefix MyBatis.
| Property
Description |
Config-location |
MyBatis XML config file (optional) |
Mapper-locations |
Mapper XML config files (optional) |
Type-aliases-package |
Package to search for type aliases (optional) |
Type-handlers-package |
Package to search for type aliases (optional) |
Executor-type |
Executor type:simple, Reuse, BATCH (optional) |
Configuration |
A MyBatis Configuration Bean. About available properties See the MyBatis reference page. NOTE This property cannot use at the same time with the config-location. |
(Starter) There are two ways to set up mapper: ① uses config-location to specify a config XML, where mapper and alias are set. See Example 1. ② use Type-aliases-package, need to cooperate with automatic scanning mappers use. For the second, it is important to note that if you want to automatically scan the mappers, you need to label the @mapper on the Mapper interface, otherwise it will fail. Also, you need to declare in the Application.properties file: Mybatis.type-aliases-package. Mapper-locations This configuration parameter is valid only if the mapper XML is not in the same directory as the Mapper class. So you can generally ignore it.Example 1(Specify config XML via mybatis.config-location, then set the alias and Mapper package inside):
#application. Propertiesmybatis.config-location=mybatis-config.xml
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Configuration Public "-//mybatis.org//dtd Config 3.0//en" "http://mybatis.org/dtd/mybatis-3-conf Ig.dtd "><Configuration> <typealiases> < Packagename= "Sample.mybatis.domain"/> </typealiases> <mappers> <MapperResource= "Sample/mybatis/mapper/citymapper.xml"/> <MapperResource= "Sample/mybatis/mapper/hotelmapper.xml"/> </mappers></Configuration>
Example 2(Configure MyBatis with Mybatis.type-aliases-package and @Mapper):
#application. Propertiesmybatis.type-aliases-package=com.expert.pojo
Package Com.expert.dao;import Org.apache.ibatis.annotations.mapper;import Org.apache.ibatis.annotations.Select; Import com.expert.pojo.User; @Mapperpublic interface Usermapper {// @Select ("select * from User WHERE id = #{ID}" ) User getById (String ID); @Select ("SELECT * from user WHERE id = #{ID}") user getById2 (String ID);}
Note: @Alias ("xxx") is used to set aliases, not for scanning.
Mybatis-spring-boot Usage Summary