最近刚接触spring boot,正是因为他的及简配置方便开发,促使我下定决心要用它把之前写的项目重构,那么问题来了,spring boot怎么整合mybatis呢,下面几个配置类来搞定。
在我的代码当中是实现了数据库读写分离的,所以代码仅做参考,如有需要可以加我微信:benyzhous
【后续更新】
1、文件结构
DatabaseConfiguration.Java用来获取数据库连接配置信息,配置从application.properties中读取
MybatisConfiguration.java也就是MyBatis配置核心入口,构建连接创建SqlSessionFactory
2、下面直接贴代码,有问题的话可以留言或者加我的微信公众号:cha-baba,或者个人微信号:benyzhous
application.yml 相关配置
# Server settings
server:
port:8080
address:localhost
# DATASOURCE
jdbc:
driverClass: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/local-kaishustory?useUnicode=true&characterEncoding=utf-8
username: root
password: root
# SPRING PROFILES
spring:
# HTTP ENCODING
http:
encoding.charset: UTF-8
encoding.enable: true
encoding.force: true
# WeiXin Configuration
weixin:
mp:
appid: xx
secret: ee
token: weixin
aeskey:
# MyBatis
mybatis:
typeAliasesPackage: com.modou.**.domain
mapperLocations: classpath:/com/modoumapper
@Configuration
@ConditionalOnClass({ EnableTransactionManagement.class, EntityManager.class })
@AutoConfigureAfter({ DatabaseConfiguration.class })
@MapperScan(basePackages={"com.modou.**.mapper","com.github.abel533.entity.mapper"})
public class MybatisConfiguration implements EnvironmentAware{
private static Log logger = LogFactory.getLog(MybatisConfiguration.class);
private RelaxedPropertyResolver propertyResolver;
@Resource(name="writeDataSource")
private DataSource writeDataSource;
@Resource(name="readDataSources")
private List
Application.java
package com.modou.weixin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.modou.weixin.service.HelloWorldService;
@Configuration
@ComponentScan(basePackages={"com.modou.conf","com.modou.weixin"})
@EnableAutoConfiguration
public class Application implements CommandLineRunner{
@Autowired
HelloWorldService helloWorldService;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(this.helloWorldService.print());
}
}
3、maven pom.xml 相关依赖[我是基于我的多模块依赖,这里只是一个示意],其中配置了jrebel热部署插件,需要搭配jrebel6.2.1,具体配置和下载请转向 http://blog.csdn.net/xiaoyu411502/article/details/48047369
4.0.0 weixin-boot-services com.modou.weixin weixin-boot-parent0.0.1-SNAPSHOT ../weixin-boot-parent weixin-boot-services http://maven.apache.org UTF-8 1.2.4.RELEASE com.modou.weixin weixin-boot-sdk${project.version} com.modou.weixin mybatis-plugin-rw${project.version} org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatororg.springframework.boot spring-boot-starter-thymeleaforg.springframework spring-jdbcjavax.persistence persistence-apiorg.mybatis mybatisorg.mybatis mybatis-springcom.alibaba druidmysql mysql-connector-javacom.github.pagehelper pagehelpertk.mybatis mapperorg.mybatis.generator mybatis-generator-core
以上所述是小编给大家介绍的Spring boot整合Mybatis的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!



