首页 > 代码库 > mybatis中的关联查询

mybatis中的关联查询

1》在实体映射层中引入关联对象

package com.jinglin.hotelsup.model;

import java.io.Serializable;

public class Goodsinfo implements Serializable{
       private Integer goodsid;
       private Integer companyid;
       private Integer goodstypeid;
       private Integer unitid;
       private String createuser;
       private String updateuser;
       private String commdityid;
       private String commdityname;
       private String describeit;
       private String createtime;
       private String updatetime;
       private String remark;
       private String ifdelete;
       //关联查询
       private Unit unit;
       private Goodstype goodstype;
       private Company company;
       
    public Unit getUnit() {
        return unit;
    }
    public void setUnit(Unit unit) {
        this.unit = unit;
    }
    public Goodstype getGoodstype() {
        return goodstype;
    }
    public void setGoodstype(Goodstype goodstype) {
        this.goodstype = goodstype;
    }
    public Company getCompany() {
        return company;
    }
    public void setCompany(Company company) {
        this.company = company;
    }
    public Integer getGoodsid() {
        return goodsid;
    }
    public void setGoodsid(Integer goodsid) {
        this.goodsid = goodsid;
    }
    public Integer getCompanyid() {
        return companyid;
    }
    public void setCompanyid(Integer companyid) {
        this.companyid = companyid;
    }
    public Integer getGoodstypeid() {
        return goodstypeid;
    }
    public void setGoodstypeid(Integer goodstypeid) {
        this.goodstypeid = goodstypeid;
    }
    public Integer getUnitid() {
        return unitid;
    }
    public void setUnitid(Integer unitid) {
        this.unitid = unitid;
    }
    public String getCreateuser() {
        return createuser;
    }
    public void setCreateuser(String createuser) {
        this.createuser = createuser;
    }
    public String getUpdateuser() {
        return updateuser;
    }
    public void setUpdateuser(String updateuser) {
        this.updateuser = updateuser;
    }
    public String getCommdityid() {
        return commdityid;
    }
    public void setCommdityid(String commdityid) {
        this.commdityid = commdityid;
    }
    public String getCommdityname() {
        return commdityname;
    }
    public void setCommdityname(String commdityname) {
        this.commdityname = commdityname;
    }
    public String getDescribeit() {
        return describeit;
    }
    public void setDescribeit(String describeit) {
        this.describeit = describeit;
    }
    public String getCreatetime() {
        return createtime;
    }
    public void setCreatetime(String createtime) {
        this.createtime = createtime;
    }
    public String getUpdatetime() {
        return updatetime;
    }
    public void setUpdatetime(String updatetime) {
        this.updatetime = updatetime;
    }
    public String getRemark() {
        return remark;
    }
    public void setRemark(String remark) {
        this.remark = remark;
    }
    public String getIfdelete() {
        return ifdelete;
    }
    public void setIfdelete(String ifdelete) {
        this.ifdelete = ifdelete;
    }      
}

2》在同目录下的GoodsinfoMapper下配置查询结果集

3》编写关联查询语句

