zoukankan      html  css  js  c++  java
  • mybatis自定义join和模糊查询

    关于mybatis通过和数据库连接生成的map.xml不能满足我们的查询条件的时候,我们要做的就是自定义一个查询的功能。

    (1)需要在生成的xml文件当中添加我们想要join的两张表的字段信息。

    <resultMap id="queryForListMap" type="com.report.pojo.RuleRunResult">
            <id column="INTERNAL_ID" property="internalId" jdbcType="INTEGER" />
            <result column="FIELD_CHECK_CONF_ID" property="fieldCheckConfId"  jdbcType="INTEGER" />
            <result column="BUSINESS_DATE" property="businessDate" jdbcType="DATE" />
            <result column="FACT_EXPRESSION" property="factExpression" jdbcType="VARCHAR" />
            <result column="FACT_RULE_WHERE" property="factRuleWhere" jdbcType="VARCHAR" />
            <result column="RUN_RESULT" property="runResult" jdbcType="VARCHAR" />
            <result column="ADD_TIME" property="addTime" jdbcType="TIMESTAMP" />
            <association property="fieldCheckConf" javaType="com.report.pojo.FieldCheckConf">
                <id column="INTERNAL_ID" property="internalId" jdbcType="INTEGER" />
                <result column="RULE_CONF_ID" property="ruleConfId" jdbcType="INTEGER" />
                <result column="RULE_NAME" property="ruleName" jdbcType="VARCHAR" />
                <result column="FACT_EXPRESSION" property="factExpression" jdbcType="VARCHAR" />
                <result column="FACT_RULE_WHERE" property="factRuleWhere" jdbcType="VARCHAR" />
                <result column="ADD_TIME" property="addTime" jdbcType="TIMESTAMP" />
            </association>
    
        </resultMap>

    (2)然后在xml文件当中实现自定义的sql(代码如下)

    下面这两个定义的参数一定要提前设置成这个样子。要不然的话一直其实sql报错。

         ruleName = "%"+ruleName+"%";
        expression = "%"+expression+"%";

    <select id="queryForList" resultMap="queryForListMap">
            SELECT
            *
            FROM
            RULE_RUN_RESULT r
            LEFT JOIN
            FIELD_CHECK_CONF f
            ON
            f.INTERNAL_ID = r.FIELD_CHECK_CONF_ID
            <where>
                <if test="ruleName != null and ruleName != ''">AND f.RULE_NAME like "${ruleName}" <!--这里一定要注意模糊查询的时候的语法格式 -->
                </if>
                <if test="expression != null and expression != ''">AND r.FACT_EXPRESSION like "${expression}"</if>
            </where>
    </select>

     总结:总的来说基本个的思路是不难实现的,但是在整个过程当中一定要注意我们使用的where查询条件模糊查询的配置百分号的写法。要不要会浪费很多时间的。

  • 相关阅读:
    Temporal Action Detection with Structured Segment Networks (ssn)【转】
    ubuntu多版本cuda并存与切换【两个博客链接】
    TURN TAP: Temporal Unit Regression Network for Temporal Action Proposals(ICCV2017)
    CTAP: Complementary Temporal Action Proposal Generation (ECCV2018)
    很实用的HTML5+CSS3注册登录窗体切换效果
    基于js的网页换肤(不需要刷新整个页面,只需替换css文件)
    CSS重置 reset.css
    CSS3制作分步注册表单
    CSS3 3D立体柜子实现
    创建 CSS3 下拉菜单
  • 原文地址:https://www.cnblogs.com/gxgd/p/8963675.html
Copyright © 2011-2022 走看看