欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?Boot實現(xiàn)MyBatis動態(tài)創(chuàng)建表的操作語句

 更新時間:2024年01月29日 15:41:13   作者:早起的年輕人  
這篇文章主要介紹了Spring?Boot實現(xiàn)MyBatis動態(tài)創(chuàng)建表,MyBatis提供了動態(tài)SQL,我們可以通過動態(tài)SQL,傳入表名等信息然組裝成建表和操作語句,本文通過案例講解展示我們的設計思路,需要的朋友可以參考下

在有些應用場景中,我們會有需要動態(tài)創(chuàng)建和操作表的需求。

比如因為單表數(shù)據(jù)存儲量太大而采取分表存儲的情況,又或者是按日期生成日志表存儲系統(tǒng)日志等等。這個時候就需要我們動態(tài)的生成和操作數(shù)據(jù)庫表了。

而我們都知道,以往我們使用MyBatis是需要提前生成包括Model,Mapper和XML映射文件的,顯然因為動態(tài)生成和操作表的需求一開始表都是不存在的,所以也就不能直接通過MyBatis連接數(shù)據(jù)庫來生成我們的數(shù)據(jù)訪問層代碼并用來訪問數(shù)據(jù)庫了。

MyBatis提供了動態(tài)SQL,我們可以通過動態(tài)SQL,傳入表名等信息然組裝成建表和操作語句。

本小節(jié)中實現(xiàn)的案例中每個用戶都會有一個自己日志表,我們的設計 思路就是在新創(chuàng)建用戶的時候,根據(jù)用戶的信息 創(chuàng)建一個日志存儲表,表名是根據(jù)用戶的 id 來創(chuàng)建,首先是控制中新增用戶:

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @PostMapping(value="/add")
    public Object addUser(@RequestBody User user) {
        return userService.addUser(user);
    }
}

然后用戶的操作IUserService實現(xiàn)定義如下:

@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
?
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserLogMapper userLogMapper;
?
    @Override
    @Transactional
    public User addUser(User user) {
        // 插入
        userMapper.saveUser(user);
        // 添加用戶時,創(chuàng)建日志存儲表
        Integer id = user.getId();
        //定義用戶日志表表名
        String tableName = "t_user_log_" + id;
        //查詢表是否存在
        if (userLogMapper.existTable(tableName) > 0) {
            //刪除用戶對應的日志表
            userLogMapper.dropTable(tableName);
        }
        //新創(chuàng)建表
        userLogMapper.createTable(tableName);
        return user;
    }
}

UserMapper 就是操作用戶數(shù)據(jù)相關的,這里使用的是新增用戶的數(shù)據(jù):

@Mapper
public interface UserMapper extends BaseMapper<User> {
    int saveUser(@Param("user") User user);
}

對應的xml

<?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="flutter.dio.model.mapper.UserMapper">
    <insert id="saveUser"  useGeneratedKeys="true" keyProperty="id">
        insert into t_user(name,password,age)
        values(#{user.name},#{user.password},#{user.age})
    </insert>
</mapper>

用戶新建成功后,再根據(jù)用戶的id定義表名,然后創(chuàng)建新的日志表:

UserLogMapper 是用戶日志操作使用Mapper ,定義如下:

public interface UserLogMapper {
    //保存用戶的日志 
    int insert(@Param("tableName")String tableName, @Param("userLog") UserLog userLog);
    /**
     * 查找用戶全部的日志
     * @param tableName 用戶對應的表名
     * @return
     */
    List<UserLog> selectAll(@Param("tableName")String tableName);
?
    /**
     * 是否存在表
     * @param tableName
     * @return
     */
    int existTable(@Param("tableName")String tableName);
    /**
     * 刪除表
     * @param tableName
     * @return
     */
    int dropTable(@Param("tableName")String tableName);
    /**
     * 創(chuàng)建表
     * @param tableName
     * @return
     */
    int createTable(@Param("tableName")String tableName);
}

UserLogMapper對應的xml核心內(nèi)容如下:

<?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="flutter.dio.model.mapper.UserLogMapper">
    <resultMap id="BaseResultMap" type="flutter.dio.model.entity.UserLog">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="user_name" jdbcType="VARCHAR" property="userName"/>
        <result column="operation" jdbcType="VARCHAR" property="operation"/>
        <result column="method" jdbcType="VARCHAR" property="method"/>
        <result column="params" jdbcType="VARCHAR" property="params"/>
        <result column="time" jdbcType="BIGINT" property="time"/>
        <result column="ip" jdbcType="VARCHAR" property="ip"/>
    </resultMap>
    <sql id="Base_Column_List">
        id, user_name, operation, method, params, time, ip
    </sql>
?
    <insert id="insert" parameterType="flutter.dio.model.entity.UserLog">
        insert into ${tableName} (id, user_name, operation,
                                  method, params, time,
                                  ip)
        values (#{userLog.id,jdbcType=BIGINT}, #{userLog.userName,jdbcType=VARCHAR},
                #{userLog.operation,jdbcType=VARCHAR},
                #{userLog.method,jdbcType=VARCHAR}, #{userLog.params,jdbcType=VARCHAR}, #{userLog.time,jdbcType=BIGINT},
                #{userLog.ip,jdbcType=VARCHAR})
    </insert>
?
    <select id="selectAll" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from ${tableName}
    </select>
?
    <!--    查看指定的表是否存在-->
    <select id="existTable" parameterType="String" resultType="Integer">
        select count(*)
        from information_schema.TABLES
        where table_name = #{tableName}
    </select>
    <!-- 刪除指定的表-->
    <update id="dropTable">
        DROP TABLE IF EXISTS ${tableName}
    </update>
    <!-- 創(chuàng)建新的日志表-->
    <update id="createTable" parameterType="String">
        CREATE TABLE ${tableName}
        (
            `id`        bigint(20) NOT NULL AUTO_INCREMENT COMMENT '編號',
            `user_name` varchar(50)   DEFAULT NULL COMMENT '用戶名',
            `operation` varchar(50)   DEFAULT NULL COMMENT '用戶操作',
            `method`    varchar(200)  DEFAULT NULL COMMENT '請求方法',
            `params`    varchar(5000) DEFAULT NULL COMMENT '請求參數(shù)',
            `time`      bigint(20) NOT NULL COMMENT '執(zhí)行時長(毫秒)',
            `ip`        varchar(64)   DEFAULT NULL COMMENT 'IP地址',
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=2897 DEFAULT CHARSET=utf8 COMMENT='用戶操作日志';
    </update>
</mapper>

上述代碼中包括兩部分內(nèi)容,一部分是對表的操作 創(chuàng)建 與 刪除,另一部分是對表中的數(shù)據(jù)的操作,保存用戶的日志數(shù)據(jù)與查詢用戶的日志數(shù)據(jù),都需要將用戶對應的日志表名做為參數(shù)查詢。

到此這篇關于Spring Boot實現(xiàn)MyBatis動態(tài)創(chuàng)建表的文章就介紹到這了,更多相關Spring Boot MyBatis創(chuàng)建表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論