SpringBoot實(shí)現(xiàn)多數(shù)據(jù)源配置的示例詳解
配置文件
Pom 包就不貼了比較簡(jiǎn)單該依賴的就依賴,主要是數(shù)據(jù)庫這邊的配置:
mybatis.config-location=classpath:mybatis/mybatis-config.xml spring.datasource.test1.jdbc-url=jdbc:mysql://localhost:3306/test1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true spring.datasource.test1.username=root spring.datasource.test1.password=root spring.datasource.test1.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.test2.jdbc-url=jdbc:mysql://localhost:3306/test2?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true spring.datasource.test2.username=root spring.datasource.test2.password=root spring.datasource.test2.driver-class-name=com.mysql.cj.jdbc.Driver
一個(gè) test1 庫和一個(gè) test2 庫,其中 test1 位主庫,在使用的過程中必須指定主庫,不然會(huì)報(bào)錯(cuò)。
數(shù)據(jù)源配置
@Configuration @MapperScan(basePackages = "com.test.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 文件地址,以及分庫dao層代碼。
@MapperScan(basePackages = "com.test.mapper.test1", sqlSessionTemplateRef = "test1SqlSessionTemplate")
這塊的注解就是指明了掃描 dao 層,并且給 dao 層注入指定的 SqlSessionTemplate。所有@Bean都需要按照命名指定正確。
dao 層和 xml層
dao 層和 xml 需要按照庫來分在不同的目錄,比如:test1 庫 dao 層在com.test.mapper.test1包下,test2 庫在com.test.mapper.test2
public interface User1Mapper { List<UserEntity> getAll(); UserEntity getOne(Long id); void insert(UserEntity user); void update(UserEntity user); void delete(Long id); }
xml 層
<mapper namespace="com.test.mapper.test1.User1Mapper" > <resultMap id="BaseResultMap" type="com.test.model.User" > <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.test.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.test.model.User" > INSERT INTO users (userName,passWord,user_sex) VALUES (#{userName}, #{passWord}, #{userSex}) </insert> <update id="update" parameterType="com.test.model.User" > 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>
測(cè)試
測(cè)試可以使用 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); } }
Mybatis 注解版本配置多數(shù)據(jù)源和 Xml 版本基本一致。
以上就是SpringBoot實(shí)現(xiàn)多數(shù)據(jù)源配置的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot多數(shù)據(jù)源配置的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
聊聊java 過濾器、監(jiān)聽器、攔截器的區(qū)別(終結(jié)篇)
這篇文章主要介紹了聊聊java 過濾器、監(jiān)聽器、攔截器的區(qū)別(終結(jié)篇),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02解決Spring Cloud Gateway獲取body內(nèi)容,不影響GET請(qǐng)求的操作
這篇文章主要介紹了解決Spring Cloud Gateway獲取body內(nèi)容,不影響GET請(qǐng)求的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12springboot使用jasypt加密庫實(shí)現(xiàn)數(shù)據(jù)庫加解密示例代碼
這篇文章主要給大家介紹了關(guān)于springboot使用jasypt加密庫實(shí)現(xiàn)數(shù)據(jù)庫加解密的相關(guān)資料,Jasypt是一個(gè)用于配置文件加密的Java庫,它可以用來加密和解密配置文件中的敏感信息,如數(shù)據(jù)庫密碼、API?密鑰等,需要的朋友可以參考下2024-04-04jvm垃圾回收GC調(diào)優(yōu)基礎(chǔ)原理分析
談到調(diào)優(yōu),這一定是針對(duì)特定場(chǎng)景、特定目的的事情, 對(duì)于 GC 調(diào)優(yōu)來說,首先就需要清楚調(diào)優(yōu)的目標(biāo)是什么?從性能的角度看,通常關(guān)注三個(gè)方面,內(nèi)存占用(footprint)、延時(shí)(latency)和吞吐量(throughput)2022-01-01劍指Offer之Java算法習(xí)題精講鏈表與數(shù)組專項(xiàng)訓(xùn)練
跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化2022-03-03Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(24)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07