Mybatis註解開發之動態SQL通過類方法註解
阿新 • • 發佈:2018-12-17
之前是xml和直接在mapper方法上面寫sql,發現太麻煩了,然後上網查了一下發現個好玩的方法,就是通過類的方法來註解開發(才開始學習寫部落格,有問題煩請多擔待)後續將繼續更新,更新完畢後將取消這段說明文字
1.寫對應的動態sql的類和相應方法(這裡我因為馬虎了,踩了大半天的坑,後來同事幫我看的時候我才注意到我哪裡寫錯了,學習還是要仔細點啊!!!我踩得坑就在new SQL(){}裡面,注意喲,new SQL(){ 這裡已經有一個{了,但是下一行還有一個{哦,這樣才對,可以用idea跟下原始碼,因為INSERT_INTO(TABLENAME)是在靜態程式碼塊裡面的,如果你發現你寫出來的INSERT_INTO(TABLENAME)需要讓你override這個方法的話那就是因為你少了對{},同樣,如果你在用userInfo資訊的時候用不了也是因為你沒把它放在靜態程式碼塊裡去!!!
package com.xuanyuan.demo.util.sql; import com.xuanyuan.demo.domain.UserInfo; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.jdbc.SQL; /** * @program: now * @description: 使用者操作相關動態sql類 * @author: Mr.Long * @create: 2018-10-26 11:24 **/ public class UserDynamicSqlProvider{ public String insertUserSql(UserInfo userInfo){ return new SQL(){ { INSERT_INTO("user_info"); if(userInfo.getUserName() != null && !"".equals(userInfo.getUserName())){ VALUES("userName","#{userName}"); } if (userInfo.getLoginName() != null && !"".equals(userInfo.getLoginName())) { VALUES("loginName", "#{loginName}"); } if (userInfo.getUserPassword() != null && !"".equals(userInfo.getUserPassword())) { VALUES("userPassword", "#{userPassword}"); } if (userInfo.getUserCode() != null && !"".equals(userInfo.getUserCode())) { VALUES("userCode", "#{userCode}"); } if (userInfo.getUserPhone() != null && !"".equals(userInfo.getUserPhone())) { VALUES("userPhone", "#{userPhone}"); } if (userInfo.getUserSex() != null && !"".equals(userInfo.getUserSex())) { VALUES("userSex", "#{userSex}"); } if (userInfo.getUserEmail() != null && !"".equals(userInfo.getUserEmail())) { VALUES("userEmail", "#{userEmail}"); } if (userInfo.getUserIDcard() != null && !"".equals(userInfo.getUserIDcard())) { VALUES("userIDcard", "#{userIDcard}"); } if (userInfo.getUserAge() != null && !"".equals(userInfo.getUserAge())) { VALUES("userAge", "#{userAge}"); } } }.toString(); } }
2.mapper介面的方法上加(記得type裡面要寫成類名.class哦,防止新手踩坑,method就是對應的拼接sql的方法)
@XXXProvider(type = XXXClass.class,method = "XXXmethod")
package com.xuanyuan.demo.dao; import com.xuanyuan.demo.domain.UserInfo; import com.xuanyuan.demo.util.sql.UserDynamicSqlProvider; import org.apache.ibatis.annotations.*; @Mapper public interface UserMapper { /** * 註冊 * @param userInfo 註冊人資訊 * @return */ @InsertProvider(type = UserDynamicSqlProvider.class,method = "insertUserSql") @Options(useGeneratedKeys = true,keyColumn = "userId",keyProperty = "userId") Integer insert(UserInfo userInfo); }
PS:直接寫註解的話是這樣的:
在其他部落格主那裡看到寫JdbcType的作用如下:
MyBatis 插入空值時,需要指定JdbcType mybatis insert空值報空值異常,但是在pl/sql不會提示錯誤,主要原因是mybatis無法進行轉換,
@Insert("insert into user_info(userName,loginName,userPassword,userAge,userSex,userCode,userPhone,userIDcard,userEmail) value(#{userName,jdbcType=VARCHAR},#{loginName,jdbcType=VARCHAR},#{userPassword,jdbcType=VARCHAR},#{userAge,jdbcType=INTEGER},#{userSex,jdbcType=VARCHAR},#{userCode,jdbcType=VARCHAR},#{userPhone,jdbcType=VARCHAR},#{userIDcard,jdbcType=VARCHAR},#{userEmail,jdbcType=VARCHAR})")