`

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

相关推荐

    MyBatis Generator 生成器把其他数据库的同名表生成下来的问题:[WARNING] Table Configuration product matched more than one t

    MyBatis Generator(MBG)是一款强大的工具,用于自动生成MyBatis的Mapper接口、XML映射文件以及实体类,从而极大地提高了开发效率。然而,在使用MBG时,可能会遇到一些问题,比如“Table Configuration product ...

    mybatis自动生成工具修改版1.3.7源码-maven项目可直接运行

    mybatis-1-3-7自动生成工具修改版源码--maven项目 1、实体类添加注释。 2、mapper.xml去掉jdbcType类型转换 3、修改文件名mapper.java修改为IDAO.java 4、提取BaseDao,修改dao层接口自动继承。考虑是否生成Example类...

    python学习资料

    - 变量与数据类型:包括整型(int)、浮点型(float)、字符串(str)、布尔型(bool)等,以及列表(list)、元组(tuple)、字典(dict)和集合(set)等复合数据结构。 - 控制流:如条件语句(If-elif-else)、循环语句(for、...

    hibernate入門

    - **常用的 O/R 映射框架**:除了 Hibernate 外,还有 MyBatis、TopLink 等。 #### Hibernate 基础配置 - **提纲**:介绍 Hibernate 配置的基本要素,包括 XML 配置文件、日志配置、JUnit 测试环境搭建等。 - **...

    计算机发展与计算机应用概述.pdf

    计算机发展与计算机应用概述.pdf

    计算机二级公共基础知识全集合.pdf

    计算机二级公共基础知识全集合.pdf

    计算机机试答案.pdf

    计算机机试答案.pdf

    基于STM32F103的750W全桥逆变器并离网设计方案及其实现

    内容概要:本文详细介绍了基于STM32F103RCT6的750W全桥逆变器设计方案,涵盖硬件电路设计、软件编程以及保护机制等方面。硬件部分包括主控芯片的选择、PWM配置、Boost升压电路、PCB布局优化等;软件部分涉及并离网切换的状态机设计、过流保护、风扇控制算法、并机功能实现等。文中还分享了许多实战经验和调试技巧,如死区时间配置、电流采样方法、并网同步算法等。 适合人群:具有一定电子电路和嵌入式开发基础的技术人员,尤其是从事逆变器及相关电力电子产品开发的工程师。 使用场景及目标:适用于希望深入了解逆变器工作原理和技术实现的开发者,特别是那些需要掌握并离网切换、高效电源管理及可靠保护机制的人群。目标是帮助读者构建一个稳定可靠的逆变器系统,能够应对各种复杂的工作环境。 其他说明:本文不仅提供了详细的理论讲解,还有丰富的代码片段和实践经验分享,有助于读者更好地理解和应用相关技术。

    基于Simulink的单相全桥逆变器仿真与优化:MATLAB环境下的详细实现

    内容概要:本文详细介绍了如何利用Simulink在MATLAB环境中搭建单相全桥逆变器的仿真模型。首先,通过构建H桥结构,连接直流电源和RL负载,并引入PWM控制器进行开关管的控制。接着,针对仿真过程中遇到的各种问题,如谐波失真、开关管直通等问题,提出了具体的解决方案,包括加入LC滤波器、设置死区时间和优化PWM参数等。此外,还探讨了通过MATLAB脚本自动化测试不同参数组合的方法,以及如何提高电压利用率和降低谐波失真。最终,通过对仿真结果的分析,验证了所提方法的有效性和优越性。 适合人群:电力电子工程师、科研人员、高校学生等对逆变器仿真感兴趣的群体。 使用场景及目标:适用于研究和开发高效、稳定的逆变器系统,旨在通过仿真手段减少实验成本,优化设计方案,提高系统的性能指标。 其他说明:文中提供了详细的建模步骤和技术细节,帮助读者更好地理解和掌握相关技术和方法。同时,强调了仿真参数的选择和优化对于获得理想仿真结果的重要性。

    计算机红外通信.pdf

    计算机红外通信.pdf

    软考考试学习必备资料.md

    软考考试学习必备资料.md

    基于cornerstonejs开发移动端

    基于cornerstonejs开发移动端

    JavaScript网页设计高级案例:构建交互式图片画廊#JavaScript

    构建交互式图片画廊

    在学习Wpf的过程中,手搓了一个2048

    源码

    Bosch Rexroth IndraWorks Ds IndraWorks Ds 14V16.310.0

    Bosch Rexroth IndraWorks Ds IndraWorks Ds 14V16.310.0

    java面向对象 - 类与对象

    java面向对象 - 类与对象

    电机控制领域无感FOC算法的AT32平台实现及其鲁棒性优化

    内容概要:本文详细介绍了基于AT32平台的无感FOC(Field-Oriented Control)控制算法,特别是针对永磁同步电机(PMSM)和无刷直流电机(BLDC)的位置速度观测器实现。文章首先展示了启动策略的独特之处,即跳过传统前馈强拖阶段,直接利用矢量控制环和观测器协同启动。接着深入探讨了磁链观测器的核心算法,包括磁链积分、反正切求角度以及速度估算部分使用的改良版PLL。此外,文中还提到了容差配置模块,用于提高系统的鲁棒性和稳定性。最后,强调了模块间良好的解耦设计,使得各功能模块拥有明确的输入输出接口,增强了代码的可维护性和移植性。 适合人群:从事电机控制系统开发的技术人员,尤其是对无感FOC算法感兴趣的工程师。 使用场景及目标:适用于需要高精度、快速响应的电机控制系统开发项目,旨在提升系统的鲁棒性和稳定性,特别是在电机参数存在偏差的情况下依然能够保持良好性能。 其他说明:文章不仅提供了详细的代码实现,还分享了许多实用的经验和技术细节,如启动策略、磁链观测器的物理本质、速度估算方法等,有助于读者更好地理解和应用无感FOC算法。

    计算机机房de设置与维护.pdf

    计算机机房de设置与维护.pdf

    《Java 面试进阶指北 》 质量很高,专为面试打造

    《Java 面试进阶指北 》 质量很高,专为面试打造

    外转子开关磁阻电机多目标优化的NSGA-II算法实现与Matlab代码解析

    内容概要:本文详细介绍了外转子开关磁阻电机(ER-SRM)的多目标优化方法,主要采用NSGA-II算法进行优化。文章首先解释了为什么ER-SRM比传统内转子电机更难以优化,接着展示了如何利用NSGA-II算法解决这一难题。文中提供了详细的Matlab代码,包括种群初始化、交叉变异操作、非支配排序以及目标函数的定义。此外,还讨论了优化过程中的一些注意事项,如初始种群多样性的保持、交叉变异参数的选择、目标函数的设计等。最后,通过具体的案例和图表展示了优化结果及其应用价值。 适合人群:从事电机设计与优化的研究人员和技术人员,尤其是对外转子开关磁阻电机感兴趣的读者。 使用场景及目标:适用于需要同时优化电机效率、转矩波动和制造成本等多种目标的情况。通过NSGA-II算法,可以在多个相互冲突的目标间找到最佳平衡点,从而提高电机的整体性能。 其他说明:文章不仅提供了完整的Matlab代码实现,还分享了许多实践经验,如参数设置的经验公式、常见错误及解决方案等。这对于理解和掌握NSGA-II算法的实际应用非常有帮助。

Global site tag (gtag.js) - Google Analytics