1. Add dependencies in the POM:
#mybatis依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
#mysql
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
#分页插件
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.1</version>
</dependency>
#通用mapper
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.3.6</version>
</dependency>
2. Add in Application.properties:
Spring.datasource.url=jdbc:mysql://ip:3306/myht?characterencoding=utf-8
Spring.datasource.username=root
Spring.datasource.password=root
Spring.datasource.driver-class-name=com.mysql.jdbc.driver
Spring.datasource.minPoolSize = 3
Spring.datasource.maxPoolSize = 25
Spring.datasource.maxLifetime = 20000
Spring.datasource.borrowConnectionTimeout = 30
Spring.datasource.loginTimeout = 30
Spring.datasource.maintenanceInterval = 60
Spring.datasource.maxIdleTime = 60
Spring.datasource.testQuery =select 1
Spring.datasource.autoReconnect = True
Spring.datasource.test-while-idle=true
spring.datasource.time-between-eviction-runs-millis=27800
3.MyBatis Basic Configuration
/** * MyBatis Basic Configuration * * @author Liuzh * @since 2015-12-19 10:11 * *
@Configuration
@EnableTransactionManagement
public class Mybatisconfig implements Transactionmanagementconfigurer {
@Autowired
DataSource DataSource;
@Bean (name = "Sqlsessionfactory")
Public Sqlsessionfactory Sqlsessionfactorybean () {
Sqlsessionfactorybean bean = new Sqlsessionfactorybean ();
Bean.setdatasource (DataSource);
Bean.settypealiasespackage ("Com.bfd.api.*.domain");
Paging Plugin
Pagehelper pagehelper = new Pagehelper ();
Properties Properties = new properties ();
Properties.setproperty ("reasonable", "true");
Properties.setproperty ("Supportmethodsarguments", "true");
Properties.setproperty ("Returnpageinfo", "check");
Properties.setproperty ("params", "count=countsql");
Pagehelper.setproperties (properties);
Add Plugin
Bean.setplugins (New Interceptor[]{pagehelper});
Setting Configuration parameters
Org.apache.ibatis.session.Configuration conf = new org.apache.ibatis.session.Configuration ();
Conf.setmapunderscoretocamelcase (TRUE);
Conf.setlogprefix ("Api-cloud.");
Bean.setconfiguration (conf);
Add an XML directory
Resourcepatternresolver resolver = new Pathmatchingresourcepatternresolver ();
try {
Bean.setmapperlocations (Resolver.getresources ("Classpath:mapper/*.xml"));
return Bean.getobject ();
} catch (Exception e) {
Logutil.error ("Classpath:mapper/*.xml", "Mapper parsing error", E);
throw new RuntimeException (e);
}
}
@Bean
Public sqlsessiontemplate sqlsessiontemplate (sqlsessionfactory sqlsessionfactory) {
return new Sqlsessiontemplate (sqlsessionfactory);
}
@Bean
@Override
Public Platformtransactionmanager Annotationdriventransactionmanager () {
return new Datasourcetransactionmanager (DataSource);
}
}
4.MyBatis Scanning interface
/** * MyBatis Scan interface * * @author Liuzh * @since 2015-12-19 14:46 * *
@Configuration //todo Note that Since Mapperscannerconfigurer executes earlier, it must have the following annotations @AutoConfigureAfter ( Mybatisconfig.class) public class mybatismapperscannerconfig { @Bean public mapperscannerconfigurer mapperscannerconfigurer () { Mapperscannerconfigurer mapperscannerconfigurer = new mapperscannerconfigurer (); Mapperscannerconfigurer.setsqlsessionfactorybeanname ( "sqlsessionfactory"); Mapperscannerconfigurer.setbasepackage ( "Tk.mybatis.springboot.mapper"); return mapperscannerconfigurer; } }
This configuration must be aware @AutoConfigureAfter(MyBatisConfig.class)
that this configuration is required, otherwise there will be exceptions. The reason is that this class executes earlier, and because sqlSessionFactory
it does not exist, subsequent executions fail.
You can use MyBatis after you have configured the above configuration.
Spring Boot Integrated MyBatis