从MyBatis3.0开始,对mapper中的namespace属性新增了一个特性:可以指定具体的接口来作为持久化操作类,在接口中定义与映射文件中id属性值相同的方法,MyBatis会自动去绑定和执行对应的SQL语句。这种接口实现方式,需要为每个Mapper创建一个接口,如果系统做大了,维护这些类会比较麻烦,大象个人倾向于基础服务式的Dao实现类,当调用这个dao接口某个方法时,会自动找namespace为该接口的mapper文件,并找到文件中id和方法名称相同的sql。
如例子中的MyBatisDao。
(1)mapper文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="ibp.ioc.dao.rpt.collect.T2a_case_cust_curDAO">
<select id="queryT2a_case_cust_curList" parameterType="T2a_case_cust_curDTO" resultType="T2a_case_cust_curDTO">
select
CUST_ID, ORG_ID, CUST_NAME, CERT_TYPE, CERT_NO, CUST_TYPE, PBC_CUST_TYPE, CUST_NAT,
ADDR, TEL, OTHR_CTCT, PBC_INDUS, BS_INDUS, PBC_OCP, REG_FUND_AMT, REG_FUND_CURR_CD,
LEGAL_NAME, LEGAL_CERT_TYPE, LEGAL_CERT_NO, HOLD_NAME, HOLD_CERT_TYPE, HOLD_CERT_NO,
BH_VALID, BS_VALID, DUE_DT, CREATE_TM, CREATOR, MODIFY_TM, MODIFIER, RSRV_01, RSRV_02,
RSRV_03, RSRV_04
from T2a_case_cust_cur
<trim prefix="WHERE" prefixOverrides="AND|OR">
and (bh_valid = '0' or bs_valid = '0')
<if test="cust_name !='' and cust_name !=null " >
and cust_name like '%${cust_name}%'
</if>
<if test="org_id !='' and org_id !=null " >
and org_id = #{org_id}
</if>
<if test="due_dt_str_st !=null " >
<![CDATA[ and due_dt >= #{due_dt_str_st,jdbcType=DATE} ]]>
</if>
<if test="due_dt_str_ed !=null " >
<![CDATA[ and due_dt <= #{due_dt_str_ed,jdbcType=DATE} ]]>
</if>
<if test="loginusername !='' and loginusername !=null " >
and org_id in (select organkey from ibp_user_organ_resource where username = #{loginusername})
</if>
<if test="cust_id !='' and cust_id !=null " >
and cust_id like '%${cust_id}%'
</if>
<if test="cust_types !='' and cust_types !=null " >
and cust_type in
<foreach item="item" collection="cust_types" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</trim>
<if test="sidx !='' and sidx !=null and sord !='' and sord !=null " >
order by ${sidx} ${sord}
</if>
</select>
<select id="queryT2a_case_cust_cur" parameterType="T2a_case_cust_curDTO" resultType="T2a_case_cust_curDTO">
select
CUST_ID, ORG_ID, CUST_NAME, CERT_TYPE, CERT_NO, CUST_TYPE, PBC_CUST_TYPE, CUST_NAT,
ADDR, TEL, OTHR_CTCT, PBC_INDUS, BS_INDUS, PBC_OCP, REG_FUND_AMT, REG_FUND_CURR_CD,
LEGAL_NAME, LEGAL_CERT_TYPE, LEGAL_CERT_NO, HOLD_NAME, HOLD_CERT_TYPE, HOLD_CERT_NO,
BH_VALID, BS_VALID, DUE_DT, CREATE_TM, CREATOR, MODIFY_TM, MODIFIER, RSRV_01, RSRV_02,
RSRV_03, RSRV_04
from T2a_case_cust_cur
where cust_id = #{cust_id}
</select>
<update id="updateT2a_case_cust_cur" parameterType="T2a_case_cust_curDTO" >
update T2A_CASE_CUST_CUR
<trim prefix="SET" suffixOverrides=",">
<if test="cust_id != null" >
cust_id = #{cust_id,jdbcType=VARCHAR},
</if>
<if test="org_id != null" >
org_id = #{org_id,jdbcType=VARCHAR},
</if>
<if test="cust_name != null" >
cust_name = #{cust_name,jdbcType=VARCHAR},
</if>
<if test="cert_type != null" >
cert_type = #{cert_type,jdbcType=VARCHAR},
</if>
<if test="cert_no != null" >
cert_no = #{cert_no,jdbcType=VARCHAR},
</if>
<if test="cust_type != null" >
cust_type = #{cust_type,jdbcType=VARCHAR},
</if>
<if test="pbc_cust_type != null" >
pbc_cust_type = #{pbc_cust_type,jdbcType=VARCHAR},
</if>
<if test="cust_nat != null" >
cust_nat = #{cust_nat,jdbcType=VARCHAR},
</if>
<if test="addr != null" >
addr = #{addr,jdbcType=VARCHAR},
</if>
<if test="tel != null" >
tel = #{tel,jdbcType=VARCHAR},
</if>
<if test="othr_ctct != null" >
othr_ctct = #{othr_ctct,jdbcType=VARCHAR},
</if>
<if test="pbc_indus != null" >
pbc_indus = #{pbc_indus,jdbcType=VARCHAR},
</if>
<if test="bs_indus != null" >
bs_indus = #{bs_indus,jdbcType=VARCHAR},
</if>
<if test="pbc_ocp != null" >
pbc_ocp = #{pbc_ocp,jdbcType=VARCHAR},
</if>
<if test="reg_fund_amt != null" >
reg_fund_amt = #{reg_fund_amt,jdbcType=DECIMAL},
</if>
<if test="reg_fund_curr_cd != null" >
reg_fund_curr_cd = #{reg_fund_curr_cd,jdbcType=VARCHAR},
</if>
<if test="legal_name != null" >
legal_name = #{legal_name,jdbcType=VARCHAR},
</if>
<if test="legal_cert_type != null" >
legal_cert_type = #{legal_cert_type,jdbcType=VARCHAR},
</if>
<if test="legal_cert_no != null" >
legal_cert_no = #{legal_cert_no,jdbcType=VARCHAR},
</if>
<if test="hold_name != null" >
hold_name = #{hold_name,jdbcType=VARCHAR},
</if>
<if test="hold_cert_type != null" >
hold_cert_type = #{hold_cert_type,jdbcType=VARCHAR},
</if>
<if test="hold_cert_no != null" >
hold_cert_no = #{hold_cert_no,jdbcType=VARCHAR},
</if>
<if test="bh_valid != null" >
bh_valid = #{bh_valid,jdbcType=VARCHAR},
</if>
<if test="bs_valid != null" >
bs_valid = #{bs_valid,jdbcType=VARCHAR},
</if>
<if test="due_dt != null" >
due_dt = #{due_dt,jdbcType=DATE},
</if>
<if test="create_tm != null" >
create_tm = #{create_tm,jdbcType=VARCHAR},
</if>
<if test="creator != null" >
creator = #{creator,jdbcType=VARCHAR},
</if>
<if test="modify_tm != null" >
modify_tm = #{modify_tm,jdbcType=VARCHAR},
</if>
<if test="modifier != null" >
modifier = #{modifier,jdbcType=VARCHAR},
</if>
<if test="rsrv_01 != null" >
rsrv_01 = #{rsrv_01,jdbcType=VARCHAR},
</if>
<if test="rsrv_02 != null" >
rsrv_02 = #{rsrv_02,jdbcType=VARCHAR},
</if>
<if test="rsrv_03 != null" >
rsrv_03 = #{rsrv_03,jdbcType=VARCHAR},
</if>
<if test="rsrv_04 != null" >
rsrv_04 = #{rsrv_04,jdbcType=VARCHAR},
</if>
</trim>
where cust_id = #{cust_id}
</update>
</mapper>
(2)与mapper文件对应得T2a_case_cust_curDAO接口:
package ibp.ioc.dao.rpt.collect;
import java.util.List;
import org.hibernate.validator.constraints.br.TituloEleitoral;
import com.github.abel533.echarts.Title;
import ibp.dto.rpt.collect.T2a_case_cust_curDTO;
import ibp.ioc.dao.core.BaseDAO;
public interface T2a_case_cust_curDAO extends BaseDAO{
/**
*
* queryT2a_trans_preList
* @Title: 查询列表
* @Description: TODO
* @param t2a_trans_pre
* @return
* 修改记录 :
* 版本号 日期 修改人 修改描述
* --------------------------------------------------------------
* V1.0 2016年6月14日 liwm TODO
*/
public List<T2a_case_cust_curDTO> queryT2a_case_cust_curList (T2a_case_cust_curDTO t2a_case_cust_cur);
/**
*
* 查询待补录客户补录 根据tr_id
* @Title: queryT2a_case_cust_cur
* @Description: TODO
* @param t2a_case_cust_cur
* @return
* 修改记录 :
* 版本号 日期 修改人 修改描述
* --------------------------------------------------------------
* V1.0 Jul 15, 2016 liwm 创建
*/
public T2a_case_cust_curDTO queryT2a_case_cust_cur (T2a_case_cust_curDTO t2a_case_cust_cur);
/**
*
* 补录
* @Title: updateT2a_case_cust_cur
* @Description: TODO
* @param t2a_trans_pre
* @return
* 修改记录 :
* 版本号 日期 修改人 修改描述
* --------------------------------------------------------------
* V1.0 Jul 15, 2016 liwm 创建
*/
public Integer updateT2a_case_cust_cur(T2a_case_cust_curDTO t2a_case_cust_cur);
}