更新時間:2022-03-23 11:00:40 來源:動力節(jié)點 瀏覽4258次
映射器
int dropExistTable(@Param("tableName") String tableName);//自動創(chuàng)建數(shù)據(jù)表
映射文件
<update id="dropExistTable" parameterType="string" statementType="STATEMENT">
CREATE TABLE ${tableName} (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(100) DEFAULT NULL,
`password ` varchar(100) DEFAULT NULL,
`create_time` 時間戳 NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
</update>
控制器
@RequestMapping("/createtable")
@ResponseBody
public String createTable(String tableName){
Map<String, Object> reMap = new HashMap<String, Object>();
int res = userService.dropExistTable(tableName);
if (res == 0) {
reMap.put("code",1);
reMap.put("msg"," 創(chuàng)建成功");
logger.info("'tableNameString'+ 創(chuàng)建成功");
}else{
reMap.put("code",-1);
reMap.put("msg"," 創(chuàng)建失敗");
logger.info("創(chuàng)建失敗");
}
返回 JSONUtil。獲取JSON(重新映射);
}
添加屬性 statementType="STATEMENT"
同時將sql中因變量的值改為${xxxx},而不是#{xxx}
statementType:STATEMENT(未預編譯),PREPARED(預編譯)或CALLABLE中的任何一種,告訴你MyBatis單獨使用Statement,PreparedStatement或者CallableStatement。默認:準備。這里明顯沒有預編譯,要改成非預編譯..
${xxxx}:$直接在sql中生成傳入的數(shù)據(jù),對于字符串數(shù)據(jù),需要手動加引號
List<User> selectTable(@Param("tableName") String tableName);
項目中 User 表為空,有動態(tài)創(chuàng)建的表結構,字段一致
映射文件 *mapper.xml
<select id="selectTable" parameterType="java.lang.String" resultMap="BaseResultMap" statementType="STATEMENT">
select * from ${tableName}
</選擇>
控制器
@RequestMapping("/selectTable")
public String showSelectTable(String tableName){
List<User> objects = userService.selecTable(tableName);
System.out.println("user::::"+objects);
logger.info("成功");
返回 JSONUtil.getJSON(對象);
}
#和$符號的區(qū)別
#{}代表占位符符號,可以實現(xiàn)preparedStatement給占位符設置值,自動加粗樣式java類型和jdbc類型轉(zhuǎn)換。#{}可以有效防止sql注入。#{}可以接受簡單類型值或 pojo 屬性值。如果 parameterType 傳遞一個單一的簡單類型值,#{} 可以是括號中的 value 或者其他一些名字。
${ } 表示拼接sql strand,通過 ${} 可以是 parameterType 傳入的內(nèi)容在 sql 中拼接出來的 jdbc 類型轉(zhuǎn)換,可以接受簡單類型值或者 pojo 屬性值,如果 parameterType 傳遞單個簡單類型值,{可以接受簡單類型值或者 pojo 屬性值,如果 parameterType 傳遞單個簡單類型值,可以接受簡單類型值或者 pojo 屬性值,如果 parameterType 傳遞單個簡單類型值,{} 只能是括號內(nèi)的值.
注意:關于 order by 下面的排序規(guī)則,表名等數(shù)據(jù)庫對象名,如果要傳入,還應該使用${……}
int insertTable(@Param("user")User user,@Param("tableName") String tableName);
insertTable() 方法中有兩個參數(shù),一個參數(shù)是 tabelName ,另一個參數(shù)是 User object ,因此 <insert> 標簽中不能有...的參數(shù)Type參數(shù),而是使用@Param(“”)來修飾,注意,
@Param 是 import org.apache.ibatis.annotations.Param;,不要誤導包。
映射器.xml
<insert id="insertTable" >
插入 ${tableName}(名稱、密碼、create_time)
值(#{user.name}、#{user.password}、#{user.createTime})
</插入>
控制器實現(xiàn)方法
@RequestMapping("/addTable")
@ResponseBody
public String addTable( String name,String password,String tableName){
Map<String, Object> reMap = new HashMap<String, Object>();
用戶用戶=新用戶();
user.setName(name);
user.setPassword(密碼);
user.setCreateTime(new Date());
System.out.println("傳入用戶參數(shù)"+user);
int res = userService.insertTable(user,tableName);
if (res > 0) {
reMap.put("code",1);
reMap.put("msg"," 成功 ");
logger.info("'tableNameString'+ 成功");
}else{
reMap.put("code",-1);
重新映射.put("
}
返回 JSONUtil.getJSON(reMap);
}
通過以上介紹相信大家對動態(tài)MyBatis創(chuàng)建表的方法已經(jīng)有所了解,大家如果對此比較感興趣,想了解更多相關知識,不妨來關注一下動力節(jié)點的Mybatis-Plus視頻教程,里面的課程內(nèi)容由淺到深,通俗易懂,適合沒有基礎的小伙伴學習,希望對大家能夠有所幫助。