SpringCloud集成MybatisPlus實現(xiàn)MySQL多數(shù)據(jù)源配置方法
引入依賴
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.15</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
配置多數(shù)據(jù)源
在application.properties中配置多數(shù)據(jù)源:
spring.datasource.master.driver-class-name=com.mysql.jdbc.Driver spring.datasource.master.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC spring.datasource.master.username=root spring.datasource.master.password=root spring.datasource.slave.driver-class-name=com.mysql.jdbc.Driver spring.datasource.slave.url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC spring.datasource.slave.username=root spring.datasource.slave.password=root spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.pool.init-size=10 spring.datasource.pool.max-size=20 spring.datasource.pool.min-size=5 spring.datasource.pool.max-wait=30000 spring.datasource.filters=stat,wall,log4j spring.datasource.log-enabled=true spring.datasource.log-prefix=druid.log spring.datasource.stat-view-servlet.enabled=true spring.datasource.stat-view-servlet.url-pattern=/druid/* spring.datasource.web-stat-filter.enabled=true spring.datasource.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/* spring.datasource.max-total=20 spring.datasource.max-idle=10 spring.datasource.min-idle=5 spring.datasource.time-between-eviction-runs-millis=60000 spring.datasource.min-evictable-idle-time-millis=300000 spring.datasource.validation-query=SELECT 1 FROM dual spring.datasource.test-on-borrow=true spring.datasource.test-on-return=true spring.datasource.test-while-idle=true spring.datasource.remove-abandoned=true spring.datasource.remove-abandoned-timeout=60000 spring.datasource.log-abandoned=true
配置解釋
這是一個Spring Boot應用程序中用于配置數(shù)據(jù)庫連接的屬性文件。以下是每個配置項目的解釋:
- spring.datasource.master.driver-class-name: 指定主數(shù)據(jù)庫的JDBC驅(qū)動程序類名,這里是MySQL數(shù)據(jù)庫的驅(qū)動類。
- spring.datasource.master.url: 主數(shù)據(jù)庫的JDBC URL,指定了數(shù)據(jù)庫的位置和連接參數(shù),包括字符編碼、時區(qū)等。
- spring.datasource.master.username: 主數(shù)據(jù)庫的用戶名。
- spring.datasource.master.password: 主數(shù)據(jù)庫的密碼。
- spring.datasource.slave.driver-class-name: 指定從數(shù)據(jù)庫(副本)的JDBC驅(qū)動程序類名,同樣是MySQL數(shù)據(jù)庫的驅(qū)動類。
- spring.datasource.slave.url: 從數(shù)據(jù)庫的JDBC URL,與主數(shù)據(jù)庫不同的地方可能包括不同的數(shù)據(jù)庫名稱或連接參數(shù)。
- spring.datasource.slave.username: 從數(shù)據(jù)庫的用戶名。
- spring.datasource.slave.password: 從數(shù)據(jù)庫的密碼。
- spring.datasource.type: 數(shù)據(jù)源類型,這里使用了阿里巴巴的Druid數(shù)據(jù)源。
- spring.datasource.pool.init-size: 數(shù)據(jù)源的初始連接池大小,表示在啟動時會創(chuàng)建的數(shù)據(jù)庫連接數(shù)。
- spring.datasource.pool.max-size: 數(shù)據(jù)源的最大連接池大小,表示連接池中允許存在的最大連接數(shù)。
- spring.datasource.pool.min-size: 數(shù)據(jù)源的最小連接池大小,表示連接池中允許存在的最小連接數(shù)。
- spring.datasource.pool.max-wait: 獲取連接時的最大等待時間(毫秒),如果連接池中的連接都被占用,且達到最大連接數(shù),新請求會等待一段時間。
- spring.datasource.filters: 數(shù)據(jù)源的過濾器,可以用于監(jiān)控、安全等目的。這里包括了統(tǒng)計(stat)、SQL防火墻(wall)和日志(log4j)。
- spring.datasource.log-enabled: 是否啟用Druid的連接池日志。
- spring.datasource.log-prefix: 連接池日志的前綴。
- spring.datasource.stat-view-servlet.enabled: 是否啟用Druid的統(tǒng)計數(shù)據(jù)查看servlet。
- spring.datasource.stat-view-servlet.url-pattern: 統(tǒng)計數(shù)據(jù)查看servlet的URL路徑。
- spring.datasource.web-stat-filter.enabled: 是否啟用Druid的Web統(tǒng)計過濾器。
- spring.datasource.web-stat-filter.exclusions: 需要排除統(tǒng)計的資源路徑,如JavaScript、圖片等。
- spring.datasource.max-total: 最大活動連接數(shù),與spring.datasource.pool.max-size相同。
- spring.datasource.max-idle: 最大空閑連接數(shù)。
- spring.datasource.min-idle: 最小空閑連接數(shù)。
- spring.datasource.time-between-eviction-runs-millis: 連接池定期檢查空閑連接的時間間隔。
- spring.datasource.min-evictable-idle-time-millis: 連接池中連接的最小空閑時間,超過此時間的連接將被回收。
- spring.datasource.validation-query: 用于驗證連接是否有效的SQL查詢。
- spring.datasource.test-on-borrow: 是否在借用連接時測試連接的有效性。
- spring.datasource.test-on-return: 是否在歸還連接時測試連接的有效性。
- spring.datasource.test-while-idle: 是否在連接空閑時測試連接的有效性。
- spring.datasource.remove-abandoned: 是否移除長時間未使用的連接。
- spring.datasource.remove-abandoned-timeout: 設置長時間未使用連接的超時時間。
- spring.datasource.log-abandoned: 是否記錄移除連接的日志。
- 這些配置項用于定義應用程序與數(shù)據(jù)庫之間的連接池、數(shù)據(jù)庫連接屬性和連接池監(jiān)控等相關設置。不同的配置項可以根據(jù)應用程序的需求進行調(diào)整。
配置MybatisPlus
在application.properties中配置MybatisPlus:
mybatis-plus.mapper-locations=classpath:mapper/*.xml mybatis-plus.global-config.id-type=auto mybatis-plus.global-config.db-config.logic-delete-value=1 mybatis-plus.global-config.db-config.logic-not-delete-value=0 mybatis-plus.global-config.base-namespace=test mybatis-plus.global-config.mapper-namespace=test.mapper
配置MybatisPlus解釋
這是一個MyBatis Plus(通常簡稱為MyBatis+或MP)的配置文件,用于配置MyBatis Plus在Spring Boot應用程序中的行為。以下是每個配置項目的解釋:
- mybatis-plus.mapper-locations=classpath:mapper/*.xml: 這個配置項指定了MyBatis Plus的Mapper XML文件的位置。在這個例子中,它告訴MyBatis Plus在類路徑下的"mapper"目錄中查找所有以".xml"結尾的文件,以作為Mapper定義文件。
- mybatis-plus.global-config.id-type=auto: 這個配置項指定了主鍵ID的生成策略。在這里,設置為"auto"表示使用數(shù)據(jù)庫自動生成的主鍵值,這通常是由數(shù)據(jù)庫管理的自增長或唯一標識符。
- mybatis-plus.global-config.db-config.logic-delete-value=1: 這個配置項指定了邏輯刪除的值。在MyBatis Plus中,邏輯刪除是一種通過標記記錄來表示刪除狀態(tài)的方式,這里設置為"1"表示已刪除。
- mybatis-plus.global-config.db-config.logic-not-delete-value=0: 這個配置項指定了邏輯未刪除的值。在MyBatis Plus中,這是指記錄未被刪除的狀態(tài),這里設置為"0"表示未刪除。
- mybatis-plus.global-config.base-namespace=test: 這個配置項指定了基礎的Mapper命名空間。這個命名空間將會被用于生成Mapper接口的全限定名,通常與包名相關聯(lián)。
- mybatis-plus.global-config.mapper-namespace=test.mapper: 這個配置項指定了Mapper接口的命名空間。在MyBatis Plus中,Mapper接口與XML文件相關聯(lián),這個配置項將會在生成的Mapper接口中設置XML文件的命名空間。
- 這些配置項用于自定義MyBatis Plus的行為,包括主鍵生成策略、邏輯刪除的值、Mapper接口命名空間等。它們允許根據(jù)應用程序的需求來配置和控制MyBatis Plus的行為。
配置Mapper
創(chuàng)建一個Mapper接口,例如UserMapper:
@Mapper public interface UserMapper extends BaseMapper<User> { }
使用多數(shù)據(jù)源
在需要使用多數(shù)據(jù)源的地方,使用@MapperScan注解指定Mapper所在包路徑:
@SpringBootApplication @EnableDiscoveryClient public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @MapperScan("com.example.demo.mapper") public class MyApp { // ... } }
CRUD示例
@Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User save(User user) { return userMapper.save(user); } @Override public User update(User user) { return userMapper.updateById(user); } @Override public User findById(Long id) { return userMapper.selectById(id); } @Override public void delete(Long id) { userMapper.deleteById(id); } }
@RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @PostMapping public User save(@RequestBody User user) { return userService.save(user); } @PutMapping("/{id}") public User update(@PathVariable Long id, @RequestBody User user) { user.setId(id); return userService.update(user); } @GetMapping("/{id}") public User findById(@PathVariable Long id) { return userService.findById(id); } @DeleteMapping("/{id}") public void delete(@PathVariable Long id) { userService.delete(id); } }
使用不同數(shù)據(jù)源
要使用不同的數(shù)據(jù)源查詢,可以在Mapper接口中使用@MapperScan注解指定需要使用的數(shù)據(jù)源,例如:
@MapperScan("com.example.demo.mapper.master") public interface UserMapperMaster extends BaseMapper<User> { } @MapperScan("com.example.demo.mapper.slave") public interface UserMapperSlave extends BaseMapper<User> { }
然后在需要使用不同數(shù)據(jù)源的地方,使用@Autowired注解注入對應的Mapper接口,例如:
@Service public class UserServiceImpl implements UserService { @Autowired private UserMapperMaster userMapperMaster; @Autowired private UserMapperSlave userMapperSlave; @Override public User save(User user) { return userMapperMaster.save(user); } @Override public User update(User user) { return userMapperMaster.updateById(user); } @Override public User findById(Long id) { return userMapperMaster.selectById(id); } @Override public void delete(Long id) { userMapperMaster.deleteById(id); } }
要在某個方法上使用不同的數(shù)據(jù)源,可以在該方法上使用@MapperScan注解指定需要使用的數(shù)據(jù)源,例如:
@Service public class UserServiceImpl implements UserService { @Autowired private UserMapperMaster userMapperMaster; @Autowired private UserMapperSlave userMapperSlave; @Override public User save(User user) { return userMapperMaster.save(user); } @Override public User update(User user) { return userMapperMaster.updateById(user); } @Override public User findById(Long id) { return userMapperMaster.selectById(id); } @Override public void delete(Long id) { userMapperMaster.deleteById(id); } @MapperScan("com.example.demo.mapper.slave") @Override public User findByIdSlave(Long id) { return userMapperSlave.selectById(id); } }
到此這篇關于SpringCloud集成MybatisPlus實現(xiàn)MySQL多數(shù)據(jù)源配置的文章就介紹到這了,更多相關SpringCloud MybatisPlus MySQL多數(shù)據(jù)源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Java實現(xiàn)多種方式的http數(shù)據(jù)抓取
本篇文章主要介紹了Java實現(xiàn)多種方式的http數(shù)據(jù)抓取,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧。2016-12-12Java 實戰(zhàn)項目之畢業(yè)設計管理系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實現(xiàn)畢業(yè)設計管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11Jmeter多臺機器并發(fā)請求實現(xiàn)壓力性能測試
這篇文章主要介紹了Jmeter多臺機器并發(fā)請求實現(xiàn)壓力性能測試,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10Java基本數(shù)據(jù)類型與封裝類型詳解(int和Integer區(qū)別)
這篇文章主要介紹了Java基本數(shù)據(jù)類型與封裝類型詳解(int和Integer區(qū)別) ,需要的朋友可以參考下2017-02-02