springboot+mybatis多數據源最簡解決方案
説起多數據源,一般都來解決那些問題呢,主從模式或者業務比較複雜需要連接不同的分庫來支持業務。本文是本站小編搜索整理的關於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中的裝箱和拆箱深入理解
-
java xp環境變量設置
本文實例為大家分享了java的環境變量設置方法,供大家參考,具體內容如下安裝好jdk後找到我的電腦(右鍵)——》屬性——》高級選項卡——》環境變量,裏面有管理員的用户變量,有系統變量。選哪一個都無所謂,我選的是Administrator的用户變量即管理員的.用户變量,點擊“新...
-
在Java裏處理文件的技巧
導讀:看到太多的凌亂的,不安全的處理文件的代碼了。可以説每個項目都會有人喜歡寫自己的一些FileUitl。下面是小編為大家整理的在Java裏處理文件的技巧,歡迎參考~實用的工具類,Path,Paths,Files,FileSystem有一些很靈活的處理方法://得到一個Path對象Pathpath=("/test/...
-
Java開發崗位面試題
為幫助參加Java開發崗位面試的朋友們做好準備,以下是本站小編搜索整理的一份Java集合面試問題40個【附答案】,供參考練習,希望對大家有所幫助!想了解更多相關信息請持續關注我們應屆畢業生考試網!一、Java基礎1、String類為什麼是final的。2、HashMap的源碼,實現原...
-
2017計算機二級考試Java練習題及答案
以下是應屆畢業生考試網小編整理的2017年計算機二級考試Java練習題及答案,供考生參考。更多計算機等級考試相關信息請關注應屆畢業生計算機等級考試網。1[單選題]下列方法被調用後,一定使調用線程改變當前狀態的是()。fy()d()p()ive()參考答案:C參考解析:線程調用s...
相關文章
- Passage Eighteen (Strictly Ban smoking)英語閲讀理解
- Why Public Speaking Is So Important 公共演説的重要性大學英語
- whats your plan for dragon boat festival英語作文
- 電腦開機出現bootmgr is missing怎麼解決
- 開機提示“bootmgr is missing”的解決方法和步驟
- 提示operating system not found的解決方法
- 國中My opinion about the stars優秀英語作文
- Spring+MyBatis數據讀寫分離的實例詳解
- The Opinion of Buying Cars英語作文
- Time To Stop Worrying About Invasive Species美文欣賞