mybatis 動態添加表,查看表,添加數據


1.動態添加表

mapper

 int dropExistTable(@Param("tableName") String tableName);//自動創建數據表

 

 映射文件

   <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` timestamp NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 </update>

 

controller

    @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","創建成功"); logger.info("'tableNameString'+創建成功"); }else{ reMap.put("code",-1); reMap.put("msg","創建失敗"); logger.info("創建失敗"); } return JSONUtil.getJSON(reMap); }

 

  • 添加屬性statementType="STATEMENT"
  • 同時sql里的屬有變量取值都改成${xxxx},而不是#{xxx}
  • statementType:STATEMENT(非預編譯),PREPARED(預編譯)或CALLABLE中的任意一個,這就告訴 MyBatis 分別使用Statement,PreparedStatement或者CallableStatement。默認:PREPARED。這里顯然不能使用預編譯,要改成非預編譯。。

  •  ${xxxx}:$將傳入的數據直接顯示生成在sql中,對於字符串數據,需要手動加上引號

2.動態查詢數據表

 List<User> selecTable(@Param("tableName") String tableName);

 

項目中User表是個空表,里面跟動態創建的表結構,字段一致

映射文件 *mapper.xml

<select id="selecTable"  parameterType="java.lang.String" resultMap="BaseResultMap" statementType="STATEMENT">
    select  *   from  ${tableName}
  </select>

controller

	@RequestMapping("/selecTable")
	public  String  showSelecTable(String tableName){
		List<User> objects = userService.selecTable(tableName);
		System.out.println("user::::"+objects);
		logger.info("成功");
		return JSONUtil.getJSON(objects);
	}

 

#和$符號的區別
#{ }表示一個占位符號, 可以實現preparedStatement向占位符中設置值,自動加粗樣式進行java類型和jdbc類型轉換。#{}可以有效防止sql注入。#{}可以接收簡單類型值或pojo屬性值。 如果parameterType傳輸單個簡單類型值,#{}括號中可以是value或其它名稱。

$ { } 表示拼接sql串,通過${}可以將parameterType 傳入的內容拼接在sql中且不進行jdbc類型轉換, 可以接收簡單類型值或pojo屬性值,如果parameterType傳輸單個簡單類型值, {}可以接收簡單類型值或pojo屬性值,如果parameterType傳輸單個簡單類型值,可以接收簡單類型值或pojo屬性值,如果parameterType傳輸單個簡單類型值,{}括號中只能是value。

注意:對於order by 后面的排序規則,表名等數據庫對象名,如果要傳入,也應該使用${……}

3.動態表數據添加 

 int insertTable(@Param("user")User user,@Param("tableName") String tableName);
insertTable() 方法中是兩個參數,一個參數是tabelName , 另一個參數 是User對象, 所以<insert > 標簽中不能有parameterType 參數, 而是使用 @Param(“”) 進行修飾, 使用的時候要注意, 
@Param  是 import org.apache.ibatis.annotations.Param;, 不要導錯包了。

mapper.xml

  <insert id="insertTable" >
  insert into ${tableName}   ( name, password,  create_time) 
  values (#{user.name}, #{user.password},  #{user.createTime})
  </insert>

 controller實現方法

@RequestMapping("/addTable")
	@ResponseBody
	public  String addTable( String name,String password,String tableName){
		Map<String, Object> reMap = new HashMap<String, Object>();
		User user= new User();
		user.setName(name);
		user.setPassword(password);
		user.setCreateTime(new Date());
		System.out.println("傳入的user參數"+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);
			reMap.put("msg","失敗");
			logger.info("失敗");
		}
		return JSONUtil.getJSON(reMap);
		
	}

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM