亲宝软件园·资讯

展开

Mybatis(三)Mybatis映射开发

北柠java 人气:0

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 表对应关系


查询语句可以写为

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 表对应关系


对应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>




原文:https://www.cnblogs.com/benjaming0321/p/12546839.html

加载全部内容

相关教程
猜你喜欢
用户评论