Spring?Boot實現(xiàn)MyBatis動態(tài)創(chuàng)建表的操作語句
在有些應用場景中,我們會有需要動態(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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot?使用定時任務(SpringTask)的詳細步驟
Cron?表達式非常靈活,可以滿足各種定時任務的需求,但需要注意的是,Cron?表達式只能表示固定的時間點,無法處理復雜的時間邏輯,本文給大家介紹SpringBoot?使用定時任務(SpringTask)的詳細步驟,感興趣的朋友一起看看吧2024-02-02使用@Builder導致無法創(chuàng)建無參構造方法的解決
這篇文章主要介紹了使用@Builder導致無法創(chuàng)建無參構造方法的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12Spring MVC 中 短信驗證碼功能的實現(xiàn)方法
短信驗證功能在各個網(wǎng)站應用都非常廣泛,那么在springmvc中如何實現(xiàn)短信驗證碼功能呢?今天小編抽時間給大家介紹下Spring MVC 中 短信驗證碼功能的實現(xiàn)方法,一起看看吧2016-09-09Java多線程的其他知識_動力節(jié)點Java學院整理
這篇文章主要介紹了Java多線程的其他知識,需要的朋友可以參考下2017-05-05java 記錄一個子串在整串中出現(xiàn)的次數(shù)實例
今天小編就為大家分享一篇java 記錄一個子串在整串中出現(xiàn)的次數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Spring的連接數(shù)據(jù)庫以及JDBC模板(實例講解)
下面小編就為大家?guī)硪黄猄pring的連接數(shù)據(jù)庫以及JDBC模板(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10