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依赖4.0.0 com.spring.mybatis com-spring-mybatis-common-data-demo war 0.0.1-SNAPSHOT com-spring-mybatis-common-data-demo Maven Webapp http://maven.apache.org UTF-8 2.0 4.11 com.spring.mybatis spring-mybatis-data-common ${spring.mybatis.data.version} junit junit ${junit.version} test com.alibaba druid 0.2.26 mysql mysql-connector-java 5.1.1 org.codehaus.jackson jackson-mapper-asl 1.9.13 com-spring-mybatis-common-data-demo
2.持久层继承分表Dao接口,也可以自己定义
@Repositorypublic interface ExampleModelDao extends ShardCudDao,ShardReadDao { /**get common base table max auto_increment id*/ public Long selectMaxId() throws DaoException;}
在持久层自定义了一个selectMaxId()用于多个分表共享同一个自增策略,这里采用程序级别控制
3.业务层继承分表,业务层操作可以自定义,也可以继承com.spring.mybatis.data.common.service.BaseService中提供的常规业务@Servicepublic 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 * * 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 * * 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.持久层实现
INSERT INTO ${tablename}( id, eId, exampleName, exampleTitle, exampleDate) VALUES( #{object.id}, #{object.eId}, #{object.exampleName}, #{object.exampleTitle}, #{object.exampleDate} ) DELETE FROM ${tablename} WHERE id=#{id}
程序运行结果
查询各个分表
实例下载:
转载请注明地址:[]