mybatis 学习一

1、collection

resultMap中的collection 一个强大的标签,直接说应用场景。
实例1
1、实体类:

public class TaskPackage {
    private Integer id;

    private String packageName;

    private Long commitTime;

    private String version;

    private String mainClass;

    private Integer taskId;

    private String location;

    private String sparkVersion;

    private Integer expId;

    private String expName;

    private String malssVersion;

    private Integer deployType;

    private List<Env> envs;

可以看到,类里面有一个属性是envs,这个属性是另一个表中的查询结果,相当于每个TaskPackage 中都会对应一个list的env,如果查询10个package,如果是正常的方式,需要查询10+1此数据库才能查询出想要的结果,但是用collection 之后,一条查询就可以查询出结果。
TaskPackage 的mapper文件如下

<?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.suning.ssmp.dao.TaskPackageMapper">
    <resultMap id="BaseResultMap" type="com.suning.ssmp.domain.console.TaskPackage">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="package_name" jdbcType="VARCHAR" property="packageName" />
        <result column="commit_time" jdbcType="BIGINT" property="commitTime" />
        <result column="version" jdbcType="VARCHAR" property="version" />
        <result column="main_class" jdbcType="VARCHAR" property="mainClass" />
        <result column="task_id" jdbcType="INTEGER" property="taskId" />
        <result column="location" jdbcType="VARCHAR" property="location" />
        <result column="spark_version" jdbcType="VARCHAR" property="sparkVersion" />
        <result column="exp_id" jdbcType="INTEGER" property="expId" />
        <result column="exp_name" jdbcType="VARCHAR" property="expName" />
        <result column="malss_version" jdbcType="VARCHAR" property="malssVersion" />
        <result column="deploy_type" jdbcType="VARCHAR" property="deployType" />
    </resultMap>
    <resultMap id="TaskPackageInfo" type="com.suning.ssmp.domain.console.TaskPackage"
        extends="BaseResultMap">
        <collection property="envs" ofType="Env"
            column="id" select="com.suning.ssmp.dao.EnvMapper.selectEnvbyPackageId">
        </collection>
    </resultMap>
        <select id="selectTaskPackage" parameterType="com.suning.ssmp.domain.console.TaskPackage"
        resultMap="TaskPackageInfo">
        select
        <include refid="Base_Column_List" />
        from t_task_package
        <where>
            <if test='id != null  '> and env_type = #{id,jdbcType=INTEGER} </if>
            <if test='packageName != null  '> and package_name = #{packageName,jdbcType=VARCHAR} </if>
            <if test='version != null  '> and version = #{version,jdbcType=VARCHAR} </if>
            <if test='mainClass != null  '> and main_class = #{mainClass,jdbcType=VARCHAR} </if>
            <if test='taskId != null  '> and task_id = #{taskId,jdbcType=INTEGER} </if>
            <if test='location != null  '> and location = #{location,jdbcType=VARCHAR} </if>
            <if test='sparkVersion != null  '> and spark_version = #{sparkVersion,jdbcType=VARCHAR} </if>
            <if test='expId != null  '> and exp_id = #{expId,jdbcType=INTEGER} </if>
            <if test='expName != null  '> and exp_name = #{expName,jdbcType=VARCHAR} </if>
            <if test='malssVersion != null  '> and malss_version = #{malssVersion,jdbcType=VARCHAR} </if>
            <if test='deployType != null  '> and deploy_type = #{deployType,jdbcType=INTEGER} </if>
        </where>
    </select>
</mapper>

envmapper

    <select id="selectEnvbyPackageId" parameterType="com.suning.ssmp.domain.console.Env"
        resultMap="BaseResultMap">
        select
        v.id, v.env_type, v.idc_name,v.idc_code,v.core_url,
        v.jboss_ip,
        v.create_time
        from t_env v left join t_env_task k on v.id =
        k.env_id where
        k.task_package_id
        = #{id,jdbcType=INTEGER} ;
    </select>

需要注意的地方:
property=”envs”是TaskPackage 中list的属性名
ofType=”Env”是TaskPackage 中list中的实体类。
column=”id” 是TaskPackage 中的id属性,注意没必要是外键,只要和下面select语句中的条件一样就可以。
select=”com.suning.ssmp.dao.EnvMapper.selectEnvbyPackageId”是你需要查询的语句,查询结果直接被封装到了TaskPackage 的envs属性中,类似于as

<resultMap id="TaskPackageInfo" type="com.suning.ssmp.domain.console.TaskPackage"
        extends="BaseResultMap">
        <collection property="envs" ofType="Env"
            column="id" select="com.suning.ssmp.dao.EnvMapper.selectEnvbyPackageId">
        </collection>
    </resultMap>

猜你喜欢

转载自blog.csdn.net/wzqllwy/article/details/78539121