mybatis中怎么配置注解,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
mybatis中注解就是簡單不需要寫配置文件,適合簡單的數據處理,理解起來比較容易,不動態生成SQL時候可以用用。
需要綁定,有些時候不如配置文件,配置文件擴展強。 選擇合適的方式應用在合適的場景,注解主要應用于sql語句比較簡單容易理解的情況下可讀性高;生成動態sql時用xml配置文件要更簡潔,擴展性強
@CacheNamespace 類 <cache>
@CacheNamespaceRef 類 <cacheRef>
@Results 方法 <resultMap>
@Result 方法 <result> <id>
@One 方法 <association>
@Many 方法 <collection>
@select <select>
@Insert <insert>
@Update <update>
@Delete 方法 <delete>
@InsertProvider <insert> 允許創建動態SQL
@UpdateProvider <update> 允許創建動態SQL
@DeleteProvider <delete> 允許創建動態SQL
@SelectProvider <select> 允許創建動態SQL
@Param 參數 N/A 如果你的映射器的方法需要多個參數, 這個注解可以被應用于映射器的方法 參數來給每個參數一個名字。否則,多 參數將會以它們的順序位置來被命名 (不包括任何 RowBounds 參數) 比如。 #{param1} , #{param2} 等 , 這 是 默 認 的 。 使用 @Param(“person”),參數應該被命名為 #{person}。
@Options 方法 映射語句的屬性 這個注解提供訪問交換和配置選項的 寬廣范圍, 它們通常在映射語句上作為 屬性出現。 而不是將每條語句注解變復 雜,Options 注解提供連貫清晰的方式 來訪問它們
舉幾個比較典型和常用的
一對一關聯查詢
@Select("select * from authority")
@Results(id="au",
value=@Result(column="uid",
property="user",
one=@One(select="findUserByid",
fetchType=FetchType.LAZY)))
List<Authority> findAll();@Select里面填寫要查詢的主表的sql語句
@Results里面映射一個id="au"的返回結果集
value=@Result()表示某一屬性的映射關系
column為對應從表的外鍵名
property為主表實體類的從表實體類屬性名
one表示一對一映射
fetchType=FetchType.LAZY表示為惰性加載,當查詢的結構數據需要用到從表的數據才會調用select中的從表的查詢方法
select為關聯查詢的另一個從表的查詢方法
uid為select里的參數
findUserByid為mapper中定義的方法
@Select("select * from user where id = #{id}")
User findUserByid(int id);此方法可以在xml中配置也可以在本方法中用注解配置
<resultMap type="com.jt.mybatis.entity.Authority" id="au">
<association property="user" column="uid" javaType="com.jt.mybatis.entity.User" select="findByUserId">
</association>
</resultMap>
<select id="findAll" resultMap="au">
select * from authority
</select>
<select id="findUserByid" resultType="com.jt.mybatis.entity.User">
select * from user where id= #{id}
</select>測試方法
@Test
public void testA(){
AuthorityMapper mapper = session.getMapper(AuthorityMapper.class);
mapper.findAll().get(0).getUser();
}一對多關聯查詢
xml配置方式
<resultMap type="com.jt.mybatis.entity.User" id="user">
<id column="id" property="id" />
<collection property="authoritieList" column="id"
fetchType="lazy" select="findAuthorityByUid">
<id column="id" property="id" />
</collection>
</resultMap>
<select id="findUserByUserName" resultMap="user">
select * from user
where username = #{username}
</select>
<select id="findAuthorityByUid" resultType="com.jt.mybatis.entity.Authority">
select * from
authority where uid = #{uid}
</select>@Select("select * from user where username = #{username}")
@Results(id="user",
value=@Result(column="id",
property="authoritieList",
many=@Many(fetchType=FetchType.LAZY,
select="findAuthorityByUid")))
User findUserByUserName(String username);
@Select("select * from authority where uid = #{uid}")
List<Authority> findAuthorityByUid(int uid);many表示一對多映射
測試方法
@Test
public void testB(){
AuthorityMapper mapper = session.getMapper(AuthorityMapper.class);
mapper.findUserByUserName("admin").getAuthoritieList();
}動態sql
@SelectProvider(type=AuthorityProvider.class,method="returnSelectSQL")
List<Authority> findByIdAndUid(Authority authority);
class AuthorityProvider{
public String returnSelectSQL(Authority authority){
SQL sql = new SQL(){{
SELECT("*");
FROM("authority");
if(authority.getId() != 0){
WHERE("id = " + authority.getId());
}
if(authority.getUid() != 0){
WHERE("uid = " + authority.getUid());
}
}};
return sql.toString();
}
}
//用XXXProvider的注解是動態生成sql語句的,
//type=AuthorityProvider.class為生成動態語句的具體類
//method="returnSelectSQL"為生成動態語句的方法
//SQL類為動態生成sql的類測試方法
@Test
public void testC(){
AuthorityMapper mapper = session.getMapper(AuthorityMapper.class);
Authority authority = new Authority();
mapper.findByIdAndUid(authority);
//執行此語句返回的sql語句為DEBUG [main] - ==> Preparing: SELECT * FROM authority
authority.setId(1);
mapper.findByIdAndUid(authority);
//執行此語句返回的sql語句為DEBUG [main] - ==> Preparing: SELECT * FROM authority WHERE (id = 1)
authority.setUid(2);
mapper.findByIdAndUid(authority);
//執行此語句返回的sql語句為DEBUG [main] - ==> Preparing: SELECT * FROM authority WHERE (id = 1 AND uid = 2)
}增加了xml文件,修改麻煩,條件不確定(ifelse判斷),容易出錯,特殊轉義字符比如大于小于
復雜sql不好用,搜集sql不方便,管理不方便,修改需重新編譯
相同
都是對參數進行標記的符號
#是預編譯,防止sql注入
$ 相當于一個占位符,不能防止sql注入
看完上述內容,你們掌握mybatis中怎么配置注解的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。