我使用myBatis映射一个简单的数据库(作为例子)。
它由4个模型组成:用户,汽车,关税,保险。
用户具有私人列表carList 和私人关税费用以及其他一些包含getter和setter的字段。
汽车拥有私人保险以及其他一些有吸气剂和制定者的字段。
所以我只能映射第一个嵌套级别。我的意思是我可以映射用户及其字段 - 关税和汽车列表。但我无法映射汽车的保险字段。我该怎么办?
这是我的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>
我的DB:
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;
答案 0 :(得分:0)
你应该提取汽车地图:
<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 集合中定义的内容实际上被忽略(超出范围)。这就是保险清单为空的原因。