0


猿创征文|Mybatis的注解实现复杂映射开发

Mybatis的注解实现复杂映射开发

实现复杂关系映射之前哦我们可以在映射文件中通过配置<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<Order> 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<Order> 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<Order> 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<User> findUserAndOrderAll();

orderMapper加上


   @Select("select *from orders where uid=#{uid}")
public List<Order> findById(int id);

user实体类中加上order实体类的信息

public class User {
    private int id;
    private String username;
    private String password;
    private Date birthday;
private List<Order> 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<User> 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<Role> 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<User> 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<Role> findByUid(int uid);
}

test类下的测试下

public class MybatisTest2 {

private OrderMapper oMapper;

    @Test
    public void testAll(){
        List<Order> all = oMapper.findAll();
        for (Order order : all) {
            System.out.println(order);
        }

    
}

运行结果

标签: mybatis java mysql

本文转载自: https://blog.csdn.net/weixin_60719453/article/details/126628649
版权归原作者 执久呀 所有, 如有侵权,请联系我们删除。

“猿创征文|Mybatis的注解实现复杂映射开发”的评论:

还没有评论