zoukankan      html  css  js  c++  java
  • Mybatis_一对多关联查询

    person表对应orders表,一个人能下好几个订单。查询一个人下的所有订单。

    有Orders类和Person类。

    在Person类中加上一句:

    private List<Orders> orderList;  并生成setter getter。

        <!--=======多表关联查询=======-->
        <!-- collection:集合的意思,property要和类中list名一样 ,ofType是集合中泛型也就是属性类型是什么-->
        <resultMap type="person" id="selectOrderByPersonIdRM">
            <id column = "person_id" property = "personId" />
            <result column = "name" property = "name" />
            <result column = "gender" property = "gender" />
            <result column = "person_addr" property = "personAddr" />
            <result column = "birthday" property = "birthday" />
            <collection property="orderList" ofType="xxx.x.model.Orders">
                <id column = "ORDER_ID" property = "orderId" />
                <result column = "PERSON_ID" property = "personId" />
                <result column = "TOTAL_PRICE" property = "totalPrice" />
                <result column = "ADDR" property = "addr" jdbcType="VARCHAR" />
            </collection>
        </resultMap>
        <select id = "selectOrderByPersonId" parameterType="int" resultMap="selectOrderByPersonIdRM">
            select * from person p, order o where p.PERSON_ID = o.person_id and p.PERSON_ID = #{id}
        </select>
        public void selectOrderByPersonId() {
            // 创建SqlSession
            SqlSession session = sessionFactory.openSession();
            try {
                Person person = session.selectOne("xxx.x.mapper.PersonTestMapper.deleteBatch", 1);
                System.out.println(person);
            } catch (Exception e) {
                e.printStackTrace();
                session.rollback();
            } finally {
                session.close();
            }
        }
  • 相关阅读:
    "bs".endsWith()的使用注意,别用错了
    国外测试专家BLOG地址
    手机测试地址
    校验日期格式{YYYYMM_DD的java代码
    汉字的字节计算情况不同
    AssertThat汇集
    maven常见仓库
    取消冒泡事件
    好看的绿色阴影按钮
    IComparer 继承接口排序 实例 可选择排序
  • 原文地址:https://www.cnblogs.com/lonske/p/9015806.html
Copyright © 2011-2022 走看看