springboot+mybatis多数据源最简解决方案
来源:才华咖 本文已影响1.56W人
来源:才华咖 本文已影响1.56W人
说起多数据源,一般都来解决那些问题呢,主从模式或者业务比较复杂需要连接不同的分库来支持业务。本文是本站小编搜索整理的关于springboot+mybatis多数据源最简解决方案,供参考学习,希望对大家有所帮助!想了解更多相关信息请持续关注我们应届毕业生考试网!
配置文件
pom包就不贴了比较简单该依赖的就依赖,主要是数据库这边的'配置:
ig-locations=classpath:mybatis/
erClassName = er
= jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8
name = root
word = root
erClassName = er
= jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8
name = root
word = root
一个test1库和一个test2库,其中test1位主库,在使用的过程中必须制定主库,不然会报错。
数据源配置
@Configuration
@MapperScan(basePackages = "1", sqlSessionTemplateRef = "test1SqlSessionTemplate")
public class DataSource1Config {
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "1")
@Primary
public DataSource testDataSource() {
return te()d();
}
@Bean(name = "test1SqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
ataSource(dataSource);
apperLocations(new PathMatchingResourcePatternResolver()esources("classpath:mybatis/mapper/test1/*"));
return bject();
}
@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 = "1", sqlSessionTemplateRef = "test1SqlSessionTemplate")
这块的注解就是指明了扫描dao层,并且给dao层注入指定的SqlSessionTemplate。所有@Bean都需要按照命名指定正确。
dao层和xml层
dao层和xml需要按照库来分在不同的目录,比如:test1库dao层在1包下,test2库在1
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 "-///DTD Mapper 3.0//EN" "" >
<mapper namespace="1Mapper" >
<resultMap id="BaseResultMap" type="Entity" >
<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="SexEnum"/>
<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="" resultMap="BaseResultMap" >
SELECT
<include refid="Base_Column_List" />
FROM users
WHERE id = #{id}
</select>
<insert id="insert" parameterType="Entity" >
INSERT INTO
users
(userName,passWord,user_sex)
VALUES
(#{userName}, #{passWord}, #{userSex})
</insert>
<update id="update" parameterType="Entity" >
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="" >
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=ll();
return users;
}
@RequestMapping("/getUser")
public UserEntity getUser(Long id) {
UserEntity user=ne(id);
return user;
}
@RequestMapping("/add")
public void save(UserEntity user) {
rt(user);
}
@RequestMapping(value="update")
public void update(UserEntity user) {
te(user);
}
@RequestMapping(value="/delete/{id}")
public void delete(@PathVariable("id") Long id) {
te(id);
}
}
最后源码地址在这里spring-boot-mybatis-mulidatasource
Java中的装箱和拆箱深入理解
My hobby is reading books英语作文带翻译范文
电脑开机出现bootmgr is missing怎么解决
Thinkpad笔记本开机显示Tointerrruptnormalstartup解决方法
开机提示“bootmgr is missing”的解决方法和步骤
英语阅读:Web Boosts Grandpas Brain
Scientists Build Robots to Live With Humans英语美文
Spring+MyBatis数据读写分离的实例详解
有关JavaScript中的prototype.bind()方法介绍
Is It Good to Being A Postgraduate大学英语作文
最新《Fate/Stay Night Unlimited Blade Works》经典名句
初中My opinion about the stars优秀英语作文
ASandpiperToBringYouJoy英语阅读
The Opinion of Buying Cars英语作文
whats your plan for dragon boat festival英语作文
Remarks by President Bush at CEO Summit
英语作文Build a Harmonious Society
Global citizenship begins at home演讲稿
Happy Dragon Boat Festival英语作文
数据中心解决方案
A Mixed Blessing for the City英语四六级作文
英语教材分析:composition,analysis (unit 2)
Spring Boot使用slf4j+logback记录日志配置详解
Unit 10《It Is My Bedroom》优秀教案范文
我的英语演讲稿living at present is my top concern
测试数据管理解决方案建议书
Unit 13 Lesson50 Standing Room Only说课稿
剑桥大学University of Cambridge简介
英语口语:Teaching and Learning Responsibility
iOS8.3越狱后修复Cydia Substrate不兼容问题解决方法
Book5-Unit1Greatssientists的重点单词、词组整理
英语阅读:Dollar Hit by Trump Tax Plan Doubts
思科数据中心Datacenter解决方案
Module 7 Travel Unit 2 We are going to visit the Ming Tombs
数字决定成败—让数据充实你的简历
Global Citizenship Begins At Home英语作文
Unit 5 It must belong to Caral section A英语评课稿
2016职称英语阅读理解范文:TV Shows and Long Bus Trips
英文作文:My Military Training Instructor 我的教官
MySQL安装时出现current root password的解决方法
托福口语真题解析: About spare time and money