spring-mybatis-data-common程序级分表操作实例
spring-mybatis-data-common-2.0新增分表机制,在1.0基础上做了部分调整.
基于机架展示分库应用
数据库分表实力创建
create table tb_example_1(
id bigint primary key auto_increment ,
eId bigint,
exampleName varchar(40),
exampleTitle varchar(200),
exampleDate datetime
)ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; create table tb_example_2 like tb_example_1; create table tb_example_3 like tb_example_1; create table tb_example(
id bigint primary key auto_increment ,
eId bigint,
exampleName varchar(40),
exampleTitle varchar(200),
exampleDate datetime
)ENGINE=MERGE UNION=(tb_example_1,tb_example_2,tb_example_3) INSERT_METHOD=LAST AUTO_INCREMENT=1 ;
程序构建分表操作
1.spring-mybatis-common-data中提供了com.spring.mybatis.data.common.model.ExampleModel用于Demo的实体类
添加maven依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring.mybatis</groupId>
<artifactId>com-spring-mybatis-common-data-demo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>com-spring-mybatis-common-data-demo Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.mybatis.data.version>2.0</spring.mybatis.data.version>
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<!-- spring-mybatis-data-common begin -->
<dependency>
<groupId>com.spring.mybatis</groupId>
<artifactId>spring-mybatis-data-common</artifactId>
<version>${spring.mybatis.data.version}</version>
</dependency>
<!-- spring-mybatis-data-common end -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>0.2.26</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
</dependencies>
<build>
<finalName>com-spring-mybatis-common-data-demo</finalName>
</build>
</project>
2.持久层继承分表Dao接口,也可以自己定义
@Repository
public interface ExampleModelDao extends ShardCudDao<ExampleModel>,ShardReadDao<ExampleModel>{ /**get common base table max auto_increment id*/
public Long selectMaxId() throws DaoException; }
在持久层自定义了一个selectMaxId()用于多个分表共享同一个自增策略,这里采用程序级别控制
3.业务层继承分表,业务层操作可以自定义,也可以继承com.spring.mybatis.data.common.service.BaseService中提供的常规业务
@Service
public class ExampleModelService extends BaseShard{ @Autowired
private ExampleModelDao exampleModelDao; @Override
public String getBaseShardTableName() {
return "tb_example_";
} @Override
public int getShardTableCount() {
return 3;
} public int deleteObject(ExampleModel entity) throws ServiceException {
try {
return this.exampleModelDao.delete(getShardTableName(entity.geteId()+"", 1), entity.getId());
} catch (DaoException e) {
e.printStackTrace();
}
return 0;
} public int save(ExampleModel entity) throws ServiceException {
long mxId = 1;
try {
Long maxId = this.exampleModelDao.selectMaxId();
if(null != maxId && maxId >= 1){
mxId = maxId + 1;
}
} catch (DaoException e) {
LogUtils.dao.error("insert exampleModel before get Max Id error.",e);
e.printStackTrace();
}
try {
entity.setId(mxId);
return this.exampleModelDao.insert(getShardTableName(entity.geteId()+"", 1), entity);
} catch (DaoException e) {
LogUtils.dao.error("insert exampleModel to table " + getShardTableName(entity.geteId()+"", 1) + " error");
e.printStackTrace();
}
return 0;
} public ExampleModel selectObject(ExampleModel entity)
throws ServiceException {
try {
return this.exampleModelDao.selectById(getShardTableName(entity.geteId()+"", 1), entity.geteId());
} catch (DaoException e) {
e.printStackTrace();
}
return null;
} public void setExampleModelDao(ExampleModelDao exampleModelDao) {
this.exampleModelDao = exampleModelDao;
} }
BaseShard是一个抽象类,继承它需要实现两个方法.
/**
* get shard table count
* @return
*/
public abstract int getShardTableCount(); /**
* get base shard name
* @return
*/
public abstract String getBaseShardTableName();
getShardTableCount()用于返回分表数量, public abstract String getBaseShardTableName()用于返回分表表名统一前缀.
如实例中的分表为tb_example、tb_example_1、tb_example_2、tb_example_3,分表表名前缀为"tb_example_",分表数量为3.
BaseShard中获取映射表名的操作
/**
* get shard table name <br>
*
* shard table index start with 0
*
* just like follows
*
* tb_example_0
* tb_example_1
* tb_example_2
* tb_example_3
*
* @param tableName
* @return
*/
public String getShardTableName(String shardKey); /**
* get shard table name <br>
*
* shard table index start with (0+baseNumber)
*
* just like follows
*
* tb_example_(0+baseNumber)
* tb_example_(1+baseNumber)
* tb_example_(2+baseNumber)
* tb_example_(3+baseNumber)
*
*
* @param shardKey
* @param baseNumber
* @return
*/
public String getShardTableName(String shardKey,int baseNumber);
4.持久层实现
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.spring.mybatis.common.data.demo.dao"> <!--insert entity-->
<insert id="insert" parameterType="exampleModel" flushCache="true">
INSERT INTO ${tablename}(
id,
eId,
exampleName,
exampleTitle,
exampleDate)
VALUES(
#{object.id},
#{object.eId},
#{object.exampleName},
#{object.exampleTitle},
#{object.exampleDate}
)
</insert> <select id="selectMaxId" resultType="long">
select max(id) from tb_example
</select> <delete id="delete" parameterType="long" flushCache="true">
DELETE FROM
${tablename}
WHERE
id=#{id}
</delete> <select id="selectById" parameterType="long" resultType="exampleModel">
SELECT
id AS id,eId AS eId,exampleName AS exampleName,exampleTitle AS exampleTitle,exampleDate AS exampleDate
FROM
${tablename}
WHERE
id=#{id}
</select> </mapper>
程序运行结果

