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查询条件模糊查询的配置百分号的写法。要不要会浪费很多时间的。

  • 相关阅读:
    终于想起了博客园密码
    关于GCD的8题
    idea快捷键 ctrl + shift + f 失效解决方法
    前端和后端日期类型交互
    poi、easypoi和easyexcel的使用
    事务总结
    数据库cte的理解和使用
    mybatis 调用存储过程没有返回值
    postgresql 查询锁表并解锁
    tigase网络核心SockThread详解(十九)
  • 原文地址:https://www.cnblogs.com/gxgd/p/8963675.html
Copyright © 2011-2022 走看看