溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MyBatis如何動態(tài)創(chuàng)建表

發(fā)布時間:2021-06-07 11:33:33 來源:億速云 閱讀:291 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關MyBatis如何動態(tài)創(chuàng)建表的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

項目中業(yè)務需求的不同,有時候我們需要動態(tài)操作數(shù)據(jù)表(如:動態(tài)建表、操作表字段等)。常見的我們會把日志、設備實時位置信息等存入數(shù)據(jù)表,并且以一定時間段生成一個表來存儲,log_201806、log_201807等。在這里我們用MyBatis實現(xiàn),會用到動態(tài)SQL。

動態(tài)SQL是Mybatis的強大特性之一,MyBatis在對sql語句進行預編譯之前,會對sql進行動態(tài)解析,解析為一個BoundSql對象,也是在此對動態(tài)sql進行處理。

在動態(tài)sql解析過程中,#{ }與${ }的效果是不一樣的:

#{ } 解析為一個JDBC預編譯語句(prepared statement)的參數(shù)標記符。

如以下sql語句:

select * from user where name = #{name};

會被解析為:

select * from user where name = ?;

可以看到#{ }被解析為一個參數(shù)占位符 ? 。

${ } 僅僅為一個純粹的String替換,在動態(tài)SQL解析階段將會進行變量替換。

如以下sql語句:

select * from user where name = ${name};

當我們傳遞參數(shù)“joanna”時,sql會解析為:

select * from user where name = “joanna”;

可以看到預編譯之前的sql語句已經不包含變量name了。

綜上所述,${ }的變量的替換階段是在動態(tài)SQL解析階段,而#{ } 的變量的替換是在DBMS中。

下面實現(xiàn)MyBatis動態(tài)創(chuàng)建表,判斷表是否存在,刪除表功能。

Mapper.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="xx.xxx.xx.mapper.OperateTableMapper" >
  <select id="existTable" parameterType="String" resultType="Integer"> 
    select count(*) 
    from information_schema.TABLES 
    where LCASE(table_name)=#{tableName} 
  </select>
  <update id="dropTable"> 
    DROP TABLE IF EXISTS ${tableName} 
  </update> 
  <update id="createNewTable" parameterType="String"> 
    CREATE TABLE ${tableName} (
     id bigint(20) NOT NULL AUTO_INCREMENT,
     entityId bigint(20) NOT NULL,
     dx double NOT NULL,
     dy double NOT NULL,
     dz double NOT NULL,
     ntype varchar(32) NOT NULL,
     gnssTime bigint(20) NOT NULL,
     speed float DEFAULT NULL,
     direction float DEFAULT NULL,
     attributes varchar(255) DEFAULT NULL,
     PRIMARY KEY (id)) 
  </update> 
  <insert id="insert" parameterType="xx.xxx.xx.po.Trackpoint">
    insert into ${tableName}
    (entityId,dx,dy,dz,ntype,gnssTime,speed,direction,attributes)
    values
    (#{trackpoint.entityid},
    #{trackpoint.dx},
    #{trackpoint.dy},
    #{trackpoint.dz},
    #{trackpoint.ntype},
    #{trackpoint.gnsstime},
    #{trackpoint.speed},
    #{trackpoint.direction},
    #{trackpoint.attributes})
  </insert>
</mapper>

Mapper.java

package xx.xxx.xx.mapper;

import org.apache.ibatis.annotations.Param;
import xx.xxx.xx.po.Trackpoint;
public interface OperateTableMapper {
  int existTable(String tableName);
  int dropTable(@Param("tableName")String tableName);
  int createNewTable(@Param("tableName")String tableName);
  int insert(@Param("tableName")String tableName,@Param("trackpoint")Trackpoint trackpoint);
}

感謝各位的閱讀!關于“MyBatis如何動態(tài)創(chuàng)建表”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI