Mybatis 自动建表

时间:2020-03-27
本文章向大家介绍Mybatis 自动建表,主要包括Mybatis 自动建表使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在项目中经常会遇到动态创建表的场景,例如,统计用户日活,每天活跃的用户数据需要单独的表中(eg: tb_user_active_20200327,tb_user_active_20200328),根据用户活跃时间把数据存入不同的表中。这些和日期项目的表结构需要我们起个定时器,每天定时创建。具体参考以下示例:

  1. 表结构
create table tb_user_active(
    id int(11) primary key auto_increment comment 'id',
    user_id int(11) comment '用户ID',
    active_time datetime comment '活跃时间'
)    
  1. UserActiveService
import com.test.mybatis.mapper.UserActiveMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 */
@Service
public class UserActiveService {
    /**
     * 日活表前缀
     */
    public static final String USER_ACTIVE_TABLE_PREFIX = "tb_user_active_";

    @Autowired(required = false)
    UserActiveMapper userActiveMapper;

    /**
     * 定时器(每天凌晨1点执行一次)
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void createTableJob() {
        int num = 3;

        for(int i=1; i<=num; i++) {
            Calendar c = Calendar.getInstance();
            c.add(Calendar.DAY_OF_MONTH, i);

            createTable(c.getTime());
        }
    }

    /**
     * 获取表名
     * @param date
     * @return
     */
    public String getTableName(Date date) {
        return USER_ACTIVE_TABLE_PREFIX + new SimpleDateFormat("yyyyMMdd").format(date);
    }

    /**
     * 创建表
     * @param date
     */
    public void createTable(Date date) {
        String sql = "create table if not exists " + getTableName(date) + "(\n" +
                    "    id int(11) primary key auto_increment comment 'id',\n" +
                    "    user_id int(11) comment '用户ID',\n" +
                    "    active_time datetime comment '活跃时间'\n" +
                    ")";
        userActiveMapper.createTable(sql);
    }
}

  1. UserMapper
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

/**
 */
@Repository
public interface UserActiveMapper {

    /**
     * 建表
     * @param createTabelSql
     */
    @Update("${createTabelSql}")
    void createTable(@Param("createTabelSql")String createTabelSql);
}

以上是自动建表的主要代码,如需源码,请点击获取auto-create-table

注意:如果出现java.lang.IllegalAccessError: org.apache.commons.dbcp.DelegatingPreparedStatement.isClosed()Z 错误,可能是dbcp和mybatis版本不兼容导致,具体版本请参考pom.xml

原文地址:https://www.cnblogs.com/rookie-test/p/12581518.html