一一映射 借助@One
@One就相当于我们使用xml的时候的 asscciation
//@Results 等价于 resultMap
// @Result 相当于 result和id的组合
@Results(
id = "userMap",//这个就是唯一标识,后来直接使用这个标识就可以了
value = {
//主键id才有写true 不是逐渐的id就不要用写了
@Result ( id = true , property = "userId",column = "id"),
@Result ( property = "userName",column = "username"),
@Result ( property = "userAge",column = "age"),
@Result ( property = "userAddress",column = "address"),
@Result ( property = "userGender",column = "gender"),
@Result ( property = "userRid",column = "u_rid"),
@Result ( property = "userDid",column = "u_did"),
@Result( property = "department",//这是user类里面的department属性
column = "u_did",//这个是外键
one = @One(select = "com.zhiyou100.dao.IDepartmentDao.queryDepartmentById")//这个是一一映射该路径下的这个接口的方法
)
}
)
一对多映射 借助@Many
@Many 就相当于我们使用xml开发的collection
@Results(
id = "departmentMap",
value = {
@Result(id = true,column = "d_id",property = "id"),
@Result(column = "d_name",property = "departmentName"),
@Result(column = "d_desc",property = "departmentDesc"),
@Result(column = "d_update",property = "departmentUpdate"),
@Result(
column = "d_id",//这是外键
property = "user",//这是pojo类里面的的映射
many = @Many(select = "com.zhiyou100.dao.IUserDao.queryUserByDid")//这是我们要映射泪下接口内部的方法
)
}
)
使用注解实现二级缓存 @CacheNamespace()
是写在接口上面的
@CacheNamespace(blocking = true)//true表示开启二级缓存
public interface IDepartmentDao {
使用注解实现延时加载 借助于fetchType来实现
@Results(
id = "userMap",//这个就是唯一标识,后来直接使用这个标识就可以了
value = {
//主键id才有写true 不是逐渐的id就不要用写了
@Result ( id = true , property = "userId",column = "id"),
@Result ( property = "userName",column = "username"),
@Result ( property = "userAge",column = "age"),
@Result ( property = "userAddress",column = "address"),
@Result ( property = "userGender",column = "gender"),
@Result ( property = "userRid",column = "u_rid"),
@Result ( property = "userDid",column = "u_did"),
@Result( property = "department",//这是user类里面的department属性
column = "u_did",//这个是外键
//fetchType = FetchType.LAZY Lazy就是开启懒加载 EAGER就是直接加载
one = @One(select = "com.zhiyou100.dao.IDepartmentDao.queryDepartmentById",fetchType = FetchType.LAZY)//这个是一一映射该路径下的这个接口的方法
)
}
)