mybatis一对一&多对一配置详解

假装没事ソ 提交于 2020-03-10 20:32:28

一对一建表原则
        将任意一方当做主表(B是主表),A创建外键列指向B的一方的主键,A外键列唯一
        说白了多对一也是一对一的其中一种表现形式

java建立关系原则
        一方持有另外一方的引用
        
        举例子
                一个客户对应多个订单
                就客户而言,客户和订单是1对多的关系
                就订单而言,每个订单只能属于一个用户,所以订单和客户属于1对1的关系

代码实现(订单和客户的一对一关系)

步骤一:明确两个实体类关系,在订单的一方提供客户的引用

[Java] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

public class Customer {

    private Integer cid ;

    private String cname;

    //省略get/set/toString

}

/**

    一个订单属于一个客户,在订单方创建客户引用

    注意.客户引用的对象名字很重要

 */

public class Orders {

    private Integer oid ;

    private String orderName;

    private String cid;

    private Customer c;

    //省略get/set/toString

}  


步骤二:创建OrdersMapper接口和OrdersMapper.xml映射配置文件(具体解释看下图)

[Java] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

public interface OrdersMapper {

    List<Orders> findAllInfo();

}

 

<?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.b3a4a.maybatis.mapper.OrdersMapper">

 

    <resultMap id="order_customer" type="orders">

        <id property="oid" column="oid"/>

        <result property="orderName" column="oname" />

        <result property="cid" column="customer_id" />

        <association property="c" javaType="customer">

            <id property="cid" column="cid"/>

            <id property="cname" column="cname"/>

        </association>

    </resultMap>

    <select id="findAllInfo" resultMap="order_customer">

        SELECT customer.*,orders.oid,orders.oname,orders.cid AS customer_id

                FROM orders

                JOIN customer  ON customer.cid=orders.cid

    </select>

</mapper>






步骤三:编写测试用例

[Java] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

@Test

public void testFindAll() throws IOException {

    InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");

    SqlSession session = new SqlSessionFactoryBuilder().build(is).openSession();

    OrdersMapper mapper = session.getMapper(OrdersMapper.class);

    List<Orders> orders = mapper.findAllInfo();

    for (Orders order : orders) {

        System.out.println(order);

        System.out.println(order.getCustomer());

    }

    session.close();

    is.close();

}  


核心配置文件

[Java] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<?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>

    <properties resource="jdbcConfig.properties"></properties>

    <typeAliases>

        <package name="com.b3a4a.maybatis.domain" />

    </typeAliases>

    <environments default="mysql">

        <environment id="mysql">

            <transactionManager type="JDBC"></transactionManager>

            <dataSource type="POOLED">

                <property name="driver" value="${jdbc.driver}"/>

                <property name="url" value="${jdbc.url}"/>

                <property name="username" value="${jdbc.username}"/>

                <property name="password" value="${jdbc.password}"/>

            </dataSource>

        </environment>

    </environments>

    <mappers>

        <package name="com.b3a4a.maybatis.mapper"/>

    </mappers>

</configuration>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!