博客
关于我
Mybatis-多表查询(注解)
阅读量:99 次
发布时间:2019-02-26

本文共 3818 字,大约阅读时间需要 12 分钟。

项目目录

一对一查询

一对一查询语句

一对一查询通常涉及通过外键关联两个表。例如,订单表和用户表的关系,可以通过订单的外键oid来查询对应的用户信息。

代码示例

OrderMapper接口

@Select("select * from orders")
@Results({
@Result(id = true, column = "id", property = "id"),
@Result(column = "ordertime", property = "ordertime"),
@Result(column = "total", property = "total"),
@Result(column = "oid", property = "oid"),
@Result(property = "user", javaType = User.class, column = "oid",
one = @One(select = "cn.guardwhy.dao.UserMapper.findById", fetchType = FetchType.EAGER))
})
public interface OrderMapper {
void findAllWithUser();
}

UserMapper接口

@Select("select * from user where id = #{id}")
@Results({
@Result(column = "id", property = "id", id = true),
@Result(column = "user_name", property = "username"),
@Result(column = "birthday", property = "birthday"),
@Result(column = "sex", property = "sex"),
@Result(column = "address", property = "address")
})
public interface UserMapper {
User findById(Integer id);
}

测试代码

// 查询一个订单,并同时查询该订单所属的用户
@Test
public void testOrderWithUser() {
SqlSession sqlSession = MybatisUtils.getSession();
OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
List
list = mapper.findAllWithUser();
for (Orders orders : list) {
System.out.println(orders);
}
sqlSession.close();
}

一对多查询

一对多查询语句

一对多查询涉及通过一张表的主键到多张表的关联。例如,用户表和订单表的关系,可以通过用户表的id查询所有关联的订单信息。

代码示例

UserMapper接口

@Select("select * from user")
@Results({
@Result(column = "id", property = "id", id = true),
@Result(column = "user_name", property = "username"),
@Result(column = "birthday", property = "birthday"),
@Result(column = "sex", property = "sex"),
@Result(column = "address", property = "address"),
@Result(property = "ordersList", javaType = List.class, column = "id",
many = @Many(select = "cn.guardwhy.dao.OrderMapper.findByOid"))
})
public interface UserMapper {
void findAllWithOrders();
}

OrderMapper接口

@Select("select * from orders where id = #{oid}")
public interface OrderMapper {
List
findByOid(Integer oid);
}

测试代码

// 查询一个用户,并同时查询该用户具有的订单
@Test
public void testUserWithOrders() {
SqlSession sqlSession = MybatisUtils.getSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List
userList = mapper.findAllWithOrders();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}

多对多查询

多对多查询语句

多对多查询涉及通过中间表(关联表)查询两个表之间的关系。例如,用户表和角色表的关系,可以通过中间表user_role查询用户的角色信息。

代码示例

UserMapper接口

@Select("select * from user")
@Results({
@Result(column = "id", property = "id", id = true),
@Result(column = "user_name", property = "username"),
@Result(column = "birthday", property = "birthday"),
@Result(column = "sex", property = "sex"),
@Result(column = "address", property = "address"),
@Result(property = "roleList", javaType = List.class, column = "id",
many = @Many(select = "cn.guardwhy.dao.RoleMapper.findByOid"))
})
public interface UserMapper {
void findAllWithRole();
}

RoleMapper接口

@Select("select * from role r inner join user_role ur on r.id = ur.user_id where ur.role_id = #{id}")
@Results({
@Result(id = true, column = "id", property = "id"),
@Result(column = "role_name", property = "roleName"),
@Result(column = "role_detail", property = "roleDetail")
})
public interface RoleMapper {
List
findByOid(Integer id);
}

测试代码

// 多对多查询测试
@Test
public void testUserWithRole() {
SqlSession sqlSession = MybatisUtils.getSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List
list = mapper.findAllWithRole();
for (User user : list) {
System.out.println(user);
}
sqlSession.close();
}

执行结果

(以下为示例执行结果图)

转载地址:http://dscz.baihongyu.com/

你可能感兴趣的文章
Objective-C实现factorial iterative阶乘迭代算法(附完整源码)
查看>>
Objective-C实现factorial recursive阶乘递归算法(附完整源码)
查看>>
Objective-C实现factorial阶乘算法(附完整源码)
查看>>
Objective-C实现factorial阶乘算法(附完整源码)
查看>>
Objective-C实现Factors因数算法(附完整源码)
查看>>
Objective-C实现Farey Approximation近似算法(附完整源码)
查看>>
Objective-C实现Fast Powering算法(附完整源码)
查看>>
Objective-C实现Fedwick树算法(附完整源码)
查看>>
Objective-C实现fenwick tree芬威克树算法(附完整源码)
查看>>
Objective-C实现FenwickTree芬威克树算法(附完整源码)
查看>>
Objective-C实现fermat little theorem费马小定理算法(附完整源码)
查看>>
Objective-C实现FermatPrimalityTest费马素数测试算法(附完整源码)
查看>>
Objective-C实现fft2函数功能(附完整源码)
查看>>
Objective-C实现FFT快速傅立叶变换算法(附完整源码)
查看>>
Objective-C实现FFT算法(附完整源码)
查看>>
Objective-C实现fibonacci search斐波那契查找算法(附完整源码)
查看>>
Objective-C实现fibonacci斐波那契算法(附完整源码)
查看>>
Objective-C实现fibonacci斐波那契算法(附完整源码)
查看>>
Objective-C实现FIFO(附完整源码)
查看>>
Objective-C实现FigurateNumber垛积数算法(附完整源码)
查看>>