tk.mybatis 分表 實現數據庫增刪改查

需求描述:

根據參數變量pid,動態獲取各個省份(pid)所屬的表,如 pid 爲1 則查詢的表爲 table_1。

 

 

具體實現:

1、建立兩個測試表 test_1、test_2

DROP TABLE IF EXISTS `test_1`;
CREATE TABLE `test_1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of test_1
-- ----------------------------
INSERT INTO `test_1` VALUES ('1', '1111');



DROP TABLE IF EXISTS `test_2`;
CREATE TABLE `test_2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of test_1
-- ----------------------------
INSERT INTO `test_1` VALUES ('1', '2222');

 

2、實體類test實現 IDynamicTableName 接口,重寫 getDynamicTableName 方法

import javax.persistence.*;

/**
 * @description: 測試分表實體類
 * @author: 
 * @create: 
 */
@Data
@Table(name = "test")
public class Test implements IDynamicTableName {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;

    @Transient//非數據庫字段,不查詢
    private String pid;

    /** 
     * @description:  動態表名拼接組裝方法
     * @param:  
     * @return: java.lang.String 
     * @author: chenping
     * @date: 2020/3/2
     **/
    @Override
    public String getDynamicTableName() {
        StringBuffer tableName = new StringBuffer(this.getClass().getAnnotation(Table.class).name());
        return tableName+"_"+pid;
    }

3、實體類對應的mapper不變:

import tk.mybatis.mapper.common.Mapper;
public interface TestMapper extends Mapper<Test> {
}

 

4、測試 簡單的增刪改查:

@RestController
@Log4j2
public class TestController extends BaseController {
 

    @Autowired
    TestMapper testMapper;

    /** 
     * @description:  測試分表接口
     * @param:  
     * @return: java.lang.String 
     * @author: 
     * @date: 
     **/
    @PostMapping("/test")
    public String test() {
        Test t = new Test();
        t.setPid("2");
        List<Test> list = testMapper.select(t);
//
//        //insert
//        t.setPid("1");
//        t.setName("11111114");
//        int insert = testMapper.insert(t);
//        log.info("insert num:"+insert);

        //update
        t.setPid("2");
        t.setId(1);
        t.setName("22222222223");
        /**
         * 不支持 Example 更新,需要將實體獲取的表名set給Example
         **/
        Example example = new Example(t.getClass());
        example.setTableName(t.getDynamicTableName());
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", 1);
        criteria.andEqualTo("name", "22222222221");
//        int update = testMapper.updateByExampleSelective(t,example);//不加example.setTableName 不支持
        int update = testMapper.updateByExample(t,example);//不加example.setTableName 不支持
//        int update = testMapper.updateByPrimaryKey(t);//支持
//        int update = testMapper.updateByPrimaryKeySelective(t);//支持
        log.info("update num:"+update);

        //delete
//        t.setPid("1");
//        t.setId(3);
//        int delete = testMapper.delete(t);
//        log.info("delete num:"+delete);
        return update+"";
    }

5、調用接口:

http://localhost:8080/test

6、查詢日誌示例展示:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章