Mybatis嵌套一对一或一对多关系映射 [英] Mybatis nested one-to-one or one-to-many relations mapping

查看:139
本文介绍了Mybatis嵌套一对一或一对多关系映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用myBatis映射一个简单的数据库(例如).

I use myBatis to map a simple database (as an example).

它包含4种模型:用户汽车关税保险.

It consists of 4 models: User, Car, Tariff, Insurance.

用户具有私有列表carList 私有关税以及其他带有获取器和设置器的字段.

User has private List carList and private Tariff tariff and some other fields with getters and setters.

汽车具有私人保险和其他一些带有获取器和设置器的字段.

Car has private Insurance insurance and some other fields with getters and setters.

所以我只能映射第一个嵌套级别.我的意思是我可以映射用户及其字段-关税汽车列表.但是我无法映射汽车保险字段.我该怎么办?

So I can map only 1st nesting level. I mean i can map User and its fields - Tariff and a List of Cars. But I can't map Insurance field of Car. What should I do?

这是我的mapper.xml:

Here is my mapper.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 = "UserNamespace">
    <resultMap id="resultUser" type="User">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <association property="tariff" column="tariff" select="getTariff" javaType="Tariff"/>
        <collection property="carList" column="id" select="getCars" javaType="ArrayList" ofType="Car">
            <id property="id" column="id"/>
            <result property="model" column="model"/>
            <association property="insurance" column="insurance" select="getInsurance" javaType="Insurance"/>
        </collection>
    </resultMap>

    <select id = "getAll" resultMap = "resultUser">
        SELECT * FROM carwashservice.users
    </select>

    <select id = "getTariff" parameterType="int" resultType="Tariff">
        SELECT tariffs.description FROM carwashservice.tariffs WHERE tariffs.id = #{id}
    </select>

    <select id = "getCars" parameterType="int" resultType="Car">
        SELECT * FROM carwashservice.cars WHERE cars.user = #{id}
    </select>

    <select id = "getInsurance" parameterType="int" resultType="Insurance">
        SELECT * FROM carwashservice.insurance WHERE insurance.id = #{insurance}
    </select>
</mapper>

还有我的数据库:

CREATE DATABASE  IF NOT EXISTS `carwashservice` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `carwashservice`;

DROP TABLE IF EXISTS `cars`;
CREATE TABLE `cars` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `model` VARCHAR(45) NOT NULL,
  `user` INT(11) DEFAULT NULL,
  `insurance` INT(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user_idx` (`user`),
  KEY `insurance_idx` (`insurance`),
  CONSTRAINT `user` FOREIGN KEY (`user`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `insurance`;
CREATE TABLE `insurance` (
  `id` INT(11) NOT NULL,
  `cost` VARCHAR(45) NOT NULL,
  `exp_date` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`),
  CONSTRAINT `id` FOREIGN KEY (`id`) REFERENCES `cars` (`insurance`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `tariffs`;
CREATE TABLE `tariffs` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `description` VARCHAR(45) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `id` (`id`,`description`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `id` INT(11) NOT NULL,
  `name` VARCHAR(45) NOT NULL,
  `tariff` INT(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `tariff_idx` (`tariff`),
  KEY `id` (`id`,`name`),
  CONSTRAINT `tariff` FOREIGN KEY (`tariff`) REFERENCES `tariffs` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

推荐答案

您应该提取汽车映射: <collection property="carList" column="id" select="getCars" javaType="ArrayList" ofType="Car" />

You should extract the car mapping: <collection property="carList" column="id" select="getCars" javaType="ArrayList" ofType="Car" />

在单独的resultMap中: <resultMap id="resultCar" type="Car"> <id property="id" column="id"/> <result property="model" column="model"/> <association property="insurance" column="insurance" select="getInsurance" javaType="Insurance"/> </resultMap>

In a separated resultMap: <resultMap id="resultCar" type="Car"> <id property="id" column="id"/> <result property="model" column="model"/> <association property="insurance" column="insurance" select="getInsurance" javaType="Insurance"/> </resultMap>

并从语句中引用它 <select id = "getCars" parameterType="int" resultMap="resultCar">

您正在使用resultType="Car".这对于基本映射来说很好,但是与保险有关联:这不是基本的,需要特定的映射.

You are using resultType="Car". This is fine for basic mapping, but there is an association with Insurance: this is not basic and require specific mapping.

此外, getCars 语句使用其自己的resultMap,然后实际上会忽略您在 carList 集合中定义的内容(超出范围).这就是为什么保险清单为空的原因.

Furthermore, the getCars statement uses its own resultMap, then what you define inside carList collection is actually ignored (out of scope). that's why the insurance list is null.

这篇关于Mybatis嵌套一对一或一对多关系映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