一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - Java教程 - 詳解springboot+mybatis多數(shù)據(jù)源最簡解決方案

詳解springboot+mybatis多數(shù)據(jù)源最簡解決方案

2020-09-24 15:53純潔的蟲子 Java教程

本篇文章主要介紹了詳解springboot+mybatis多數(shù)據(jù)源最簡解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

說起多數(shù)據(jù)源,一般都來解決那些問題呢,主從模式或者業(yè)務(wù)比較復(fù)雜需要連接不同的分庫來支持業(yè)務(wù)。我們項(xiàng)目是后者的模式,網(wǎng)上找了很多,大都是根據(jù)jpa來做多數(shù)據(jù)源解決方案,要不就是老的spring多數(shù)據(jù)源解決方案,還有的是利用aop動態(tài)切換,感覺有點(diǎn)小復(fù)雜,其實(shí)我只是想找一個(gè)簡單的多數(shù)據(jù)支持而已,折騰了兩個(gè)小時(shí)整理出來,供大家參考。

廢話不多說直接上代碼吧

配置文件

pom包就不貼了比較簡單該依賴的就依賴,主要是數(shù)據(jù)庫這邊的配置:

?
1
2
3
4
5
6
7
8
9
10
11
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
 
spring.datasource.test1.driverClassName = com.mysql.jdbc.Driver
spring.datasource.test1.url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8
spring.datasource.test1.username = root
spring.datasource.test1.password = root
 
spring.datasource.test2.driverClassName = com.mysql.jdbc.Driver
spring.datasource.test2.url = jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8
spring.datasource.test2.username = root
spring.datasource.test2.password = root

一個(gè)test1庫和一個(gè)test2庫,其中test1位主庫,在使用的過程中必須制定主庫,不然會報(bào)錯。

數(shù)據(jù)源配置

?
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
@Configuration
@MapperScan(basePackages = "com.neo.mapper.test1", sqlSessionTemplateRef = "test1SqlSessionTemplate")
public class DataSource1Config {
 
  @Bean(name = "test1DataSource")
  @ConfigurationProperties(prefix = "spring.datasource.test1")
  @Primary
  public DataSource testDataSource() {
    return DataSourceBuilder.create().build();
  }
 
  @Bean(name = "test1SqlSessionFactory")
  @Primary
  public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
    return bean.getObject();
  }
 
  @Bean(name = "test1TransactionManager")
  @Primary
  public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
  }
 
  @Bean(name = "test1SqlSessionTemplate")
  @Primary
  public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
  }
 
}

最關(guān)鍵的地方就是這塊了,一層一層注入,先創(chuàng)建DataSource,在創(chuàng)建SqlSessionFactory在創(chuàng)建事務(wù),最后包裝到SqlSessionTemplate中。其中需要制定分庫的mapper文件地址,以及分庫到層代碼

 

復(fù)制代碼 代碼如下:

@MapperScan(basePackages = "com.neo.mapper.test1", sqlSessionTemplateRef  = "test1SqlSessionTemplate")

 

這塊的注解就是指明了掃描dao層,并且給dao層注入指定的SqlSessionTemplate。所有@Bean都需要按照命名指定正確。

dao層和xml層

dao層和xml需要按照庫來分在不同的目錄,比如:test1庫dao層在com.neo.mapper.test1包下,test2庫在com.neo.mapper.test1

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public interface User1Mapper {
 
  List<UserEntity> getAll();
 
  UserEntity getOne(Long id);
 
  void insert(UserEntity user);
 
  void update(UserEntity user);
 
  void delete(Long id);
 
}

xml層

?
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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.neo.mapper.test1.User1Mapper" >
  <resultMap id="BaseResultMap" type="com.neo.entity.UserEntity" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="userName" property="userName" jdbcType="VARCHAR" />
    <result column="passWord" property="passWord" jdbcType="VARCHAR" />
    <result column="user_sex" property="userSex" javaType="com.neo.enums.UserSexEnum"/>
    <result column="nick_name" property="nickName" jdbcType="VARCHAR" />
  </resultMap>
 
  <sql id="Base_Column_List" >
    id, userName, passWord, user_sex, nick_name
  </sql>
 
  <select id="getAll" resultMap="BaseResultMap" >
    SELECT
    <include refid="Base_Column_List" />
    FROM users
  </select>
 
  <select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" >
    SELECT
    <include refid="Base_Column_List" />
    FROM users
    WHERE id = #{id}
  </select>
 
  <insert id="insert" parameterType="com.neo.entity.UserEntity" >
    INSERT INTO
      users
      (userName,passWord,user_sex)
    VALUES
      (#{userName}, #{passWord}, #{userSex})
  </insert>
 
  <update id="update" parameterType="com.neo.entity.UserEntity" >
    UPDATE
      users
    SET
    <if test="userName != null">userName = #{userName},</if>
    <if test="passWord != null">passWord = #{passWord},</if>
    nick_name = #{nickName}
    WHERE
      id = #{id}
  </update>
 
  <delete id="delete" parameterType="java.lang.Long" >
    DELETE FROM
       users
    WHERE
       id =#{id}
  </delete>
 
</mapper>

測試

測試可以使用SpringBootTest,也可以放到Controller中,這里只貼Controller層的使用

?
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
@RestController
public class UserController {
 
  @Autowired
  private User1Mapper user1Mapper;
 
  @Autowired
  private User2Mapper user2Mapper;
 
  @RequestMapping("/getUsers")
  public List<UserEntity> getUsers() {
    List<UserEntity> users=user1Mapper.getAll();
    return users;
  }
 
  @RequestMapping("/getUser")
  public UserEntity getUser(Long id) {
    UserEntity user=user2Mapper.getOne(id);
    return user;
  }
 
  @RequestMapping("/add")
  public void save(UserEntity user) {
    user2Mapper.insert(user);
  }
 
  @RequestMapping(value="update")
  public void update(UserEntity user) {
    user2Mapper.update(user);
  }
 
  @RequestMapping(value="/delete/{id}")
  public void delete(@PathVariable("id") Long id) {
    user1Mapper.delete(id);
  }
 
}

最后源碼地址在這里spring-boot-mybatis-mulidatasource

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/ityouknow/article/details/70153631

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美激情综合 | 日本护士撒尿 | 小寡妇好紧进去了好大看视频 | 高清不卡一区二区 | bt伙计最新合集 | 精品日韩欧美一区二区三区 | 日本在线视频免费观看 | 日本不卡在线一区二区三区视频 | 国产精品成人亚洲 | 99草精品视频| 国产ay| 国产精品www视频免费看 | 香港日本三级亚洲三级 | 精品久久久久香蕉网 | 国产精品国语自产拍在线观看 | 日本-区二区三区免费精品 日本破处 | 国产99久久精品 | 免费视频专区一国产盗摄 | 国内精品久久久久久不卡影院 | 成人欧美一区二区三区白人 | 欧美日韩国产成人精品 | 国产欧美成人不卡视频 | 亚洲精品久久麻豆蜜桃 | 国产嘿咻 | 欧美日韩成人在线 | 91成人爽a毛片一区二区 | 91精品啪在线观看国产91九色 | 干操网| 99在线观看视频免费 | 污小说h| 日本激情在线 | 精品国产综合区久久久久久 | 艾秋麻豆果冻传媒老狼仙踪林 | a黄色| 动漫美女3d被爆漫画 | 亚洲精品一区二区久久久久 | 欧美高清在线精品一区 | 日韩欧美国产一区 | 狠狠干奇米 | 国产91精品在线观看 | 5566中文字幕亚洲精品 |