Mybatisplus整合sharding-jdbc

来源:互联网 发布:万户网络做网站怎么样 编辑:程序博客网 时间:2024/05/17 04:17

首先说明一下,本项目是一个springboot项目,整合了mybatis-plus、sharding-jdbc,如果有不了解mybatis-plus或者sharding-jdbc的同学请先事先看一下相关文档(百度即可,都是开源项目),这里我做一下大体介绍;

mybatis-plus:它是mybatis的升级版,深度封装了mybatis,比如通常我们mybatis都需要写sql语句来对应相应的mapper,mybatis-plus可以做到单表操作不用写sql,通过java代码即可解决,并且支持兼容mybatis的写法,可以提高我们的开发效率。

sharding-jdbc:当当网开源的一款分库分表的框架,现在已经推出了2.x版本,可以通过它对之前未分库分表的项目进行升级,需要改的地方较少,是项目做分库分表的一个比较好的选择;说到分库分表,可能很多同学会说怎么不用mycat呀,也是开源的项目。说到这里,我觉着他们各有各的好处,shardingjdbc是在代码层面的实现了分库分表,需要对之前的代码进行一些改造,而mycat则是一个服务,相当于一个数据库中间代理,我们在代码中只需要指定这个mycat的scheme即可,具体的数据库信息、分表策略都是在mycat的配置文件中,但是mycat有一个缺点在当前的最新版本中(1.6版本),在做水平分表的时候,比如说我要把user表分成100份,分在同一个库,user${1..100},它只能支持单datanode,目前还不支持多个数据库这样操作,sharding-jdbc可以。

接下来说一下本项目中的配置文件(这个整合我已近上传到github,有需要代码的同学请去github下载    https://github.com/xiaonaiping/mybatisplus_shardingjdbc)

#appserver:    port: 8080#springspring:  devtools:    restart:      enabled: false#sharding-jdbcsharding:  jdbc:      datasource:        names: ds_0,ds_1        ds_0:          type: org.apache.commons.dbcp.BasicDataSource          driverClassName: com.mysql.jdbc.Driver          url: jdbc:mysql://192.168.150.129:3306/demo_ds_0          username: root          password: root        ds_1:          type: org.apache.commons.dbcp.BasicDataSource          driverClassName: com.mysql.jdbc.Driver          url: jdbc:mysql://192.168.150.129:3308/demo_ds_1?useUnicode=true&characterEncoding=UTF-8          username: root          password: root      config:        sharding:          defaultDataSourceName: ds_1          default-database-strategy:            inline:              sharding-column: user_id              algorithm-expression: ds_${(user_id % 2)+0}          tables:            t_order:              actualDataNodes: ds_${0..1}.t_order_${0..2}              tableStrategy:                inline:                  shardingColumn: user_id                  algorithmExpression: t_order_${user_id % 3}              keyGeneratorColumnName: order_id            t_order_item:              actualDataNodes: ds_${0..1}.t_order_item_${0..1}              tableStrategy:                inline:                  shardingColumn: user_id                  algorithmExpression: t_order_item_${user_id % 2}              keyGeneratorColumnName: order_item_id#mybatismybatis-plus:  datasource: dataSource  mapper-locations: classpath:/mapper/*Mapper.xml  #实体扫描,多个package用逗号或者分号分隔  typeAliasesPackage: com.baomidou.springboot.entity  typeEnumsPackage: com.baomidou.springboot.entity.enums  global-config:    #主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";    id-type: 2    #字段策略 0:"忽略判断",1:" NULL 判断"),2:"非空判断"    field-strategy: 2    #驼峰下划线转换    db-column-underline: true    #刷新mapper 调试神器    refresh-mapper: true    #数据库大写下划线转换    #capital-mode: true    #序列接口实现类配置    #key-generator: com.baomidou.springboot.xxx    #逻辑删除配置    logic-delete-value: 0    logic-not-delete-value: 1    #自定义填充策略接口实现    #meta-object-handler: com.baomidou.springboot.xxx    #自定义SQL注入器    #sql-injector: com.baomidou.springboot.xxx  configuration:    map-underscore-to-camel-case: true    cache-enabled: false#logginglogging:  level: warn

原创粉丝点击