目录
实现复杂关系映射之前哦我们可以在映射文件中通过配置<resultMap>来实现,使用注解来发后,我们可以使用@Results注解,@Result注解,@Many注解组合完成复杂关系的
order类下
- public class Order {
-
- private int id;
- private Date ordertime;
- private double total;
-
- //当前订单属于哪一个用户
- private User user;
-
- //get和set、toString方式省略
- }
User类下
- public class User {
- private int id;
- private String username;
- private String password;
- private Date birthday;
- //get和set、toString方式省略
- }
OrderMapper接口下
- public interface OrderMapper {
- @Select("select *,o.id oid from orders o,user u where o.uid=u.id")
- //配置user的信息
- @Results({
- @Result(column = "oid",property = "id"),
- @Result(column = "ordertime",property = "ordertime"),
- @Result(column = "total",property = "total"),
- @Result(column = "uid",property = "user.id"),
- @Result(column = "username",property = "user.username"),
- @Result(column = "password",property = "user.password")
- })
- public List
findAll(); - }
测试类下
-
-
- public class MybatisTest2 {
-
- private OrderMapper oMapper;
- // 提前运行,抽取出来
- @Before
- public void before() throws IOException {
- InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
- SqlSession sqlSession = sqlSessionFactory.openSession(true);
-
- oMapper = sqlSession.getMapper(OrderMapper.class);
- }
-
- @Test
- public void testAll(){
- List
all = oMapper.findAll(); - for (Order order : all) {
- System.out.println(order);
- }
-
- }
- }
运行结果
以下这种格式
- select *from orders
- select *from user where id=?
orderMapper接口中
-
- public interface OrderMapper {
- @Select("select * from orders")
- @Results({
- @Result(column = "id",property = "id"),
- @Result(column = "ordertime",property = "ordertime"),
- @Result(column = "total",property = "total"),
- @Result(
- property = "user", //要封装的属性名称
- column = "uid", //根据那个字段去查询user表的数据
- javaType = User.class, //要封装的实体类型
- //select属性 代表查询那个接口的方法获得数据
- one = @One(select = "com_mybatis_ann.mapper.UserMapper.findById")//这个是UserMapper接口中的方法根据id查询
- )
- })
- public List
findAll(); - }
运行结果也是一致的
用户表和订单表的关系为,一个用户有多个订单,一个订单只属于一个用户
一对多查询的需求:查询一个用户,与此同时查询出该用户具有的订单
在userMapper接口下
-
- @Select("select *from user")
- //给user封数据
- @Results({
- @Result(column = "id",property = "id"),
- @Result(column = "username",property = "username"),
- @Result(column = "password",property = "password"),
- //给order封数据
- @Result(
- property = "orderList",
- column = "id",
- javaType = List.class,
- //写对方的根据id查询的方法(先在orderMapper中写一个根据id查询的方法)
- many = @Many(select = "com_mybatis_ann.mapper.OrderMapper.findById")
- )
- })
- public List
findUserAndOrderAll();
orderMapper加上
-
- @Select("select *from orders where uid=#{uid}")
- public List
findById(int id);
user实体类中加上order实体类的信息
- public class User {
- private int id;
- private String username;
- private String password;
- private Date birthday;
- private List
orderList; - }
test包下测试
- public class MybatisTest {
- private UserMapper mapper;
-
- // 提前运行,抽取出来
- @Before
- public void before() throws IOException {
- InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
- SqlSession sqlSession = sqlSessionFactory.openSession(true);
- mapper = sqlSession.getMapper(UserMapper.class);
- }
-
- @Test
- public void testById(){
- List
userAndOrderAll = mapper.findUserAndOrderAll(); - for (User user : userAndOrderAll) {
- System.out.println(user);
- }
-
- }
- }
运行结果
用户表和角色表的关系为,一个用户有多个角色,一个角色被多个用户使用
多对多查询的需求:查询用户同时查询出该用户的所有角色
新建一个Role实体类
- public class Role {
-
- private int id;
- private String roleName;
- private String roleDesc;
- //get、set、toString省略
-
- }
User实体类下
- public class User {
- private int id;
- private String username;
- private String password;
- private Date birthday;
- private List
roleList; - //get、set、toString省略
- }
UserMapper接口下
- @Select("Select *from user")
- @Results({
- @Result(column = "id",property = "id"),
- @Result(column = "username",property = "username"),
- @Result(column = "password" ,property = "password"),
- @Result(
- property = "roleList",
- column = "id",
- javaType = List.class,//对次方法的全限定名
- many = @Many(select ="com_mybatis_ann.mapper.RoleMapper.findByUid" )
- )
- })
- public List
findUserAndRoleAll();
RoleMapper接口下
- public interface RoleMapper {
- @Select("select *from sys_user_role ur,sys_role r where ur.roleId=r.id and ur.userId=#{uid}")
- public List
findByUid(int uid); - }
test类下的测试下
- public class MybatisTest2 {
-
- private OrderMapper oMapper;
-
-
- @Test
- public void testAll(){
- List
all = oMapper.findAll(); - for (Order order : all) {
- System.out.println(order);
- }
-
-
- }
运行结果