设为首页 收藏本站
查看: 1035|回复: 0

[经验分享] mybatis generator 的复合查询

[复制链接]

尚未签到

发表于 2016-11-26 08:22:18 | 显示全部楼层 |阅读模式
    mybatis generator可以自动生成sqlmap文件和查询语句,但是只能完成单表的查询,自己改了下,可以实现复合查询,感觉能比自己写查询语句能方便一些吧。
下面就讲讲怎么改的,和主要区别。
先说下表结构,主表stockclothestbl,副表有很多,那其中一个colortbl表举例。
正常通过generator可以自动生成StockclothestblExample.java,ColortblExample.java和StockclothestblMapper.xml,ColortblMapper.xml,4个文件。StockclothestblExample.java和ColortblExample.java文件用于查询。新建StockclothesExample.java。用于包含2个查询对象。
public class StockclothesExample {

    public StockclothestblExample stockclothestblExample;

    public ColortblExample colortblExample;

    public StockclothestblExample getStockclothestblExample() {
        return stockclothestblExample;
    }

    public void setStockclothestblExample(StockclothestblExample stockclothestblExample) {
        this.stockclothestblExample = stockclothestblExample;
    }

    public ColortblExample getColortblExample() {
        return colortblExample;
    }

    public void setColortblExample(ColortblExample colortblExample) {
        this.colortblExample = colortblExample;
    }
}

然后开始修改StockclothestblMapper.xml和ColortblMapper.xml文件

StockclothestblMapper.xml 文件里,添加sql,仿照自动生成的单表查询。
  <!-- 联合查询 -->
  <select id="selectStockclothes" resultMap="selectStockclothesResult"  parameterType="cn.com.shopdepot.javaModel.custom.StockclothesExample">
    select
    <if test="stockclothestblExample.distinct" >
      distinct
    </if>
    <include refid="cn.com.shopdepot.javaClient.StockclothestblMapper.Base_Column_List" />
    ,
    <include refid="cn.com.shopdepot.javaClient.RegisterdepottblMapper.Base_Column_List" />
    ,
    <include refid="cn.com.shopdepot.javaClient.StyletblMapper.Base_Column_List" />
    ,
    <include refid="cn.com.shopdepot.javaClient.KindtblMapper.Base_Column_List" />
    ,
    <include refid="cn.com.shopdepot.javaClient.ClotheskindtblMapper.Base_Column_List" />
    ,
    <include refid="cn.com.shopdepot.javaClient.ColortblMapper.Base_Column_List" />
    ,
    <include refid="cn.com.shopdepot.javaClient.SizetblMapper.Base_Column_List"/>
    from stockclothestbl stockclothestbl
    left join registerdepottbl registerdepottbl on registerdepottbl.REGISTERDEPOT_SERNO=stockclothestbl.REGISTERDEPOT_SERNO
    left join styletbl styletbl on styletbl.STYLE=stockclothestbl.STYLE
    left join kindtbl kindtbl on kindtbl.KIND=stockclothestbl.KIND
    left join clotheskindtbl clotheskindtbl on clotheskindtbl.CLOTHESKIND_CODE=stockclothestbl.CLOTHESKIND_CODE
    left join colortbl colortbl on colortbl.COLOR_SERNO=stockclothestbl.COLOR_SERNO
    left join sizetbl sizetbl on sizetbl.SIZE_CODE=stockclothestbl.SIZE_CODE
    and styletbl.DEL_FLAG='0'
    and kindtbl.DEL_FLAG='0'
    and clotheskindtbl.DEL_FLAG='0'
    and colortbl.DEL_FLAG='0'
    and sizetbl.DEL_FLAG='0'
    <if test="_parameter != null" >
        <where>
            <trim prefix="(" suffix=")" prefixOverrides="and" >
                <include refid="cn.com.shopdepot.javaClient.StockclothestblMapper.Example_Where_Clause_Complex" />
                <include refid="cn.com.shopdepot.javaClient.ColortblMapper.Example_Where_Clause_Complex"/>
            </trim>
          </where>
    </if>
    <if test="stockclothestblExample.orderByClause != null" >
      order by ${stockclothestblExample.orderByClause}
    </if>
    <include refid="cn.com.shopdepot.javaClient.StockclothestblMapper.mysqlDialect_Complex" />
  </select>

解释下这个sql
id="selectStockclothes",是以后java端调用的方法名。
resultMap="selectStockclothesResult"  这个是返回结果,引用selectStockclothesResult,下面是selectStockclothesResult

  <resultMap type="cn.com.shopdepot.javaModel.custom.Stockclothes" id="selectStockclothesResult">
      <association property="stockclothestbl" resultMap="cn.com.shopdepot.javaClient.StockclothestblMapper.BaseResultMap"></association>
      <association property="registerdepottbl" resultMap="cn.com.shopdepot.javaClient.RegisterdepottblMapper.BaseResultMap"></association>
      <association property="styletbl" resultMap="cn.com.shopdepot.javaClient.StyletblMapper.BaseResultMap"></association>
      <association property="kindtbl" resultMap="cn.com.shopdepot.javaClient.KindtblMapper.BaseResultMap"></association>
      <association property="clotheskindtbl" resultMap="cn.com.shopdepot.javaClient.ClotheskindtblMapper.BaseResultMap"></association>
      <association property="colortbl" resultMap="cn.com.shopdepot.javaClient.ColortblMapper.BaseResultMap"></association>
      <association property="sizetbl" resultMap="cn.com.shopdepot.javaClient.SizetblMapper.BaseResultMap"></association>
  </resultMap>
