91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用Spring boot怎么對Mybatis進行整合

發布時間:2020-11-26 16:00:16 來源:億速云 閱讀:118 作者:Leah 欄目:編程語言

這篇文章將為大家詳細講解有關使用Spring boot怎么對Mybatis進行整合,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

1、文件結構

DataBaseConfiguration.Java用來獲取數據庫連接配置信息,配置從application.properties中讀取

MybatisConfiguration.java也就是MyBatis配置核心入口,構建連接創建SqlSessionFactory

使用Spring boot怎么對Mybatis進行整合

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 List<DataSource> readDataSources(){ 
  List<DataSource> dataSources = new ArrayList<DataSource>(); 
  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<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; 
/** 
 * 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

<?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進行整合就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

瑞昌市| 正阳县| 黑水县| 咸宁市| 呈贡县| 马山县| 连城县| 太原市| 东方市| 延津县| 盖州市| 德州市| 甘德县| 太湖县| 临高县| 前郭尔| 湖口县| 郓城县| 平武县| 织金县| 乌鲁木齐县| 德化县| 泌阳县| 寿光市| 中山市| 深圳市| 虹口区| 莆田市| 界首市| 武强县| 宜兰县| 永德县| 余姚市| 天津市| 洱源县| 安溪县| 印江| 陆丰市| 绥阳县| 扬中市| 堆龙德庆县|