亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

在有些應(yīng)用場(chǎng)景中,我們會(huì)有需要?jiǎng)討B(tài)創(chuàng)建和操作表的需求。

比如因?yàn)閱伪頂?shù)據(jù)存儲(chǔ)量太大而采取分表存儲(chǔ)的情況,又或者是按日期生成日志表存儲(chǔ)系統(tǒng)日志等等。這個(gè)時(shí)候就需要我們動(dòng)態(tài)的生成和操作數(shù)據(jù)庫(kù)表了。

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

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

本小節(jié)中實(shí)現(xiàn)的案例中每個(gè)用戶都會(huì)有一個(gè)自己日志表,我們的設(shè)計(jì) 思路就是在新創(chuàng)建用戶的時(shí)候,根據(jù)用戶的信息 創(chuàng)建一個(gè)日志存儲(chǔ)表,表名是根據(jù)用戶的 id 來(lái)創(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實(shí)現(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);
        // 添加用戶時(shí),創(chuàng)建日志存儲(chǔ)表
        Integer id = user.getId();
        //定義用戶日志表表名
        String tableName = "t_user_log_" + id;
        //查詢表是否存在
        if (userLogMapper.existTable(tableName) > 0) {
            //刪除用戶對(duì)應(yīng)的日志表
            userLogMapper.dropTable(tableName);
        }
        //新創(chuàng)建表
        userLogMapper.createTable(tableName);
        return user;
    }
}

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

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

對(duì)應(yīng)的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 用戶對(duì)應(yīng)的表名
     * @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對(duì)應(yīng)的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 '編號(hào)',
            `user_name` varchar(50)   DEFAULT NULL COMMENT '用戶名',
            `operation` varchar(50)   DEFAULT NULL COMMENT '用戶操作',
            `method`    varchar(200)  DEFAULT NULL COMMENT '請(qǐng)求方法',
            `params`    varchar(5000) DEFAULT NULL COMMENT '請(qǐng)求參數(shù)',
            `time`      bigint(20) NOT NULL COMMENT '執(zhí)行時(shí)長(zhǎng)(毫秒)',
            `ip`        varchar(64)   DEFAULT NULL COMMENT 'IP地址',
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=2897 DEFAULT CHARSET=utf8 COMMENT='用戶操作日志';
    </update>
</mapper>

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

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

相關(guān)文章

最新評(píng)論