欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解springboot+mybatis多数据源最简解决方案

程序员文章站 2024-02-26 13:52:28
说起多数据源,一般都来解决那些问题呢,主从模式或者业务比较复杂需要连接不同的分库来支持业务。我们项目是后者的模式,网上找了很多,大都是根据jpa来做多数据源解决方案,要不就...

说起多数据源,一般都来解决那些问题呢,主从模式或者业务比较复杂需要连接不同的分库来支持业务。我们项目是后者的模式,网上找了很多,大都是根据jpa来做多数据源解决方案,要不就是老的spring多数据源解决方案,还有的是利用aop动态切换,感觉有点小复杂,其实我只是想找一个简单的多数据支持而已,折腾了两个小时整理出来,供大家参考。

废话不多说直接上代码吧

配置文件

pom包就不贴了比较简单该依赖的就依赖,主要是数据库这边的配置:

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

一个test1库和一个test2库,其中test1位主库,在使用的过程中必须制定主库,不然会报错。

数据源配置

@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);
  }

}

最关键的地方就是这块了,一层一层注入,先创建datasource,在创建sqlsessionfactory在创建事务,最后包装到sqlsessiontemplate中。其中需要制定分库的mapper文件地址,以及分库到层代码

复制代码 代码如下:

@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

public interface user1mapper {

  list<userentity> getall();

  userentity getone(long id);

  void insert(userentity user);

  void update(userentity user);

  void delete(long id);

}

xml层

<?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层的使用

@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);
  }

}

最后源码地址在这里

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。