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


      

    目录

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

     一对一注解查询

    查询用户时也查询出订单信息

     另一种,分两次查询

     一对多注解查询

    多对多查询


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

    实现复杂关系映射之前哦我们可以在映射文件中通过配置<resultMap>来实现,使用注解来发后,我们可以使用@Results注解,@Result注解,@Many注解组合完成复杂关系的

     一对一注解查询

    查询用户时也查询出订单信息

    order类下

    1. public class Order {
    2. private int id;
    3. private Date ordertime;
    4. private double total;
    5. //当前订单属于哪一个用户
    6. private User user;
    7. //get和set、toString方式省略
    8. }

    User类下 

    1. public class User {
    2. private int id;
    3. private String username;
    4. private String password;
    5. private Date birthday;
    6. //get和set、toString方式省略
    7. }

    OrderMapper接口下

    1. public interface OrderMapper {
    2. @Select("select *,o.id oid from orders o,user u where o.uid=u.id")
    3. //配置user的信息
    4. @Results({
    5. @Result(column = "oid",property = "id"),
    6. @Result(column = "ordertime",property = "ordertime"),
    7. @Result(column = "total",property = "total"),
    8. @Result(column = "uid",property = "user.id"),
    9. @Result(column = "username",property = "user.username"),
    10. @Result(column = "password",property = "user.password")
    11. })
    12. public List findAll();
    13. }

     测试类下

    1. public class MybatisTest2 {
    2. private OrderMapper oMapper;
    3. // 提前运行,抽取出来
    4. @Before
    5. public void before() throws IOException {
    6. InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
    7. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    8. SqlSession sqlSession = sqlSessionFactory.openSession(true);
    9. oMapper = sqlSession.getMapper(OrderMapper.class);
    10. }
    11. @Test
    12. public void testAll(){
    13. List all = oMapper.findAll();
    14. for (Order order : all) {
    15. System.out.println(order);
    16. }
    17. }
    18. }

    运行结果 

     另一种,分两次查询

    以下这种格式

    1. select *from orders
    2. select *from user where id=?

    orderMapper接口中 

    1. public interface OrderMapper {
    2. @Select("select * from orders")
    3. @Results({
    4. @Result(column = "id",property = "id"),
    5. @Result(column = "ordertime",property = "ordertime"),
    6. @Result(column = "total",property = "total"),
    7. @Result(
    8. property = "user", //要封装的属性名称
    9. column = "uid", //根据那个字段去查询user表的数据
    10. javaType = User.class, //要封装的实体类型
    11. //select属性 代表查询那个接口的方法获得数据
    12. one = @One(select = "com_mybatis_ann.mapper.UserMapper.findById")//这个是UserMapper接口中的方法根据id查询
    13. )
    14. })
    15. public List findAll();
    16. }

    运行结果也是一致的

     一对多注解查询

    用户表和订单表的关系为,一个用户有多个订单,一个订单只属于一个用户

    一对多查询的需求:查询一个用户,与此同时查询出该用户具有的订单

    在userMapper接口下

    1. @Select("select *from user")
    2. //给user封数据
    3. @Results({
    4. @Result(column = "id",property = "id"),
    5. @Result(column = "username",property = "username"),
    6. @Result(column = "password",property = "password"),
    7. //给order封数据
    8. @Result(
    9. property = "orderList",
    10. column = "id",
    11. javaType = List.class,
    12. //写对方的根据id查询的方法(先在orderMapper中写一个根据id查询的方法)
    13. many = @Many(select = "com_mybatis_ann.mapper.OrderMapper.findById")
    14. )
    15. })
    16. public List findUserAndOrderAll();

    orderMapper加上

    1. @Select("select *from orders where uid=#{uid}")
    2. public List findById(int id);

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

    1. public class User {
    2. private int id;
    3. private String username;
    4. private String password;
    5. private Date birthday;
    6. private List orderList;
    7. }

     test包下测试

    1. public class MybatisTest {
    2. private UserMapper mapper;
    3. // 提前运行,抽取出来
    4. @Before
    5. public void before() throws IOException {
    6. InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
    7. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    8. SqlSession sqlSession = sqlSessionFactory.openSession(true);
    9. mapper = sqlSession.getMapper(UserMapper.class);
    10. }
    11. @Test
    12. public void testById(){
    13. List userAndOrderAll = mapper.findUserAndOrderAll();
    14. for (User user : userAndOrderAll) {
    15. System.out.println(user);
    16. }
    17. }
    18. }

    运行结果 

    多对多查询

    用户表和角色表的关系为,一个用户有多个角色,一个角色被多个用户使用

    多对多查询的需求:查询用户同时查询出该用户的所有角色

     新建一个Role实体类

    1. public class Role {
    2. private int id;
    3. private String roleName;
    4. private String roleDesc;
    5. //get、set、toString省略
    6. }

    User实体类下

    1. public class User {
    2. private int id;
    3. private String username;
    4. private String password;
    5. private Date birthday;
    6. private List roleList;
    7. //get、set、toString省略
    8. }

    UserMapper接口下

    1. @Select("Select *from user")
    2. @Results({
    3. @Result(column = "id",property = "id"),
    4. @Result(column = "username",property = "username"),
    5. @Result(column = "password" ,property = "password"),
    6. @Result(
    7. property = "roleList",
    8. column = "id",
    9. javaType = List.class,//对次方法的全限定名
    10. many = @Many(select ="com_mybatis_ann.mapper.RoleMapper.findByUid" )
    11. )
    12. })
    13. public List findUserAndRoleAll();

     RoleMapper接口下

    1. public interface RoleMapper {
    2. @Select("select *from sys_user_role ur,sys_role r where ur.roleId=r.id and ur.userId=#{uid}")
    3. public List findByUid(int uid);
    4. }

     test类下的测试下

    1. public class MybatisTest2 {
    2. private OrderMapper oMapper;
    3. @Test
    4. public void testAll(){
    5. List all = oMapper.findAll();
    6. for (Order order : all) {
    7. System.out.println(order);
    8. }
    9. }

    运行结果

  • 相关阅读:
    VCP-DCV VMware vSphere,即将开课~想了解点击查看
    【计算机毕业设计】41.航空订票系统
    LED制造企业元亨光电牵手盘古信息,开启数字化转型新篇章
    python——第九天
    CDH6.3.2 详细介绍及使用
    Composer初次接触
    Windows 小工具之 Wifi 固定器
    谈谈什么是缓存穿透,缓存击穿,缓存雪崩?怎么解决?
    多寄存器内存访问指令的寻址方式
    【剪映专业版】17高质量视频如何导出
  • 原文地址:https://blog.csdn.net/weixin_60719453/article/details/126628649