Mybatis 嵌套一对一或一对多关系映射

2023-12-27

我使用 myBatis 来映射一个简单的数据库(作为示例)。

它由4个型号组成:User, Car, Tariff, 保险.

User has 私人列表 carList and 私人关税关税以及其他一些带有 getter 和 setter 的字段。

Car has 私人保险保险以及其他一些带有 getter 和 setter 的字段。

所以我只能映射第一个嵌套级别。我的意思是我可以绘制地图User及其领域 -Tariff和一个列表Cars。但我无法绘制地图保险现场Car。我应该怎么办?

这是我的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" />

在单独的 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"。这对于基本映射来说很好,但是与保险有关联:这不是基本的并且需要特定的映射。

此外,getCars语句使用它自己的resultMap,然后是你在里面定义的carList集合实际上被忽略(超出范围)。这就是保险清单为空的原因。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Mybatis 嵌套一对一或一对多关系映射 的相关文章

随机推荐