SpringBoot+MyBatis+MySQL读写分离实现( 四 )

selectAll() {return memberMapper.selectByExample(new MemberExample());}@Master@Overridepublic String getToken(String appId) {//有些读操作必须读主数据库//比如 , 获取微信access_token , 因为高峰时期主从同步可能延迟//这种情况下就必须强制从主数据读return null;}}4. 测试
package com.cjs.example;import com.cjs.example.entity.Member;import com.cjs.example.service.MemberService;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class CjsDatasourceDemoApplicationTests {@Autowiredprivate MemberService memberService;@Testpublic void testWrite() {Member member = new Member();member.setName("zhangsan");memberService.insert(member);}@Testpublic void testRead() {for (int i = 0; i < 4; i++) {memberService.selectAll();}}@Testpublic void testSave() {Member member = new Member();member.setName("wangwu");memberService.save(member);}@Testpublic void testReadFromMaster() {memberService.getToken("1234");}}查看控制台
SpringBoot+MyBatis+MySQL读写分离实现文章插图
SpringBoot+MyBatis+MySQL读写分离实现文章插图
SpringBoot+MyBatis+MySQL读写分离实现文章插图
SpringBoot+MyBatis+MySQL读写分离实现文章插图
5. 工程结构
SpringBoot+MyBatis+MySQL读写分离实现文章插图
6. 参考
【SpringBoot+MyBatis+MySQL读写分离实现】