查询各个分表

实例下载: http://files.cnblogs.com/dennisit/spring-mybatis-data-common-2.0-and-demo.7z
转载请注明地址:[http://www.cnblogs.com/dennisit/p/3793501.html]
spring-mybatis-data-common程序级分表操作实例的更多相关文章
- Spring boot配置多个Redis数据源操作实例
原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...
- 3.springMVC+spring+Mybatis整合Demo(单表的增删该查,这里主要是贴代码,不多解释了)
前面给大家讲了整合的思路和整合的过程,在这里就不在提了,直接把springMVC+spring+Mybatis整合的实例代码(单表的增删改查)贴给大家: 首先是目录结构: 仔细看看这个目录结构:我不详 ...
- Mybatis(四)多表操作
数据库如下: 一.创建数据库所对应的bean类 public class User { private Integer uId; private String username; private St ...
- Spring Cloud Data Flow用Shell来操作,方便建立CICD
1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! 之前我们用两篇文章讲解了Spring Cloud Data Flow,例子都是用UI操作的,但我们在Linux系统上经常是无 ...
- MariaDB数据表操作实例
1. MariaDB 数据库操作实例 MariaDB>create database class; //创建class数据库 MariaDB>use class; MariaDB>c ...
- C#简单注册表操作实例
1.简介操作 //设置注册值 private void Button_Click(object sender, RoutedEventArgs e) { //路径及间隔符号要正确 //1.如果指定路径 ...
- Spring Data JPA 实现多表关联查询
本文地址:https://liuyanzhao.com/6978.html 最近抽出时间来做博客,数据库操作使用的是 JPA,相对比 Mybatis 而言,JPA 单表操作非常方便,增删改查都已经写好 ...
- Mybatis高级:Mybatis注解开发单表操作,Mybatis注解开发多表操作,构建sql语句,综合案例学生管理系统使用接口注解方式优化
知识点梳理 课堂讲义 一.Mybatis注解开发单表操作 *** 1.1 MyBatis的常用注解 之前我们在Mapper映射文件中编写的sql语句已经各种配置,其实是比较麻烦的 而这几年来注解开发越 ...
- Spring Cloud Data Flow整合UAA使用外置数据库和API接口
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 之前的文章<Spring Cloud Data Flow整合Cloudfoundry UAA服务做权限控制 ...
随机推荐
- Delphi自动适应屏幕分辨率的属性
https://www.cnblogs.com/zhangzhifeng/category/835602.html 这是个困惑我很长时间的问题,到今天终于得到解决了. 话说Delphi有个很强的窗体设 ...
- 异常Cannot get a text value from a numeric cell
POI操作Excel时偶尔会出现Cannot get a text value from a numeric cell的异常错误. 异常原因:Excel数据Cell有不同的类型,当我们试图从一个数字类 ...
- union的含义
用于合并两个或者多个select语句的结果集 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 SELECT 语句中的列的顺序必须相同. ex ...
- Linux学习之常用权限管理命令(二)
(一)常用权限管理命令 (1)chmod命令 (2)chown (3)chgrp (4)umask (一)常用权限管理命令 (1)chmod命令 命令名称:chmod命令英文原意:change the ...
- codeforces895E. Eyes Closed
题目链接 codeforces895E. Eyes Closed 题解 线段树维护期望和 写出修改两区间的相互影响 就是一个区间修改 emmm考试的代码过不去,这么松的spj都过不去Orz,每次和答案 ...
- BZOJ.4766.文艺计算姬(Prufer)
题目链接 这是完全二分图,那么在构造Prufer序列时,最后会剩下两个点,两点的边是连接两个集合的,这两个点自然分属两个集合 那么集合A被删了m-1次,每次从n个点中选:B被删了n-1次,每次都可以从 ...
- BZOJ.2208.[JSOI2010]连通数(bitset Tarjan 拓扑)
题目链接 先缩点,对于scc之间贡献即为szscc[i]*szscc[j] 用f[i][j]表示scci是否能到sccj 拓扑排序,每次把now的f或上to的f 用bitset优化 //63888kb ...
- Python图形编程探索系列-08-再次认识标签
标签的各种属性 代码展示: import tkinter as tk root = tk.Tk() root.geometry = '500x300' label1 = tk.Label(root, ...
- Problem D: 指针函数
Description YHZ自认为很聪明的人, 在C语言课上老师布置了一个作业,让能求正方形和圆的面积, 正当YHZ要跃跃欲试的时候, 老师却要求使用函数指针来实现这个功能,YHZ立马就不会了,他现 ...
- C++学习笔记40:进程应用
进程创建 system()函数:用于在程序中执行一条命令 如果shell不能运行,返回127,如果发生其他错误返回-1: 例子:int ret_val = system(“ls -l /”); for ...