使用Springboot怎么对Mybatis进行整合
这篇文章将为大家详细讲解有关使用Spring boot怎么对Mybatis进行整合,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
创新互联于2013年成立,公司以网站设计、成都网站设计、系统开发、网络推广、文化传媒、企业宣传、平面广告设计等为主要业务,适用行业近百种。服务企业客户1000多家,涉及国内多个省份客户。拥有多年网站建设开发经验。为企业提供专业的网站建设、创意设计、宣传推广等服务。 通过专业的设计、独特的风格,为不同客户提供各种风格的特色服务。
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/modou/**/mapper/*.xml configLocation: classpath:/mybatis-config.xml # LOGGING logging: level: com.ibatis:DEBUG
DataBaseConfiguration.java
package com.modou.conf.mybatis; import java.util.ArrayList; import java.util.List; import javax.sql.DataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.env.Environment; import org.springframework.transaction.annotation.EnableTransactionManagement; import com.alibaba.druid.pool.DruidDataSource; @Configuration @EnableTransactionManagement public class DataBaseConfiguration implements EnvironmentAware { private RelaxedPropertyResolver propertyResolver; private static Logger log = LoggerFactory.getLogger(DataBaseConfiguration.class); @Override public void setEnvironment(Environment env) { this.propertyResolver = new RelaxedPropertyResolver(env, "jdbc."); } @Bean(name="writeDataSource", destroyMethod = "close", initMethod="init") @Primary public DataSource writeDataSource() { log.debug("Configruing Write DataSource"); DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(propertyResolver.getProperty("url")); datasource.setDriverClassName(propertyResolver.getProperty("driverClassName")); datasource.setUsername(propertyResolver.getProperty("username")); datasource.setPassword(propertyResolver.getProperty("password")); return datasource; } @Bean(name="readOneDataSource", destroyMethod = "close", initMethod="init") public DataSource readOneDataSource() { log.debug("Configruing Read One DataSource"); DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(propertyResolver.getProperty("url")); datasource.setDriverClassName(propertyResolver.getProperty("driverClassName")); datasource.setUsername(propertyResolver.getProperty("username")); datasource.setPassword(propertyResolver.getProperty("password")); return datasource; } @Bean(name="readTowDataSource", destroyMethod = "close", initMethod="init") public DataSource readTowDataSource() { log.debug("Configruing Read Two DataSource"); DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(propertyResolver.getProperty("url")); datasource.setDriverClassName(propertyResolver.getProperty("driverClassName")); datasource.setUsername(propertyResolver.getProperty("username")); datasource.setPassword(propertyResolver.getProperty("password")); return datasource; } @Bean(name="readDataSources") public ListreadDataSources(){ List dataSources = new ArrayList (); dataSources.add(readOneDataSource()); dataSources.add(readTowDataSource()); return dataSources; } }
MyBatisConfiguration.java
package com.modou.conf.mybatis; import java.util.List; import javax.annotation.Resource; import javax.persistence.EntityManager; import javax.sql.DataSource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.plugin.rw.RoundRobinRWRoutingDataSourceProxy; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * * 获取第二个数据库的连接信息,在application.yml中配置,并指定特定的前缀 * */ @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; /** * Created by chababa on 15/8/22. */ @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 com.modou.weixin weixin-boot-parent 0.0.1-SNAPSHOT ../weixin-boot-parent weixin-boot-services 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-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-thymeleaf org.springframework spring-jdbc javax.persistence persistence-api org.mybatis mybatis org.mybatis mybatis-spring com.alibaba druid mysql mysql-connector-java com.github.pagehelper pagehelper tk.mybatis mapper org.mybatis.generator mybatis-generator-core
关于使用Spring boot怎么对Mybatis进行整合就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
网站标题:使用Springboot怎么对Mybatis进行整合
分享路径:http://hbruida.cn/article/ipespo.html