15.使用注解实现复杂映射查询数据
1.使用注解实现复杂映射开发
1.1一对一查询—两表查询
1.1.1代码实现
(1)OrderMapper接口
- @One代替了标签,使用时需要Result(one = @One(select = “namespace.id”) 表示。
- javaType:表示返回的对象类型。
@Select(" select * from orders ")
@Results({
@Result( id = true, property ="id", column = "id" ),
@Result( property ="id", column = "id" ),
@Result( property ="orderTime", column = "orderTime" ),
@Result( property ="total", column = "total" ),
@Result( property ="uid", column = "uid" ),
@Result(one = @One(select = "com.weihong.mapper.UserMapper.findUserById") ,property = "user" , javaType = com.weihong.domain.User.class , column = "uid")
})
List<Orders> findAllWithUser();
- 上面语句等价于使用xml映射文件的sql,如下代码所示:
<resultMap id="userOrdersMapper" type="orders">
<id property="id" column="id"></id>
<result property="orderTime" column="orderTime"></result>
<result property="total" column="total"></result>
<result property="uid" column="uid"></result>
<association property="user" javaType="com.lagou.domain.User" select="com.lagou.mapper.UserMapper.findUserOrderByNested" column="uid">
</association>
</resultMap>
<select id="findUserOrders" resultMap="userOrdersMapper">
select * from orders
</select>
(2)UserMapper接口
@Select(" select * from `user` where id = #{userId} ")
User findUserById(@Param("userId") Integer id);
(3)测试代码
@Test
public void test2(){
OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);
List<Orders> orders = ordersMapper.findAllWithUser();
for (Orders order : orders) {
System.out.println(order);
}
}
(4)测试结果
1.2一对多查询—》两表查询
- 需求:查询一个用户,与此同时查询出该用户具有的订单
SELECT * FROM `user`;
SELECT * FROM orders where uid = #{用户id};
1.2.1代码实现
(1)UserMapper接口
特别注意:
- 此时查询为一对多查询,使用collection来关联查询,对应注解为@Many属性。
- 一个用户可能有多个订单,所以返回类型为List类型。javaType = List.class(这是与一对一查询不同的)
@Select("select * from `user` ")
@Results({
@Result(id = true, property = "id" , column="id"),
@Result(property = "birthday" , column = "birthday"),
@Result(property = "sex" , column = "sex"),
@Result(property = "address" , column = "address"),
@Result(column = "id" ,many = @Many(select = "com.weihong.mapper.OrdersMapper.findByUid"), property = "ordersList" , javaType = List.class )
})
List<User> findAllWithOrder();
(2)OrderMapper接口
@Select("select * from orders where uid = #{uid} ")
List<Orders> findByUid(@Param("uid") Integer uid);
(3)测试类
@Test
public void test3(){
UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
List<User> allUser = usermapper.findAllWithOrder();
for (User user : allUser) {
System.out.println(user);
}
}
(4)测试结果
1.3多对多查询
1.3代码实现
(1)UserMapper接口
@Select("select * from `user` ")
@Results({
@Result(id = true, property = "id", column = "id"),
@Result(property = "birthday", column = "birthday"),
@Result(property = "sex", column = "sex"),
@Result(property = "address", column = "address"),
@Result(column = "id" , many = @Many ( select = "com.weihong.mapper.RoleMapper.getUserRoleInfo" , fetchType = FetchType.EAGER ) , property = "roleList" ,javaType = List.class)
})
List<User> findUserofRole();
(2)RoleMapper接口
@Select(" select * from sys_role r left join sys_user_role ur on r.id = ur.roleid where ur.userid = #{id}")
List<Role> getUserRoleInfo(Integer id);
(3)测试类
@Test
public void test4(){
UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
List<User> allUser = usermapper.findUserofRole();
for (User user : allUser) {
System.out.println(user);
}
}
(4)测试结果
2.注解延迟加载
不管是一对一还是一对多 ,在注解配置中都有fetchType的属性
- fetchType = FetchType.LAZY 表示懒加载
- fetchType = FetchType.EAGER 表示立即加载
- fetchType = FetchType.DEFAULT 表示使用全局配置
3.小结
- 注解开发和xml配置优劣分析
1.注解开发和xml配置相比,从开发效率来说,注解编写更简单,效率更高。 2.从可维护性来说,注解如果要修改,必须修改源码,会导致维护成本增加。xml维护性更强。
|