java隨記

          堅持就是勝利!

           

          springboot、mybatis、mycat分庫實踐

          1.pom文件中引入下面引入mybatis逆向工程插件
          <build>
          <plugins>
          <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
          <plugin>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.3.4</version>
          <configuration>
          <verbose>true</verbose>
          <overwrite>true</overwrite>
          </configuration>
          </plugin>
          </plugins>
          </build>
             2, src/main/resource 目錄下引入mybatis逆向工程配置文件,這樣就可以自動生成mybatis需要的xml及dao interface
          <?xml version="1.0" encoding="UTF-8"?>
           <!DOCTYPE generatorConfiguration
             PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
             "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
           <generatorConfiguration>
               <!--數據庫驅動-->
               <classPathEntry    location="E:\\worksource\\springboot\\src\\main\\webapp\\WEB-INF\\lib\\mysql-connector-java-5.1.39.jar"/>
               <context id="DB2Tables"    targetRuntime="MyBatis3">
                   <commentGenerator>
                       <property name="suppressDate" value="true"/>
                       <property name="suppressAllComments" value="true"/>
                   </commentGenerator>
                   <!--數據庫鏈接地址賬號密碼-->
                   <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/jeeshop" userId="root" password="">
                   </jdbcConnection>
                   <javaTypeResolver>
                       <property name="forceBigDecimals" value="false"/>
                   </javaTypeResolver>
                   <!--生成Model類存放位置-->
                   <javaModelGenerator targetPackage="com.junjun.myblog.domain" targetProject="src/main/java">
                       <property name="enableSubPackages" value="true"/>
                       <property name="trimStrings" value="true"/>
                   </javaModelGenerator>
                   <!--生成映射文件存放位置-->
                   <sqlMapGenerator targetPackage="src/main/resources/mapper" targetProject=".">
                       <property name="enableSubPackages" value="true"/>
                   </sqlMapGenerator>
                   <!--生成Dao類存放位置-->
                   <javaClientGenerator type="XMLMAPPER" targetPackage="com.junjun.myblog.dao" targetProject="src/main/java">
                       <property name="enableSubPackages" value="true"/>
                   </javaClientGenerator>
                   <!--生成對應表及類名 mapperName="AreaDao"  工程右鍵 Debug As -> maven build... -> mybatis-generator:generate
                   <table tableName="t_area"   domainObjectName="Area" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
              -->
          </context>
           </generatorConfiguration>
          3,application 中加上注解  @MapperScan("com.junjun") 
          4,數據源連接mycat
          spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
          spring.datasource.driver-class-name=com.mysql.jdbc.Driver
          spring.datasource.url=jdbc:mysql://localhost:8066/MYSQL?useUnicode=true&characterEncoding=UTF-8
          spring.datasource.username=root
          spring.datasource.password=123456
          二,mycat配置
          1,conf/server.xml中配置用戶 和schema
          <user name="root">
          <property name="password">123456</property>
          <property name="schemas">MYSQL</property>
          </user>
          2,schema.xml配置數據庫和分片表分片規則
          <schema name="MYSQL" checkSQLschema="false" sqlMaxLimit="100">
          <table name="t_blogger" primaryKey="id" dataNode="dn1" >     </table> 
          <!--按需配置分片規則-->
                  <table name="t_area" primaryKey="id" dataNode="dn1,dn2,dn3" rule="mod-long1">     </table> 
          <table name="t_blog" primaryKey="id" dataNode="dn1" >     </table> 
          <table name="t_blogtype" primaryKey="id" dataNode="dn1" >     </table> 
          </schema>
          <!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"
          /> -->
          <dataNode name="dn1" dataHost="localhost1" database="jeeshop" />
          <dataNode name="dn2" dataHost="localhost2" database="jysystem" />
          <dataNode name="dn3" dataHost="localhost3" database="dn3" />
          <!--<dataNode name="dn4" dataHost="sequoiadb1" database="SAMPLE" />
          <dataNode name="jdbc_dn1" dataHost="jdbchost" database="db1" />
          <dataNode name="jdbc_dn2" dataHost="jdbchost" database="db2" />
          <dataNode name="jdbc_dn3" dataHost="jdbchost" database="db3" /> -->
          <dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
            writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
          <heartbeat>select user()</heartbeat>
          <!-- can have multi write hosts -->
          <writeHost host="hostM1" url="localhost:3306" user="root"
             password="">
          <!-- can have multi read hosts 
          <readHost host="hostS2" url="192.168.1.200:3306" user="root" password="" /> -->
          </writeHost>
          <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
          </dataHost>
          <dataHost name="localhost2" maxCon="1000" minCon="10" balance="0"
            writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
          <heartbeat>select user()</heartbeat>
          <!-- can have multi write hosts -->
          <writeHost host="hostM2" url="127.0.0.1:3306" user="root"
             password="">
          <!-- can have multi read hosts 
          <readHost host="hostS2" url="192.168.1.200:3306" user="root" password="" /> -->
          </writeHost>
          <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
          </dataHost>
          <dataHost name="localhost3" maxCon="1000" minCon="10" balance="0"
            writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
          <heartbeat>select user()</heartbeat>
          <!-- can have multi write hosts -->
          <writeHost host="hostM2" url="127.0.0.1:3306" user="root"
             password="">
          <!-- can have multi read hosts 
          <readHost host="hostS2" url="192.168.1.200:3306" user="root" password="" /> -->
          </writeHost>
          <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
          </dataHost>
          3,rule.xml配置分片規則
              添加如下規則 按表的name字段進行分片存儲
          <tableRule name="mod-long1">
          <rule>
          <columns>name</columns>
          <algorithm>mod-long</algorithm>
          </rule>
          </tableRule>
          備注:內容太多,記一下重點. 分庫后連接查詢不在同一個庫是查不到數據的,真有這需要那就真需要考慮
          設計是否合理了.

          posted on 2017-10-01 17:40 傻 瓜 閱讀(5106) 評論(0)  編輯  收藏 所屬分類: 雜項

          導航

          統計

          常用鏈接

          留言簿(7)

          我參與的團隊

          隨筆分類

          隨筆檔案

          文章分類

          友情鏈接

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 木兰县| 福清市| 鹿邑县| 嘉黎县| 抚州市| 赤水市| 临泉县| 错那县| 浦北县| 西宁市| 太谷县| 宁城县| 齐齐哈尔市| 正安县| 西充县| 宁远县| 平乐县| 东明县| 固始县| 黑河市| 龙胜| 会泽县| 亳州市| 垣曲县| 晋宁县| 永安市| 昂仁县| 海宁市| 贵定县| 天长市| 岑巩县| 开化县| 雷州市| 永宁县| 平遥县| 吉水县| 兖州市| 岐山县| 玛曲县| 策勒县| 宁陕县|