实体类
1、ChargeInfoEntity
车辆充电记录实体类
包含另一个实体类
CarInfoEntity
的属性
package com.zhuang.car.entity;
import com.baomidou.mybatisplus.annotation.*;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
/**
* 车辆充电记录
*
* @author mrzhuang
* @email 862627527@qq.com
* @date 2022-07-22 22:46:04
*/
@Data
@TableName("charge_info")
public class ChargeInfoEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
*
*/
@TableId
private Integer id;
/**
* 车辆id
*/
private Integer carId;
/*
* 车辆信息实体
* */
private CarInfoEntity carInfo;
/**
* 充电开始时间
*/
@TableField(fill = FieldFill.INSERT)
private Date startTime;
/**
* 充电结束时间
*/
@TableField(fill = FieldFill.UPDATE)
private Date endTime;
}
2、CarInfoEntity
车辆信息实体类
package com.zhuang.car.entity;
import com.baomidou.mybatisplus.annotation.*;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
/**
* 车辆信息表
*
* @author mrzhuang
* @email 862627527@qq.com
* @date 2022-07-22 22:46:04
*/
@Data
@TableName("car_info")
public class CarInfoEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 车辆id
*/
@TableId
private Integer id;
/**
* 车牌号
*/
private String carNumber;
/**
* 车型id
*/
private Integer carTypeId;
/**
* 所属行业Id
*/
private Integer industryId;
/**
* 所属省份id
*/
private Integer princeId;
/**
* 使用状态,0不使用,1正在使用
*/
private Integer used;
/**
* 0不充电,1充电
*/
private Integer chargeStatus;
/**
* 电池寿命
*/
private Integer chargeLife;
/**
* 告警状态,0告警,1不告警
*/
private Integer alter;
/**
* 逻辑删除
*/
@TableLogic
private Integer is_deleted;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private Date createTime;
/**
* 修改时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
}
需求:
联合查询
charge_info
与car_info
表,返回车辆充电记录信息
Dao层
ChargeInfoDao
/**
* 车辆充电记录
*
* @author mrzhuang
* @email 862627527@qq.com
* @date 2022-07-22 22:46:04
*/
@Mapper
public interface ChargeInfoDao extends BaseMapper<ChargeInfoEntity> {
/**
*返回所有车辆充电信息
* @return
*/
List<ChargeInfoEntity> listChargeInfoAll();
}
xml文件配置
原始的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.zhuang.car.dao.ChargeInfoDao">
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="com.zhuang.car.entity.ChargeInfoEntity" id="chargeInfoMap">
<result property="id" column="id"/>
<result property="carId" column="car_id"/>
<result property="startTime" column="start_time"/>
<result property="endTime" column="end_time"/>
</resultMap>
</mapper>
方法:
方法一:使用association
<?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.zhuang.car.dao.ChargeInfoDao">
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="com.zhuang.car.entity.ChargeInfoEntity" id="chargeInfoMap">
<result property="id" column="id"/>
<result property="carId" column="car_id"/>
<result property="startTime" column="start_time"/>
<result property="endTime" column="end_time"/>
</resultMap>
<!--sql语句-->
<select id="listAll" resultMap="chargeInfoMaps">
select *
from charge_info charge
left join car_info car
on charge.car_id = car.id;
</select>
<!--第一种方法:使用associationn-->
<resultMap id="chargeInfoMaps" extends="chargeInfoMap" type="com.zhuang.car.entity.ChargeInfoEntity">
<association property="carInfo" javaType="com.zhuang.car.entity.CarInfoEntity">
<id property="id" column="id"/>
<result property="carNumber" column="car_number"/>
<result property="carTypeId" column="car_type_id"/>
<result property="industryId" column="industry_id"/>
<result property="princeId" column="prince_id"/>
<result property="used" column="used"/>
<result property="chargeStatus" column="charge_status"/>
<result property="chargeLife" column="charge_life"/>
<result property="alter" column="alter"/>
<result property="is_deleted" column="is_deleted"/>
<result property="createTime" column="create_time"/>
<result property="updateTime" column="update_time"/>
</association>
</resultMap>
</mapper>
方法二:使用级联
<!--第二种方法:使用级联-->
<resultMap id="chargeInfoMaps" extends="chargeInfoMap" type="com.zhuang.car.entity.ChargeInfoEntity">
<id property="carInfo.id" column="id"/>
<result property="carInfo.carNumber" column="car_number"/>
<result property="carInfo.carTypeId" column="car_type_id"/>
<result property="carInfo.industryId" column="industry_id"/>
<result property="carInfo.princeId" column="prince_id"/>
<result property="carInfo.used" column="used"/>
<result property="carInfo.chargeStatus" column="charge_status"/>
<result property="carInfo.chargeLife" column="charge_life"/>
<result property="carInfo.alter" column="alter"/>
<result property="carInfo.is_deleted" column="is_deleted"/>
<result property="carInfo.createTime" column="create_time"/>
<result property="carInfo.updateTime" column="update_time"/>
</resultMap>
方法三:分布查询
Dao层
ChargeInfoDao
/**
* 车辆充电记录
*
* @author mrzhuang
* @email 862627527@qq.com
* @date 2022-07-22 22:46:04
*/
@Mapper
public interface ChargeInfoDao extends BaseMapper<ChargeInfoEntity> {
/**
*返回所有车辆充电信息
* @return
*/
List<ChargeInfoEntity> listChargeInfoAll();
CarInfoDao
/**
* 车辆信息表
*
* @author mrzhuang
* @email 862627527@qq.com
* @date 2022-07-22 22:46:04
*/
@Mapper
public interface CarInfoDao extends BaseMapper<CarInfoEntity> {
/**
* 返回所有车辆信息
* @return
*/
List<CarInfoEntity> listCarInfoAll();
/**
* 根据Id查询车辆信息
* @param id
* @return
*/
CarInfoEntity getCarInfoById(@Param("id") Integer id);
}
xml文件配置
CarInfoDao.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.zhuang.car.dao.CarInfoDao">
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="com.zhuang.car.entity.CarInfoEntity" id="carInfoMap">
<result property="id" column="id"/>
<result property="carNumber" column="car_number"/>
<result property="carTypeId" column="car_type_id"/>
<result property="industryId" column="industry_id"/>
<result property="princeId" column="prince_id"/>
<result property="used" column="used"/>
<result property="chargeStatus" column="charge_status"/>
<result property="chargeLife" column="charge_life"/>
<result property="alter" column="alter"/>
<result property="isDeleted" column="is_deleted"/>
<result property="createTime" column="create_time"/>
<result property="updateTime" column="update_time"/>
</resultMap>
<select id="getCarInfoById" resultMap="carInfoMap">
select * from car_info where id = #{id};
</select>
<select id="listCarInfoAll" resultMap="carInfoMap">
select * from car_info;
</select>
</mapper>
ChargeInfoDao.xml
注意:ChargeInfoDao.xml文件中column的值必须car_info表的主键id;select中的方法必须是按照id查询的方法。如果是按照车牌号查询的话,返回的信息中的carInfo属性为null。如果使用查询全部记录的方法,则会报异常!!!
<!--第三种:分布查询-->
<!--第一步:查询所有电池信息表-->
<select id="listChargeInfoAll" resultMap="chargeInfoMaps">
select *
from charge_info;
</select>
<resultMap id="chargeInfoMaps" extends="chargeInfoMap" type="com.zhuang.car.entity.ChargeInfoEntity">
<!--第二步:查询车辆信息表 selec-->
<association property="carInfo"
select="com.zhuang.car.dao.CarInfoDao.getCarInfoById"
column="id">
</association>
</resultMap>
```