Mybatis implements dynamic table creation

1 Introduction

In actual development, sometimes we encounter the situation of dynamic table creation, what does it mean?

To explain, it refers to dynamically creating database tables based on the incoming table names for use in later business scenarios.

Using the dynamic SQL of Mybatis can solve this problem for us well.

For example, now to dynamically create a class table, then the normal database statement may be like this:

CREATE TABLE `2201011` (
    `class_id` int(8) NOT NULL COMMENT' 班级id',                                                                                                                                                                                                                
    `class_name` varchar(100) NOT NULL COMMENT '班级名称',                                                                                                                                                                                                   
    `student_count` int(10) NOT NULL COMMENT '班级学生人数', 
	`teacher_count` int(8) NOT NULL COMMENT '班级配备教师人数', 
    PRIMARY KEY (`class_id`)                                                                                                                                                                                                                                      
) ENGINE InnoDB CHARACTER SET utf8 COLLATE utf8_bin;

The table name is the class name. The latter class name is dynamically passed as a parameter, and each class is such a table structure field. Next, we will implement it with Mybatis's dynamic SQL.

2. Realization of dynamic table creation

The process of building a project is omitted here, and only an implementation of dynamic table building is written.

Mapper layer

package springboot.mybatisplus.student.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import springboot.mybatisplus.student.entity.Student;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author yuhuofei
 * @since 2022-04-05
 */
public interface StudentMapper extends BaseMapper<Student> {
    
    

    /**
     * 动态建表
     *
     * @param tableName
     * @return int
     */
    int createNewTable(@Param("tableName") String tableName);
}

Mapper.xml layer

<?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="springboot.mybatisplus.student.mapper.StudentMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="springboot.mybatisplus.student.entity.Student">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="tid" property="tid"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, name, tid
    </sql>

    <update id="createNewTable" parameterType="String">
        CREATE TABLE ${tableName} (
          class_id int(8) NOT NULL ,
          class_name varchar(100) NOT NULL ,
          student_count int(10) NOT NULL ,
          teacher_count int(8) NOT NULL ,
          PRIMARY KEY (class_id)
        )ENGINE InnoDB CHARACTER SET utf8 COLLATE utf8_bin
    </update>

</mapper>

Guess you like

Origin blog.csdn.net/Crezfikbd/article/details/125149479