Code Monkey home page Code Monkey logo

dynamic_datasource_springboot_starter's Introduction

dynamic_datasource_springboot_starter

springboot mybatis 读写分离 支持事务

版本:

  • springboot 1.0+
  • jdk8
  • maven 3.0+

使用方法:

        <dependency>
            <groupId>com.djk</groupId>
            <artifactId>dynamic_datasource_springboot_starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

目前默认一个主库 一个从库 在项目的application.properties 文件中 添加

spring.datasource.type=com.zaxxer.hikari.HikariDataSource

spring.datasource.hikari.master.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.master.jdbc-url=jdbc:mysql://192.168.0.11:3306/tests?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
spring.datasource.hikari.master.username=root
spring.datasource.hikari.master.password=123456
spring.datasource.hikari.master.minimum-idle=5
spring.datasource.hikari.master.maximum-pool-size=15
spring.datasource.hikari.master.auto-commit=true
spring.datasource.hikari.master.idle-timeout=30000
spring.datasource.hikari.master.pool-name=DatebookHikariCP
spring.datasource.hikari.master.max-lifetime=1800000
spring.datasource.hikari.master.connection-timeout=30001
spring.datasource.hikari.master.connection-test-query=SELECT 1


spring.datasource.hikari.slave1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.slave1.jdbc-url=jdbc:mysql://192.168.0.11:3306/tests?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
spring.datasource.hikari.slave1.username=root
spring.datasource.hikari.slave1.password=123456
spring.datasource.hikari.slave1.minimum-idle=5
spring.datasource.hikari.slave1.maximum-pool-size=15
spring.datasource.hikari.slave1.auto-commit=true
spring.datasource.hikari.slave1.idle-timeout=30000
spring.datasource.hikari.slave1.pool-name=DatebookHikariCP
spring.datasource.hikari.slave1.max-lifetime=1800000
spring.datasource.hikari.slave1.connection-timeout=30001
spring.datasource.hikari.slave1.connection-test-query=SELECT 1

代码中使用

  • 在代码的数据库层添加注解
主库注解   @DataSource(value = DynamicDataSourceEnum.WRITE)
从库注解   @DataSource(value = DynamicDataSourceEnum.READ)
  • 如果遇到事务 则会强制走主库

配置多个从库

参照DataSourceAutoConfigurer.java中配置的slave1即可

dynamic_datasource_springboot_starter's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.