国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring boot怎么整合Mybatis

Spring boot怎么整合Mybatis

2020-11-30 15:16茶爸爸 Java教程

spring boot的簡配置方便的開發,下面通過本文給大家分享Spring boot整合Mybatis的方法,需要的朋友參考下

 最近剛接觸spring boot,正是因為他的及簡配置方便開發,促使我下定決心要用它把之前寫的項目重構,那么問題來了,spring boot怎么整合mybatis呢,下面幾個配置類來搞定。

在我的代碼當中是實現了數據庫讀寫分離的,所以代碼僅做參考,如有需要可以加我微信:benyzhous

【后續更新】

1、文件結構

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

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

Spring boot怎么整合Mybatis

2、下面直接貼代碼,有問題的話可以留言或者加我的微信公眾號:cha-baba,或者個人微信號:benyzhous

application.yml 相關配置

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?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的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:http://blog.csdn.net/xiaoyu411502/article/details/48164311

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美日韩精品久久久 | 亚洲成人精品在线观看 | 啪啪伊人 | 亚洲va欧美va人人爽成人影院 | 久久伊人网视频 | 美日韩av | 黄色国产在线视频 | 国产在线a | 午夜影院在线观看 | 亚洲成人av在线播放 | 91免费国产在线 | 国产精品久久久久久中文字 | 欧美精品一二三区 | 综合久久99 | 久久免费精品 | 天天操夜夜操av | 中文字幕免费视频 | 波多野结衣一二三四区 | 在线观看一区二区三区四区 | 一区二区三区高清不卡 | 亚洲成人一二三 | 日韩视频一区 | 久久白虎 | 国产一区不卡 | 在线一区二区免费 | 国产剧情一区二区 | 日韩在线小视频 | 中文字幕在线免费观看 | 亚洲免费婷婷 | 国产农村妇女精品久久 | аⅴ资源新版在线天堂 | 免费视频成人国产精品网站 | 99精品视频在线观看 | 成年人黄色免费视频 | 国产一级片儿 | 日韩中文字幕一区二区 | 黄色一级网站 | 免费av一区二区三区 | 99热最新网址 | 久草国产视频 | 欧美激情精品久久久久久变态 |