为什么Mybatis可以通过SO加入查询条件

本文介绍如何使用MyBatis的动态SQL特性实现灵活查询,包括条件拼接、参数处理等关键技术点,并通过实例演示其具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、比如我们在SignInfoMapper.xml中,
     <sql id= "SO_Where_Clause">
             <where>
                  1=1
                   <if test="@org.apache.commons.lang.StringUtils@isNotBlank(province)" >
                        and t.PROVINCE=#{province}
                   </if>
                   <if test="@org.apache.commons.lang.StringUtils@isNotBlank(city)" >
                        and t.CITY=#{city}
                   </if>
                   <if test="@org.apache.commons.lang.StringUtils@isNotBlank(county)" >
                        and t.COUNTY=#{county}
                   </if>
                   <if test="isSearched != -1" >
                        and NVL(t.IS_SEARCHED,0) = #{isSearched}
                   </if>
                   <if test="reqTimeFrom!=null" >
                      and t.REQ_TIME <![CDATA[ >= ]]> #{reqTimeFrom}
                   </if>
                   <if test="reqTimeTo!=null" >
                      and t.REQ_TIME <![CDATA[ <= ]]> #{reqTimeTo}+1
                   </if>
             </where>
             <include refid="sqlmap.common.Order_By_Clause" />
       </sql>

    然后<select id= "getListBySo" resultMap ="signInfoVoResultMap">
            SELECT t.* FROM ASS_SIGN_INFO t
             <include refid="SO_Where_Clause" />
       </select>
    然后:
      @Override
public <E> List<E> getVOListBySo(BasePageSO so) {
    RowBounds rowBounds = new RowBounds((so.getPageNumber() - 1) *so.getObjectsPerPage(), so.getObjectsPerPage());
     return sqlSessionTemplate.selectList(getStatementPrefix() + POSTFIX_GET_LIST_BY_SO, so, rowBounds);
    }
    这样就可以实现动态的查询了呢?Mybatis是如何将这个动态的参数拼接到SQL上的呢?

2、我们通过调试查看该方法的执行过程,如下所示:
    2.1、
     @Test
    public void testGetPageList() {
        ReqLogSO sos = new ReqLogSO();
        sos.setCity( "上海市");
        sos.setProvince( "上海");
        List<ReqLogVO> list = reqLogService.getVOListBySo(sos);
        assertTrue(list.size() > 0);
    }
    2.2、reqLogService .getVOListBySo(sos);调用:
   
@Override
public <E> List<E> getVOListBySo(BasePageSO so) {
        RowBounds rowBounds =
new RowBounds((so.getPageNumber() - 1) *so.getObjectsPerPage(), so.getObjectsPerPage());
       
return sqlSessionTemplate.selectList(getStatementPrefix() + POSTFIX_GET_LIST_BY_SO, so, rowBounds);
    }
    2.3、上述下划线的方法调用,SqlSessionTemplate.class中的:
public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {  
return this.sqlSessionProxy .<E> selectList(statement, parameter, rowBounds);}

2.4、接着调用DefaultSqlSession.class中的:
public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
try {
      MappedStatement ms =configuration.getMappedStatement(statement);
      List<E> result =
executor.<E>query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER );
     
return result;
    }
catch (Exception e) {
    ...
  }
2.5、executor.<E>query()方法接着调用CachingExecutor的方法:
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
    BoundSql boundSql = ms.getBoundSql(parameterObject);
    CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);
    return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
  }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值