• mybatis一对多,多对一


    假设两张表

    person对order为一对多

    实体类 

    person

    package com.kerwin.mybatis.pojo;
    import java.util.List;
    public class Person {
        private int id;
        private String name;
        private List<Orders> orderList;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public List<Orders> getOrderList() {
            return orderList;
        }
        public void setOrderList(List<Orders> orderList) {
            this.orderList = orderList;
        }
        @Override
        public String toString() {
            return "Person [id=" + id + ", name=" + name + "]";
        }
        public Person() {
            super();
            // TODO Auto-generated constructor stub
        }
        public Person(int id, String name, List<Orders> orderList) {
            super();
            this.id = id;
            this.name = name;
            this.orderList = orderList;
        }
    }

    order

    package com.kerwin.mybatis.pojo;
    
    public class Orders {
    private int id;
    private double price;
    private Person person;
    public Person getPerson() {
    return person;
    }
    
    public void setPerson(Person person) {
    this.person = person;
    }
    
    public int getId() {
    return id;
    }
    
    public void setId(int id) {
    this.id = id;
    }
    
    public double getPrice() {
    return price;
    }
    
    public void setPrice(double price) {
    this.price = price;
    }
    
    @Override
    public String toString() {
    return "Orders [id=" + id + ", price=" + price + "]";
    }
    
    public Orders() {
    super();
    // TODO Auto-generated constructor stub
    }
    
    }

     PersonMapper.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper
      PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
    <mapper namespace="com.kerwin.mybatis.pojo.Person">
        <resultMap type="com.kerwin.mybatis.pojo.Person" id="personreSultMap">
            <id column="p_id" property="id"/>
            <result column="name" property="name"/>
            <collection property="orderList" ofType="com.kerwin.mybatis.pojo.Orders" column="pid">
                <id column="o_id" property="id"/>
                <result column="price" property="price"/>
            </collection>
        </resultMap>
        <select id="selectPersonFetchOrder" parameterType="int" resultMap="personreSultMap" >
            select p.*,o.* from person p,orders o where o.pid=p.p_id and p.p_id=#{id}
        </select> 
    </mapper>

    OrderMpper.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper
      PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.kerwin.mybatis.pojo.Orders">
        <resultMap type="com.kerwin.mybatis.pojo.Orders" id="OrdersResultMap">
            <id column="o_id" property="id"/>
            <result column="price" property="price"/>
            <association property="person" javaType="com.kerwin.mybatis.pojo.Person">
                <id column="p_id" property="id"/>
                <result column="name" property="name"/>
            </association>
        </resultMap>
        
        <select id="selectOrdersFetchPerson" resultMap="OrdersResultMap">
            select p.*,o.* from person p,orders o where o.pid=p.p_id and o.o_id=#{id} 
        </select>
    
    </mapper>

    sqlmapConfig.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
      PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    
    <typeAliases>
        <typeAlias type="com.kerwin.mybatis.pojo.Author" alias="Author"/>
    </typeAliases>
      <environments default="development">
        <environment id="development">
          <transactionManager type="JDBC"/>
          <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
            <property name="username" value="root"/>
            <property name="password" value="root"/>
          </dataSource>
        </environment>
      </environments>
      <mappers>
        <mapper resource="com/kerwin/mybatis/pojo/AuthorMapper.xml"/>
        <mapper resource="com/kerwin/mybatis/pojo/PostMapper.xml"/>
        <mapper resource="com/kerwin/mybatis/pojo/PersonMapper.xml"/>
        <mapper resource="com/kerwin/mybatis/pojo/OrdersMapper.xml"/>
      </mappers>
    </configuration>
  • 相关阅读:
    Spring事件机制
    设计模式(07)——设计原则(2)
    设计模式(06)——设计原则(1)
    使用Feign发送HTTP请求
    设计模式(05)——组合和继承
    设计模式(04):接口和抽象类
    设计模式(03):面向对象与面向过程的区别与联系
    设计模式(二):面向对象及其特性分析
    设计模式(一):学习大纲
    Java8日期时间——LocalDateTime的使用以及相互转换
  • 原文地址:https://www.cnblogs.com/dashuai01/p/5165574.html
Copyright © 2020-2023  润新知