spring boot集成mybatis

1.配置application.properties

spring.datasource.url=jdbc:mysql://localhost: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

2.配置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>

 

3.编写通用Mapper

import tk.mybatis.mapper.common.Mapper;

import tk.mybatis.mapper.common.MySqlMapper;

/**
* 继承自己的Mapper
*/
public interface ApiBaseMapper<T> extends Mapper<T>, MySqlMapper<T> {
}

 

4.配置mybatisconfig

import java.util.Properties;


import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import com.github.pagehelper.PageHelper;

@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("Echo_service.domain");//别名类,配置这个不需要写类的全路径

//分页插件
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);

//添加插件
bean.setPlugins(new Interceptor[]{pageHelper});

//设置配置参数
org.apache.ibatis.session.Configuration conf = new org.apache.ibatis.session.Configuration();
conf.setMapUnderscoreToCamelCase(true);
conf.setLogPrefix("api-cloud.");//日志信息前缀
bean.setConfiguration(conf);

//添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));//mapper文件存放路径 
return bean.getObject();
} catch (Exception e) {
throw new RuntimeException(e);
}

}


@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}

@Bean
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}
}

5.配置scanner

import org.springframework.context.annotation.Bean;


import org.springframework.context.annotation.Configuration;

import tk.mybatis.spring.mapper.MapperScannerConfigurer;

import java.util.Properties;

/**
* MyBatis鎵弿鎺ュ彛锛屼娇鐢ㄧ殑tk.mybatis.spring.mapper.MapperScannerConfigurer
*/
@Configuration
public class MyBatisMapperScannerConfig {

@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
mapperScannerConfigurer.setBasePackage("Echo_service.mapper");//扫描mapper接口中的路径
Properties properties = new Properties();
properties.setProperty("mappers", "Echo_service.mybatis.ApiBaseMapper");//设置通用mapper
properties.setProperty("notEmpty", "false");
properties.setProperty("IDENTITY", "MYSQL");
mapperScannerConfigurer.setProperties(properties);
return mapperScannerConfigurer;
}

}

 

posted @ 2017-06-30 10:08  奋小斗g  阅读(378)  评论(0编辑  收藏  举报