zoukankan      html  css  js  c++  java
  • Mybatis(三)Mybatis映射开发

    4.1 一对一

    4.1.1 表对应关系,

    一个订单对应一个用户

    4.1.2 实体对应关系

    public class Order {
     private int id;
     private Date ordertime;
     private double total;
    //代表当前订单属于哪个用户
     private User user; }
    public class User {
     
     private int id;
     private String username;
     private String password;
     private Date birthday; }
    

    4.1.3 配置 OrderMapper.xml文件

    <mapper namespace="com.lagou.mapper.OrderMapper">
     <resultMap id="orderMap" type="com.lagou.domain.Order">
         <result column="uid" property="user.id"></result>
         <result column="username" property="user.username"></result>
         <result column="password" property="user.password"></result>
         <result column="birthday" property="user.birthday"></result>
     </resultMap>
     <select id="findAll" resultMap="orderMap">
     		select * from orders o,user u where o.uid=u.id
     </select>
    </mapper>
    #或者还可以这样配置
    <resultMap id="orderMap" type="com.lagou.domain.Order">
       <result property="id" column="id"></result>
       <result property="ordertime" column="ordertime"></result>
       <result property="total" column="total"></result>
       <association property="user" javaType="com.lagou.domain.User">
         <result column="uid" property="id"></result>
         <result column="username" property="username"></result>
         <result column="password" property="password"></result>
         <result column="birthday" property="birthday"></result>
       </association>
    </resultMap>
    

    4.2 一对多

    4.2.1 表对应关系

    image.png
    查询语句可以写为

    select *,o.id oid from user u left join orders o on u.id=o.uid;

    4.2.2 实体类关系

    public class Order {
     private int id;
     private Date ordertime;
     private double total;
     //当前订单属于哪个用户
     private User user; 
     }
     
    public class User {
     private int id;
     private String username;
     private String password;
     private Date birthday;
    //当前用户的订单列表
     private List<Order> orderList; 
     }
    

    4.2.3 配置UserMapper.xml

    <mapper namespace="com.lagou.mapper.UserMapper">
     <resultMap id="userMap" type="com.lagou.domain.User">
         <result column="id" property="id"></result>
         <result column="username" property="username"></result>
         <result column="password" property="password"></result>
         <result column="birthday" property="birthday"></result>
         <collection property="orderList" ofType="com.lagou.domain.Order">
             <result column="oid" property="id"></result>
             <result column="ordertime" property="ordertime"></result>
             <result column="total" property="total"></result>
         </collection>
     </resultMap>
     <select id="findAll" resultMap="userMap">
    	 	select *,o.id oid from user u left join orders o on u.id=o.uid
     </select>
    </mapper>
    

    4.3 多对多

    4.3.1 表对应关系

    image.png
    对应SQL语句

    select u.,r.,r.id rid from user u left join user_role ur on u.id=ur.user_id

    inner join role r on ur.role_id=r.id;

    4.3.2 实体类对应关系

    public class User {
     private int id;
     private String username;
     private String password;
     private Date birthday;
    //当前用户具备哪些角色
     private List<Role> roleList; 
     }
     
    public class Role {
     private int id;
     private String rolename; 
     }
    

    4.3.3 配置UserMapper.xml文件

    <resultMap id="userRoleMap" type="com.lagou.domain.User">
       <result column="id" property="id"></result>
       <result column="username" property="username"></result>
       <result column="password" property="password"></result>
       <result column="birthday" property="birthday"></result>
       <collection property="roleList" ofType="com.lagou.domain.Role">
         <result column="rid" property="id"></result>
         <result column="rolename" property="rolename"></result>
       </collection>
    </resultMap> 
    <select id="findAllUserAndRole" resultMap="userRoleMap">
       select u.*,r.*,r.id rid 
       from user u 
       left join user_role ur on u.id=ur.user_id
       inner join role r on ur.role_id=r.id
    </select>
    
  • 相关阅读:
    SAP MM 事务代码MI31之思考
    SAP MM 预留单据里的Base date和Requirement date
    SAP中的BOPF(Business Object Processing Framework)
    四大机器学习编程语言对比:R、Python、MATLAB、Octave
    「压缩」会是机器学习的下一个杀手级应用吗?
    机器翻译论文
    机器翻译汇总
    SAP freelancer夫妻并不难!你也可以!
    自然语言处理(nlp)比计算机视觉(cv)发展缓慢,而且更难!
    2019-9-2-win10-uwp-布局
  • 原文地址:https://www.cnblogs.com/benjaming0321/p/12546839.html
Copyright © 2011-2022 走看看