其中   <association property="colortbl" resultMap="cn.com.shopdepot.javaClient.ColortblMapper.BaseResultMap"></association>
就是ColortblMapper.xml文件中最上边的命名空间+ColortblMapper.xml文件中BaseResultMap的id。

继续上边,parameterType="cn.com.shopdepot.javaModel.custom.StockclothesExample" 参数就是我们自己添加的StockclothesExample.java文件的全路径。
<if test="stockclothestblExample.distinct" >,这个地方要修改,原来自动生成的是<if test="distinct" >修改成
 <if test="stockclothestblExample.distinct" >。这样才能反射找到是stockclothestblExample对象里的distinct。
stockclothestblExample---是我们自己添加StockclothesExample类里的成员

 <include refid="cn.com.shopdepot.javaClient.ColortblMapper.Base_Column_List" />,这个同理resultMap的,查询sql的字段,colortbl表的字段。
下面修改的相对比较多
    <if test="_parameter != null" >
        <where>
            <trim prefix="(" suffix=")" prefixOverrides="and" >
                <include refid="cn.com.shopdepot.javaClient.StockclothestblMapper.Example_Where_Clause_Complex" />
                <include refid="cn.com.shopdepot.javaClient.ColortblMapper.Example_Where_Clause_Complex"/>
            </trim>
          </where>
    </if>
自动生成的单表查询的以前的下面
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>

添加了colortbl的查询 <include refid="cn.com.shopdepot.javaClient.ColortblMapper.Example_Where_Clause_Complex"/>

以前的refid="Example_Where_Clause"------变成Example_Where_Clause_Complex,Example_Where_Clause是自动生成的,
Example_Where_Clause_Complex是我自己添加的,仿照Example_Where_Clause。下面是2个表的Example_Where_Clause_Complex。
分别写在2个自己的xml中。
StockclothestblMapper.xml添加的Example_Where_Clause_Complex
  <sql id="Example_Where_Clause_Complex" >
      <foreach collection="stockclothestblExample.oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
        </if>
      </foreach>
  </sql>

StockclothestblMapper.xml自动生成的Example_Where_Clause
  <sql id="Example_Where_Clause" >
    <where >
      <foreach collection="oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
比较看下,红色的去修改的,去掉where和trim,因为2个要在最外层加,上边画的蓝色部分。
collection="oredCriteria"变成collection="stockclothestblExample.oredCriteria",因为你的指定到你的类的具体对象。

同上
ColortblMapper.xml 添加的Example_Where_Clause_Complex
  <sql id="Example_Where_Clause_Complex" >
     <foreach collection="colortblExample.oredCriteria" item="criteria" separator="or" >
       <if test="criteria.valid" >
           <foreach collection="criteria.criteria" item="criterion" >
             <choose >
               <when test="criterion.noValue" >
                 and ${criterion.condition}
               </when>
               <when test="criterion.singleValue" >
                 and ${criterion.condition} #{criterion.value}
               </when>
               <when test="criterion.betweenValue" >
                 and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
               </when>
               <when test="criterion.listValue" >
                 and ${criterion.condition}
                 <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                   #{listItem}
                 </foreach>
               </when>
             </choose>
           </foreach>
       </if>
     </foreach>
  </sql>
ColortblMapper.xml自动生成的Example_Where_Clause
  <sql id="Example_Where_Clause" >
    <where >
      <foreach collection="oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>


继续上边的解释
    <if test="stockclothestblExample.orderByClause != null" >
      order by ${stockclothestblExample.orderByClause}
    </if>
也是指定到具体对象的排序,绿色部分。

sqlmap基本就改完了,剩下没解释的,是分页limit,就不解释了,贴一下代码
  <sql id="mysqlDialect_Complex" >
    <if test="stockclothestblExample.limitStart > -1">
      limit ${stockclothestblExample.limitStart}, ${stockclothestblExample.limitEnd}
    </if> 
  </sql>

以上基本就完成了。
然后在自动生成的StockclothestblMapper.java,添加方法
    /**
     * sqlmap id=selectStockclothes
     * 
     * @author wang
     * @param example
     * @return
     */
    List<Stockclothes> selectStockclothes(StockclothesExample example);

这样就可以使用了。大概写下

        StockclothesExample example = new StockclothesExample();

        StockclothestblExample stockclothestblExample = new StockclothestblExample();
        StockclothestblExample.Criteria stockclothestbl_criteria = stockclothestblExample.createCriteria();

        ColortblExample colortblExample = new ColortblExample();
        ColortblExample.Criteria colortbl_criteria = colortblExample.createCriteria();
        if (null != selBrand && !"".equals(selBrand)) {
            stockclothestbl_criteria.andBrandLike("%" + selBrand + "%");
        }
        if (null != selColor && !"".equals(selColor)) {
            colortbl_criteria.andColorLike("%" + selColor + "%");
        }
        example.setStockclothestblExample(stockclothestblExample);
        example.setColortblExample(colortblExample);
        .....   中间省略.......
      List <Stockclothes>list=stockclothestblMapper.selectStockclothes(example)

Stockclothes类封装的genertor自动生成的tbl

结束!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-305655-1-1.html 上篇帖子: mybatis 自身关联 映射 下篇帖子: mybatis 解决join语句缓存刷新研究
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表