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 傻 瓜 阅读(5076) 评论(0)  编辑  收藏 所属分类: 杂项


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(7)

我参与的团队

随笔分类

随笔档案

文章分类

友情链接

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