Mybaits利用resultMap实现多对多查询

resultMap的定义
<mapper namespace="dancheng.mybatis.mapper.OrdersMapperCustom">
    <!-- 查询用户及购买的商品 -->
    <resultMap type="dancheng.mybatis.po.User" id="UserAndItemsResultMap">
        <!-- 用户信息 -->
        <id column="user_id" property="id"/>
        <result column="username" property="username"/>
        <result column="sex" property="sex"/>
        <result column="address" property="address"/>
        
        <!-- 订单信息
            一个用户对应多个订单使用collection映射
        -->
        <collection property="ordersList" ofType="dancheng.mybatis.po.Orders">
            <id column="id" property="id" />
            <result column="user_id" property="userId" />
            <result column="number" property="number" />
            <result column="createtime" property="createtime" />
            <result column="note" property="note" />
            <!-- 订单明细
            一个订单包括多个明细
             -->
             <collection property="orderdetails" ofType="dancheng.mybatis.po.Orderdetail">
                 <id column="id" property="id"/>
                 <result column="orders_id" property="ordersId"/>
                 <result column="items_id" property="itemsId"/>
                 <result column="items_num" property="itemsNum"/>
                  <!-- 商品信息
                     一个订单明细对应一个商品
                 -->
                 <association property="items" javaType="dancheng.mybatis.po.Items">
                     <id column="id" property="id"/>
                     <result column="items_name" property="name"/>
                     <result column="items_datail" property="detail"/>
                     <result column="items_price" property="price"/>
                 </association>
             </collection>
        </collection>
    </resultMap>

调用resultMap
    <!-- 查询用户及购买的商品信息,使用resultMap -->
    <select id="findUserAndItemsResultMap" resultMap="UserAndItemsResultMap">
        SELECT
        orders.*,
        user.username,
        user.sex,
        user.address,
        orderdetail.*,
        items.name items_name,
        items.detail items_datail,
        items.price items_price
        FROM orders,
        USER,
        orderdetail,
        items
        WHERE orders.user_id = user.id AND orderdetail.orders_id = orders.id AND
        orderdetail.items_id = items.id
    </select>
</mapper> 


mapper中的接口:
public List<User> findUserAndItemsResultMap() throws Exception;

Test:
public class OrdersMapperCustomTest {
    private SqlSessionFactory sqlSessionFactory;
    @Before
    public void setUp() throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    
    @Test
    public void testFindUserAndItemsResultMap() throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrdersMapperCustom ordersMapperCustom = sqlSession.getMapper(OrdersMapperCustom.class);
        
        List<User> userList = ordersMapperCustom.findUserAndItemsResultMap();
        
        System.out.println(userList);
    }

}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mybatis是一种流行的Java持久层框架,可以对数据库进行操作。在mybatis中,实现一对多关联查询可以使用两种方式:嵌套查询和嵌套结果映射。嵌套查询是指在主查询中执行子查询来获取关联对象的数据,而嵌套结果映射是指在主查询的结果映射中包含关联对象的映射。 对于嵌套查询,可以使用select标签在主查询中执行子查询。例如,假设有一个班级和学生的关联关系,可以使用如下的SQL语句进行一对多关联查询: ``` <select id="getBanjiWithStudents" resultType="Banji"> SELECT * FROM Banji WHERE id = #{id} </select> <select id="getStudentsByBanjiId" resultType="Student"> SELECT * FROM Student WHERE banji_id = #{id} </select> ``` 然后,在Mapper接口中定义对应的方法: ``` public interface BanjiMapper { Banji getBanjiWithStudents(int id); } ``` 在配置文件中进行映射: ``` <mapper namespace="com.example.BanjiMapper"> <select id="getBanjiWithStudents" resultType="Banji"> SELECT * FROM Banji WHERE id = #{id} </select> <select id="getStudentsByBanjiId" resultType="Student"> SELECT * FROM Student WHERE banji_id = #{id} </select> </mapper> ``` 然后可以通过调用`getBanjiWithStudents`方法来进行一对多关联查询。 对于嵌套结果映射,可以使用association和collection标签来进行配置。例如: ``` <resultMap id="banjiResultMap" type="Banji"> <id property="id" column="id"/> <result property="name" column="name"/> <collection property="students" ofType="Student"> <id property="id" column="student_id"/> <result property="name" column="student_name"/> </collection> </resultMap> ``` 然后,在Mapper接口中定义对应的方法: ``` public interface BanjiMapper { Banji getBanjiWithStudents(int id); } ``` 在配置文件中进行映射: ``` <mapper namespace="com.example.BanjiMapper"> <resultMap id="banjiResultMap" type="Banji"> <id property="id" column="id"/> <result property="name" column="name"/> <collection property="students" ofType="Student"> <id property="id" column="student_id"/> <result property="name" column="student_name"/> </collection> </resultMap> <select id="getBanjiWithStudents" resultMap="banjiResultMap"> SELECT b.id, b.name, s.id as student_id, s.name as student_name FROM Banji b LEFT JOIN Student s ON b.id = s.banji_id WHERE b.id = #{id} </select> </mapper> ``` 同样可以通过调用`getBanjiWithStudents`方法来进行一对多关联查询

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值