1.1 @Results
@Results注解來映射查詢結果集到實體類屬性
(1)@Results的基本用法。當數據庫字段名與實體類對應的屬性名不一致時,可以使用@Results映射來將其對應起來。
column為數據庫字段名,porperty為實體類屬性名,jdbcType為數據庫字段數據類型,id為是否為主鍵。
@Select("select * from user") @Results(value = { @Result(id = true, column = "id", property = "userId",jdbcType = JdbcType.INTEGER), @Result(column = "username", property = "userName",jdbcType = JdbcType.VARCHAR), @Result(column = "birthday", property = "userBirthday",jdbcType = JdbcType.DATE), @Result(column = "sex", property = "userSex",jdbcType = JdbcType.VARCHAR), @Result(column = "address", property = "userAddress",jdbcType = JdbcType.VARCHAR) }) List<User> findAll();
如上所示的數據庫字段名id與實體類屬性名userId,就通過這種方式建立了映射關系。名字相同的可以省略。
1.2 @ResultMap
(2)@ResultMap的用法。當這段@Results代碼需要在多個方法用到時,為了提高代碼復用性,我們可以為這個@Results注解設置id,然后使用@ResultMap注解來復用這段代碼。
@Select("select * from user") @Results(id = "userMap",value = { @Result(id = true, column = "id", property = "userId",jdbcType = JdbcType.INTEGER), @Result(column = "username", property = "userName",jdbcType = JdbcType.VARCHAR), @Result(column = "birthday", property = "userBirthday",jdbcType = JdbcType.DATE), @Result(column = "sex", property = "userSex",jdbcType = JdbcType.VARCHAR), @Result(column = "address", property = "userAddress",jdbcType = JdbcType.VARCHAR) }) List<User> findAll();
用@ResultMap()注解來引用
@Select("select * from user where id=#{id}") @ResultMap(value = {"userMap"}) User findById(Integer id);
1.3 @One
(3)@One的用法。當我們需要通過查詢到的一個字段值作為參數,去執行另外一個方法來查詢關聯的內容,而且兩者是一對一關系時,可以使用@One注解來便捷的實現。
一對一:一般都用立即加載
一對多:一般都用懶加載/延遲加載
fetchType = FetchType.EAGER //設置為立即加載
package com.jh.dao; import com.jh.entity.Account; import org.apache.ibatis.annotations.One; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.mapping.FetchType; import java.util.List; public interface AccountDao { /** * 查詢所有賬戶,並且獲取每個賬戶所屬的用戶信息 * * @return */ @Select("select * from account") @Results(id = "accountMap", value = { @Result(id = true, property = "id", column = "id"), @Result(property = "uid", column = "uid"), @Result(property = "money", column = "money"), @Result(property = "user", column = "uid", one = @One(select = "com.jh.dao.UserDao.findById", fetchType = FetchType.EAGER)) }) List<Account> findAll(); }
package com.jh.dao; import com.jh.entity.User; import org.apache.ibatis.annotations.*; import org.apache.ibatis.type.JdbcType; import java.util.List; public interface UserDao { @Select("select * from user") @Results(id = "userMap", value = { @Result(id = true, column = "id", property = "userId", jdbcType = JdbcType.INTEGER), @Result(column = "username", property = "userName", jdbcType = JdbcType.VARCHAR), @Result(column = "birthday", property = "userBirthday", jdbcType = JdbcType.DATE), @Result(column = "sex", property = "userSex", jdbcType = JdbcType.VARCHAR), @Result(column = "address", property = "userAddress", jdbcType = JdbcType.VARCHAR) }) List<User> findAll(); @Select("select * from user where id=#{id}") @ResultMap(value = {"userMap"}) User findById(Integer id); } }
1.4 @Many
(4)@Many的用法。與@One類似,只不過如果使用@One查詢到的結果是多行,會拋出TooManyResultException異常,這種時候應該使用的是@Many注解,實現一對多的查詢
package com.jh.dao; import com.jh.entity.User; import org.apache.ibatis.annotations.*; import org.apache.ibatis.mapping.FetchType; import org.apache.ibatis.type.JdbcType; import java.util.List; public interface UserDao { @Select("select * from user") @Results(id = "userMap", value = { @Result(id = true, column = "id", property = "userId", jdbcType = JdbcType.INTEGER), @Result(column = "username", property = "userName", jdbcType = JdbcType.VARCHAR), @Result(column = "birthday", property = "userBirthday", jdbcType = JdbcType.DATE), @Result(column = "sex", property = "userSex", jdbcType = JdbcType.VARCHAR), @Result(column = "address", property = "userAddress", jdbcType = JdbcType.VARCHAR), @Result(column = "id", property = "accounts",many = @Many(select = "com.jh.dao.AccountDao.findById",fetchType = FetchType.LAZY)) }) List<User> findAll(); } }
package com.jh.dao; import com.jh.entity.Account; import org.apache.ibatis.annotations.One; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.mapping.FetchType; import java.util.List; public interface AccountDao {/** * 根據用戶id查詢賬戶信息 * @param id * @return */ @Select("select * from account where uid=#{uid}") List<Account> findById(Integer id); }
1.5 @CacheNamespace
@CacheNamespace(blocking = true):寫在接口名上面,注解方式開啟二級緩存
參考博客:https://blog.csdn.net/cherlshall/article/details/80950150