iBatis 相关

1、配置iBatis 返回类型(例如,sum( ) 一个BigDeciaml列后 )

  <select id="abatorgenerated_totalWt2" resultClass="java.math.BigDecimal" parameterClass="java.lang.String">

    select sum(shp_wt2) from T04_CASE_DTL_WK where slip_id = #slipId:VARCHAR# ;
  </select>

2、iBatis 模糊查询问题分析

问题:通过 iBatis 提供的接口实现“前缀匹配”模糊查询时,如果输入的参数是( "_" 或 "%") 会查询出所有的记录。

分析:iBatis 提供的模糊查询解析后的SQL语句如( like '_%' 或 like '%%')当然会查询出所有的。

解决:转义掉特殊字符(如下:)

select count(*) from m03_user_info where pc_usr_nm like '//%' escape '/' and company_id='01'

a、提供一个方法,将模糊查询时的特殊字符转义掉(包含转义字符("/")本身)

    /**
     * 转换查询条件中的通配符( "_", "%" )
     */
    public static String escapeSqlWildcard( String s ){
    	if( StringUtils.isBlank( s ) ){
    		throw new IllegalStateException( "s is null, for SQL query." );
    	}
    	
    	StringBuilder sb = new StringBuilder();
    	for( int i=0 ; i<s.length() ; i++ ){
    		char ch = s.charAt( i );
    		switch( ch ){
    			case '_':
    				sb.append( "/_" );
    				break;
    			case '%':
    				sb.append( "/%" );
    				break;
    			case '/':
    				sb.append( "//" );
    				break;
    			default:sb.append( ch );
    		}
    	}
    	return sb.toString();
    }
b、在JavaBean 中返回参数时,完成转义

package jp.co.snjp.db.model;

import jp.co.snjp.kddi.web.util.Global;

/**
 * 封装分页查询M03UserInfo的参数
 * @author GongQiang
 *
 */
public class M03UserInfoPageList {
	private int page;
	private int pageSize;
	private String companyId;
	private String name;

	public M03UserInfoPageList(int page, int pageSize, String companyId, String name) {
		super();
		this.page = page;
		this.pageSize = pageSize;
		this.companyId = companyId;
		this.name = name;
	}
	
	public M03UserInfoPageList(int page, int pageSize, String companyId) {
		super();
		this.page = page;
		this.pageSize = pageSize;
		this.companyId = companyId;
	}
	
	public M03UserInfoPageList(String companyId, String name) {
		super();
		this.companyId = companyId;
		this.name = name;
	}
	
	public M03UserInfoPageList() {
		super();
		// TODO Auto-generated constructor stub
	}

	// 其他 get/set 方法省略
	
	public String getName() {
		if( name == null ){
			return null;
		}
		return Global.escapeSqlWildcard( name ) + "%";
	}
	
}
c、在 iBatis 的sqlMap 配置文件中配置
  <!-- ***************************************通过companyId 和 name 查询记录条数*************************************** -->
  <select id="abatorgenerated_countByCompanyIdAndName" resultClass="Integer" parameterClass="jp.co.snjp.db.model.M03UserInfoPageList" >
    select count(*) from m03_user_info where company_id = #companyId:VARCHER# 
    	<isNotNull property="name">
    		and pc_usr_nm like #name# escape'/'
    	</isNotNull>
  </select>
d、dao 层新增方法

    /**
     * 查询记录条数,通过companyId 和name 
     * @param params
     * @return
     * @throws SQLException
     *
     * Date	  :2011-11-28
     * Author :GongQiang
     */
    public Integer countByCompanyIdAndName( M03UserInfoPageList params )  throws SQLException{
		if( params == null ){
			throw new IllegalStateException( "params is null." );
		}
		
		return (Integer) sqlMapClient.queryForObject( "m03_user_info.abatorgenerated_countByCompanyIdAndName", params);
    }

这样就 OK 了。。。。。。。


你可能感兴趣的:(iBatis 相关)