介紹:以前寫了一篇博客關於mysql的讀寫分離,那個須要配置多個類,現在讀寫分離升級了,咱們不須要配置任何java配置文件類就能夠,由於有人爲咱們封裝了,咱們只管添加依賴就行了。mysql
注意:若是您看的不明白,能夠去看下以前我寫的讀寫分離,會發現簡單不少,讀寫分離Mysqlgit
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>2.7.0</version> </dependency>
添加三個依賴,dynamic-datasource-spring-boot-starter 這個是數據庫讀寫分離的主要依賴,druid-spring-boot-starter 咱們使用druid數據庫鏈接池,從庫默認HikariCP數據庫鏈接池,Springboot默認數據庫鏈接池就是HikariCPspring
spring: datasource: druid: stat-view-servlet: loginUsername: admin loginPassword: 123456 dynamic: master: username: password: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/point02?characterEncoding=utf8&useSSL=false druid: initial-size: 3 max-active: 8 min-idle: 2 max-wait: -1 min-evictable-idle-time-millis: 30000 max-evictable-idle-time-millis: 30000 time-between-eviction-runs-millis: 0 validation-query: select 1 validation-query-timeout: -1 test-on-borrow: false test-on-return: false test-while-idle: true pool-prepared-statements: true max-open-prepared-statements: 100 filters: stat,wall share-prepared-statements: true slave: one: username: password: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/point02?characterEncoding=utf8&useSSL=false two: username: password: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/point02?characterEncoding=utf8&useSSL=false druid: initial-size: 2 max-active: 6 three: username: password: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/score?characterEncoding=utf8&useSSL=false druid: initial-size: 3 max-active: 10 initialization-mode: always
@DS 能夠註解在方法上和類上,同時存在方法註解優先於類上註解,強烈建議註解在service實現或mapper接口方法上。sql
註解 | 結果 |
---|---|
沒有@DS | 主庫 |
@DS("slave") | 存在slave指定slave,不存在主庫 |
@DS | 根據DynamicDataSourceStrategy策略,選擇一個從庫。默認負載均衡策略。 |
注意 | force-master 會判斷當前方法和類上是否有@Transational註解,若是有會強制主數據庫 |
@DS("one") @DS("two") | 表示使用的從庫,one指使用從庫one,two之使用從庫two指的數據庫 |
@DS public interface UserMapper { @Insert("INSERT INTO t_employee (name,cert_num) values (#{name},#{age})") boolean addUser(@Param("name") String name, @Param("age") Integer age); @Update("UPDATE t_employee set name=#{name}, cert_num=#{age} where employee_id =#{id}") boolean updateUser(@Param("id") Integer id, @Param("name") String name, @Param("age") Integer age); @Delete("DELETE from t_employee where employee_id =#{id}") boolean deleteUser(@Param("id") Integer id); @Select("SELECT * FROM t_employee") List<User> selectAll(); }
@DS("three") public interface ScoreMapper { @Select({ "select * from d_import_original_mobile" }) List<Mobile> getAll(); }
在mapper分別使用不一樣的數據源,到最後咱們均可以成功,然而,若是你使用的是xml方式的話,您能夠把註解加在service層上就能夠,最後寫controller,就能夠成功。app
感謝各位大牛的努力成果,詳解請點擊負載均衡
三個依賴+一個配置文件+一個註解(@DS)==實現mysql讀寫分離。spring-boot