<?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.jinglin.hotelsup.dao.imp.GoodsinfoMapper">
    <!-- 表示关联表的查询 -->
    <sql id="getsql">
        select t.* from 
          (Select rownum as rn,gs.*,
                gp.goodstypename,
                c.companyname,c.address,
                u.code
                From goodsinfo gs inner join goodstype gp
                on gs.goodstypeid = gp.goodstypeid
                inner join company c 
                on gs.companyid = c.companyid
                inner join unit u
                on gs.unitid = u.unitid
            where gs.ifdelete=N)t
    </sql>
    <select id="getmodel" resultMap="goodsinfo">
      <include refid="getsql"></include>
       where t.ifdelete=N and t.goodsid=#{id}
    </select>
    <!-- 配置结果集 -->
    <resultMap type="Goodsinfo" id="goodsinfo">
        <!-- 配置主键 -->
        <id column="goodsid" property="goodsid"></id>
        <result column="goodstypeid" property="goodstypeid"></result>
        <result column="companyid " property="companyid "></result>
        <result column="unitid" property="unitid"></result>
        <result column="createuser" property="createuser"></result>
        <result column="updateuser" property="updateuser"></result>
        <result column=" commdityid" property="commdityid"></result>
        <result column="commdityname" property="commdityname"></result>
        <result column="describeit" property="describeit"></result>
        <result column="createtime" property="createtime"></result>
        <result column="updatetime" property="updatetime"></result>
        <result column="remark" property="remark"></result>
        <result column="ifdelete" property="ifdelete"></result>
        <!-- 配置关联实体数据 -->
        <association property="goodstype" javaType="Goodstype">
          <id property="goodstypeid" column="goodstypeid"></id>
          <result property="goodstypename" column="goodstypename"/>
          <result property="ifdel" column="ifdel"/>
        </association>
        <association property="company" javaType="Company">
          <id column="companyid" property="companyid"></id>
          <result column="companyname" property="companyname"/>
          <result column="address" property="address"/>
          <result column="ifdel" property="ifdel"></result>
        </association>
        <association property="unit" javaType="Unit">
          <id column="unitid" property="unitid"/>
          <result column="code" property="code"/>
          <result column="ifdel" property="ifdel"/>
        </association>
    </resultMap>
    
    <!-- 查询所有的数据 -->
    <select id="getlist" resultMap="goodsinfo">
        <include refid="getsql"></include>
          where t.ifdelete=N
    </select>
    
    <!-- 查询带分页和查询条件的 -->
    <select id="getgoodslistbypage" resultMap="goodsinfo">
        <include refid="getsql"></include>
         where t.ifdelete=N
         <if test="goodsinfo.goodsid!=null">
             and t.goodsid=#{goodsInfo.goodsid}
         </if>
         <if test="goodsinfo.commdityname!=null and goodsinfo.commdityname!=‘‘">
             and t.commdityname like %${goodsInfo.commdityname}%
         </if>
         <!-- 查询时间 -->
         <if test="goodsinfo.createtime!=null and goodsinfo.createtime!=‘‘">
             and t.createtime like %${goodsInfo.createtime}%
         </if>
          <![CDATA[
             and t.rn>=#{startindex}
             and t.rn<=#{endindex}
          ]]>
    </select>
    
    <!-- 数据的插入操作 -->
    <insert id="additem" useGeneratedKeys="true" keyColumn="goodsid"  keyProperty="goodsid" parameterType="GoodsInfo">
         insert into goodsinfo(goodsid,companyid,goodstypeid,unitid,
createuser,updateuser,commdityid,
commdityname,describeit,createtime,
updatetime,remark)
         values(userseq.nextval,#{companyid},#{goodstypeid},#{unitid},
#{createuser},#{updateuser},#{commdityid},
#{commdityname},#{describeit},#{createtime},
#{updatetime},#{remark})
    </insert>
    <!-- 数据的删除 -->
    <update id="deleteitem">
         update goodsinfo set ifdelete=Y where goodsid=#{id}
    </update>
    <!-- 数据的更新 -->
    <update id="updateitem" parameterType="Goodsinfo">
         update goodsinfo
         <set>
             <if test="companyid!=null">
                companyid=#{companyid},
             </if>
             <if test="goodstypeid!=null">
                goodstypeid=#{goodstypeid},
             </if>
             <if test="unitid!=null">
                unitid=#{unitid},
             </if>
             <if test="createuser!=null">
                createuser=#{createuser},
             </if>
             <if test="updateuser!=null">
                updateuser=#{updateuser},
             </if>
             <if test="commdityid!=null">
                commdityid=#{commdityid},
             </if>
             <if test="commdityname!=null">
                commdityname=#{commdityname},
             </if>
             <if test="describeit!=null">
                describeit=#{describeit},
             </if>
             <if test="createtime!=null">
                createtime=#{createtime},
             </if>
             <if test="updatetime!=null">
                updatetime=#{updatetime},
             </if>
             <if test="remark!=null">
                remark=#{remark},
             </if>
             <if test="ifdelete!=null">
                ifdelete=#{ifdelete}
             </if>
         </set>
            where goodsid=#{goodsid}
    </update>
</mapper>

ps:rownum as rn是为了做分页查询

          <![CDATA[
             and t.rn>=#{startindex}
             and t.rn<=#{endindex}
          ]]>
第一行代码和第四行是为了向程序解释>=和<=是逻辑表达式而不是标签

此为多对一的查询,若一对多,则配置结果集的标签为
<collection property="" ofType=""></collection>

mybatis中的关联查询