spring boot(七):springboot+mybatis多数据源最简解决方案
来源:互联网 发布:点位图软件 all 编辑:程序博客网 时间:2024/06/08 13:22
说起多数据源,一般都来解决那些问题呢,主从模式或者业务比较复杂需要连接不同的分库来支持业务。我们项目是后者的模式,网上找了很多,大都是根据jpa来做多数据源解决方案,要不就是老的spring多数据源解决方案,还有的是利用aop动态切换,感觉有点小复杂,其实我只是想找一个简单的多数据支持而已,折腾了两个小时整理出来,供大家参考。
废话不多说直接上代码吧
配置文件
pom包就不贴了比较简单该依赖的就依赖,主要是数据库这边的配置:
mybatis.config-locations=classpath:mybatis/mybatis-config.xmlspring.datasource.test1.driverClassName = com.mysql.jdbc.Driverspring.datasource.test1.url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8spring.datasource.test1.username = rootspring.datasource.test1.password = rootspring.datasource.test2.driverClassName = com.mysql.jdbc.Driverspring.datasource.test2.url = jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8spring.datasource.test2.username = rootspring.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层的使用
@RestControllerpublic 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
作者:纯洁的微笑
出处:http://www.ityouknow.com/
版权所有,欢迎保留原文链接进行转载:)
0 0
- spring boot(七):springboot+mybatis多数据源最简解决方案
- spring boot(七):springboot+mybatis多数据源最简解决方案
- spring boot(七):springboot+mybatis多数据源最简解决方案
- Spring Boot系列(七):springboot+mybatis多数据源最简解决方案
- spring boot(七):springboot+mybatis多数据源最简解决方案
- spring boot(七):springboot+mybatis多数据源最简解决方案
- 转载:Spring Boot (七):springboot+mybatis多数据源最简解决方案
- springboot(七):springboot+mybatis多数据源最简解决方案
- SpringBoot ( 七 ) :springboot + mybatis 多数据源最简解决方案
- springboot(七):springboot+mybatis多数据源最简解决方案
- springboot(七):springboot+mybatis多数据源最简解决方案
- springboot(七):springboot+mybatis多数据源最简解决方案
- spring-boot+mybatis多数据源最简解决方案
- springboot + mybatis 多数据源最简解决方案
- SpringBoot开发详解(七)-- Mybatis整合Spring Boot
- spring-boot研究三:springboot+mybatis+mysql多数据源配置
- spring(spring-boot) + mybatis多数据源
- SpringBoot:spring boot集成mybatis
- daemon守护进程初识
- sql查询优化
- 命令行切换到目标文件夹、目标文件
- 常用的八种排序算法与Java代码实现
- 排序二叉树的建立注意重复元素
- spring boot(七):springboot+mybatis多数据源最简解决方案
- Android:利用SmsManager发送短信
- Trafodion 临时表
- 回顾曾经写过的c语言小代码(一)
- openstack创建instance时,如何生成对应instance的镜像
- centos 6.5下 vsftp 虚拟用户配置
- 系统级虚拟机
- MapReduce 的一些算法思路
- ubuntu MySQL5.6修改字符集编码为utf8