`

mybatis generator 的复合查询

阅读更多

  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 楼 yuchangcheng 2017-11-17  
直接把代码 上传上来吧  这样看的很吃力呀

相关推荐

    MybatisGenerator代码生成器(可查询指定字段)

    MybatisGenerator本身是没有提供查询指定字段的,例如数据库有5个字段,我想查询其中3个字段的全部记录,这是做不到的。经过研究MybatisGenerator实现原理,在保证原有功能的基础上,实现了生成查询指定字段的相关...

    mybatis generator

    Mybatis Generator是一款强大的工具,它为开发者自动化生成Mybatis的Mapper接口、XML映射文件以及实体类,大大减轻了在使用Mybatis时手动编写这些文件的工作负担。在Java开发环境中,ORM(Object-Relational Mapping)...

    mybatis generator Myeclipse插件

    文件名"mybatisgenerator_myeclipse10-sql-oracle"可能表示这是一个适用于Myeclipse 10版本的Mybatis Generator插件,并且重点支持Oracle数据库的配置和使用。"sql"可能代表SQL数据库的通用性,而"oracle"则强调了对...

    mybatis generator eclipse插件的安装

    MyBatis Generator(MBG)是一款强大的自动化代码生成工具,它可以极大地提高开发效率,通过配置文件自动生成MyBatis的映射文件、实体类以及DAO层的Java代码。Eclipse作为广泛使用的Java集成开发环境,提供了对MBG...

    mybatisgenerator.zip

    mybatis generator 是根据已创建的数据库数据表生成相映的 entity ,dao ,daoImpl ,sqlmap。 标签:mybatis

    mybatis generator mysql

    3. **运行MBG**:在Java代码中,通过调用`org.mybatis.generator.api.MyBatisGenerator`类的静态方法来执行生成过程。也可以通过命令行执行MBG,前提是配置文件和JAR文件在同一目录下。 4. **生成的代码**:MBG会...

    MyBatis Generator eclipse 插件 修改版【有中文注释】

    eclipse 插件,使用MyBatis Generator 可自动生成数据库对应的bean(有中文注释),mapper.xml和mapper dao 接口文件,可直接使用查询数据库,此插件在eclipse mars.2 版本下亲测可用,

    mybatis-generator-core-1.3.7-API文档-中文版.zip

    赠送jar包:mybatis-generator-core-1.3.7.jar; 赠送原API文档:mybatis-generator-core-1.3.7-javadoc.jar; 赠送源代码:mybatis-generator-core-1.3.7-sources.jar; 赠送Maven依赖信息文件:mybatis-generator-...

    eclipse mybatis generator插件及使用

    Eclipse MyBatis Generator插件是一款强大的自动化代码生成工具,它极大地简化了开发过程中与数据库交互的代码编写工作。MyBatis Generator可以帮助开发者自动生成Java实体类、Mapper接口及XML配置文件,从而节省了...

    eclipse Mybatis generator 1.3.7 中文注释 插件核心包

    1、这是Eclipse MyBatis generator 1.3.7插件的核心包 2、首先到Eclipse中下载 MyBatis Generator 1.3.7插件,下载完按步骤进行安装 打开Help &gt; Eclipse Marketplace &gt; Search &gt; 输入框输入 MyBatis Generator ...

    MyBatis Generator工具

    MyBatis Generator是一款强大的自动化工具,它极大地简化了在使用MyBatis框架时的数据访问层(DAL)代码编写工作。通过配置XML文件,Generator能够自动生成Java实体类、Mapper接口及实现类、XML配置文件等,从而极大地...

    mybatisGenerator-master

    MybatisGenerator是一款强大的Java工具,它能够帮助开发者自动地逆向工程数据库,生成Model、DAO(数据访问对象)以及Mapper接口和XML配置文件。这款工具极大地提升了开发效率,避免了手动编写这些基础代码的繁琐...

    MybatisGenerator.rar

    MybatisGenerator是一个强大的工具,主要用于简化Mybatis框架的模型、Mapper接口及XML配置文件的创建。这个工具基于Java,能够通过数据库元数据自动生成相关的Java源代码,极大地提高了开发效率,尤其是在处理大量的...

    mybatis中的generator工具

    MyBatis Generator(MBG)是MyBatis框架的一个实用工具,它允许用户自动生成Java源代码、XML配置文件以及数据库交互所需的SQL映射文件。这个工具极大地简化了开发过程,减少了手动编写这些文件的工作量,提高了开发...

    通过mybatis generator反向工程生成pojo及mapper类 带序列化插件

    MyBatis Generator是一款强大的自动化代码生成工具,它可以帮助开发者快速生成Java实体类(POJO)、Mapper接口和XML映射文件,极大地提高了开发效率。在本主题中,我们将深入探讨如何利用MyBatis Generator进行反向...

    mybatis_generator使用手册

    MyBatis Generator 使用手册 MyBatis Generator 是 MyBatis 的代码生成工具,旨在通过反射数据库表结构生成对应的Java代码,简化开发过程,提高开发效率。本手册将指导用户如何使用 MyBatis Generator 生成代码,...

    Mybatis Generator 代码生成工具

    Mybatis Generator 是一款强大的自动化代码生成工具,它能够极大提高开发效率,特别是在处理与数据库交互的 CRUD(创建、读取、更新、删除)操作时。这个工具能够自动生成 Mybatis 的 XML 映射文件、实体类以及 ...

    Mybatis Generator eclipse 插件

    Mybatis Generator是一款强大的自动化代码生成工具,主要用于简化Mybatis框架的使用,它可以自动生成SQL映射文件、Mapper接口、实体类以及DAO实现类等代码,极大地提高了开发效率。在Eclipse环境中,我们可以安装...

    mybatis generator eclipse plugin

    Mybatis Generator 是一款强大的工具,它能够自动化地生成 Mybatis 的映射文件、实体类以及相关的 SQL 映射代码,极大地提高了开发效率。Eclipse Mybatis Generator 插件是这个工具在 Eclipse 开发环境中的集成,让...

    Mybatis Generator逆向工程

    Mybatis Generator是一款强大的工具,它能够帮助Java开发者自动地生成Mybatis框架的Mapper接口、XML配置文件以及实体类,极大地提升了开发效率。逆向工程,简单来说,就是根据已有的数据库结构来生成相应的代码,...

Global site tag (gtag.js) - Google Analytics