这篇文章将为大家详细讲解有关使用Spring boot怎么对Mybatis进行整合,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
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<Object> readDataSources; @Override public void setEnvironment(Environment environment) { this.propertyResolver = new RelaxedPropertyResolver(environment,"mybatis."); } @Bean @ConditionalOnMissingBean public SqlSessionFactory sqlSessionFactory() { try { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(roundRobinDataSouceProxy()); sessionFactory.setTypeAliasesPackage(propertyResolver .getProperty("typeAliasesPackage")); sessionFactory .setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(propertyResolver .getProperty("mapperLocations"))); sessionFactory .setConfigLocation(new DefaultResourceLoader() .getResource(propertyResolver .getProperty("configLocation"))); return sessionFactory.getObject(); } catch (Exception e) { logger.warn("Could not confiure mybatis session factory"); return null; } } @Bean public RoundRobinRWRoutingDataSourceProxy roundRobinDataSouceProxy(){ RoundRobinRWRoutingDataSourceProxy proxy = new RoundRobinRWRoutingDataSourceProxy(); proxy.setWriteDataSource(writeDataSource); proxy.setReadDataSoures(readDataSources); proxy.setReadKey("READ"); proxy.setWriteKey("WRITE"); return proxy; } @Bean @ConditionalOnMissingBean public DataSourceTransactionManager transactionManager() { return new DataSourceTransactionManager(writeDataSource); } }
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()); } }
maven pom.xml 相关依赖[我是基于我的多模块依赖,这里只是一个示意],其中配置了jrebel热部署插件,需要搭配jrebel6.2.1,具体配置和下载请转向 http://blog.csdn.net/xiaoyu411502/article/details/48047369
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.modou.weixin</groupId> <artifactId>weixin-boot-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../weixin-boot-parent</relativePath> </parent> <artifactId>weixin-boot-services</artifactId> <name>weixin-boot-services</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <springloaded.version>1.2.4.RELEASE</springloaded.version> </properties> <dependencies> <dependency> <groupId>com.modou.weixin</groupId> <artifactId>weixin-boot-sdk</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>com.modou.weixin</groupId> <artifactId>mybatis-plugin-rw</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> </dependency> </dependencies> </project>
关于使用Spring boot怎么对Mybatis进行整合就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。