其他分享
首页 > 其他分享> > mybatis 一对一,一对多,多对多映射开发

mybatis 一对一,一对多,多对多映射开发

作者:互联网

目录

一、一对一映射查询

例:一个订单对应一个用户。
对应的sql语句:select * from orders o,user u where o.uid=u.id;

1、实体类

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;
}

2、mapper接口,OrderMapper

public interface OrderMapper {
    List<Order> findAll();
}

3、mapper配置,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:

<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、使用注解配置 mapper

public interface OrderMapper {
    @Select("select * from orders")
    @Results({
        @Result(id=true, property="id", column="id"),
        @Result(property="ordertime", column="ordertime"),
        @Result(property="total", column="total"),
        @Result(property="user", column="uid", javaType=User.class, 
            one=@One(select="com.lagou.mapper.UserMapper.findById"))
    })
    List<Order> findAll();
}
public interface UserMapper {
    @Select("select * from user where id=#{id}")
    User findById(int id);
}

5、结果测试

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

二、一对多映射查询

对应的sql语句:select *,o.id oid from user u left join orders o on u.id=o.uid;

1、实体类

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;
}

2、mapper接口,UserMapper

public interface UserMapper {
    List<User> findAllUserAndOrder();
}

3、mapper配置,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="findAllUserAndOrder" resultMap="userMap">
        select *,o.id oid from user u left join orders o on u.id=o.uid
    </select>
</mapper>

4、使用注解配置 mapper

public interface UserMapper {
    @Select("select * from user")
    @Results({
        @Result(id=true, property="id", column="id"),
        @Result(property="username", column="username"),
        @Result(property="password", column="password"),
        @Result(property="birthday", column="birthday"),
        @Result(property="orderList", column="id", javaType=List.class, 
            many=@Many(select = "com.lagou.mapper.OrderMapper.findByUid"))
    })
    List<User> findAllUserAndOrder;
}
public interface OrderMapper {
    @Select("select * from orders where uid=#{uid}")
    List<Order> findByUid(int uid);
}

5、结果测试

List<User> all = userMapper.findAllUserAndOrder();
for(User user : all){
    System.out.println(user.getUsername());
    List<Order> orderList = user.getOrderList();
    for(Order order : orderList){
        System.out.println(order);
    }
    System.out.println("-----------------------------");
}

三、多对多映射开发

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

1、实体类

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

2、添加mapper接口方法,UserMapper

public interface UserMapper {
    List<User> findAllUserAndRole();
}

3、mapper配置,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>

4、使用注解配置 mapper

public interface UserMapper {
    @Select("select * from user")
    @Results({
        @Result(id=true, property="id", column="id"),
        @Result(property="username", column="username"),
        @Result(property="password", column="password"),
        @Result(property="birthday", column="birthday"),
        @Result(property="roleList", column="id", javaType=List.class, 
            many=@Many(select="com.lagou.mapper.RoleMapper.findByUid"))
    })
    List<User> findAllUserAndRole();
}
public interface RoleMapper {
    @Select("select * from Role r, user_role ur where r.id=ur.role_id and ur.user_id=#{id}")
    List<Role> findByUid(int uid);
}

5、结果测试

UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> all = mapper.findAllUserAndRole();
for(User user : all){
    System.out.println(user.getUsername());
    List<Role> roleList = user.getRoleList();
    for(Role role : roleList){
        System.out.println(role);
    }
    System.out.println("----------------------------------");
}

标签:mapper,映射,一对一,List,private,public,user,mybatis,id
来源: https://blog.csdn.net/jason559/article/details/120362032