Mybatis 主要学习内容就是mapping,下面我就一些学习内容做些记录:
1、生成model、mapping、dao的时候要清空src,否则有的文件覆盖时会多出内容来;
2、注意mapping文件中List的返回类型,是那个实体类;
<resultMap id="userResultMap" type="kbvs.model.User"
extends="BaseResultMap">
<collection property="userRoles" column="id" javaType="list"
select="selectUserRole" />
</resultMap>
3、一个多参数查询的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="com.creditharmony.approve.antifraud.dao.BacklistAllDao" > <resultMap id="BaseResultMap" type="com.creditharmony.approve.antifraud.entity.BacklistAll" > <result column="id" property="id" jdbcType="NUMERIC" /> <result column="loan_code" property="loanCode" jdbcType="VARCHAR" /> <result column="dict_black_type" property="dictBlackType" jdbcType="VARCHAR" /> <result column="black_msg" property="blackMsg" jdbcType="VARCHAR" /> <result column="dict_source" property="dictSource" jdbcType="VARCHAR" /> <result column="dict_result" property="dictResult" jdbcType="VARCHAR" /> <result column="create_by" property="createBy" jdbcType="VARCHAR" /> <result column="create_time" property="createTime" jdbcType="TIMESTAMP" /> <result column="modify_by" property="modifyBy" jdbcType="VARCHAR" /> <result column="modify_time" property="modifyTime" jdbcType="TIMESTAMP" /> </resultMap> <sql id="balckListAll"> a.ID, a.judge_proc_by, <!-- 反欺诈处理人编号--> a.judge_proc_date, <!--反欺诈处理日期 --> a.loan_code, <!-- 借款编号 --> a.dict_case_result, <!--处理结果 --> e.customer_into_time, <!-- 进件时间 --> e.loan_customer_name, <!--借款人姓名 --> e.product_type, <!-- 产品类型 --> c.black_msg, <!-- 加黑内容 --> c.dict_black_type, <!-- 加黑类型 --> d.org_name, <!-- 机构名 --> f.user_name <!-- 销售人员 --> <!--省份 t_gl_province_city.area_name --> </sql> <!-- --> <select id="findByParams" parameterType="map" resultType="com.creditharmony.approve.antifraud.entity.ex.BlacklistAllEx"> SELECT <include refid="balckListAll"/> FROM jk.T_JK_ANTIFRAUD_JUDGE a <!--反欺诈_判定 --> left join jk.T_JK_BACKLIST_ALL c <!--反欺诈_黑名单库 --> on a.LOAN_CODE = c.LOAN_CODE left join jk.T_JK_LOAN_INFO e <!--借款_借款信息表 --> on c.LOAN_CODE = e.LOAN_CODE left join jk.T_GL_ORG d <!--机构表 --> on e.LOAN_TEAM_ORGID = d.ORG_CODE left join jk.T_GL_USER f <!--用户表 --> on e.LOAN_MANAGERCODE = f.USER_CODE where a.DICT_CASE_RESULT in ('1','0') <if test="judgeProcBy != null and judgeProcBy != ''"> and a.JUDGE_PROC_BY = #{judgeProcBy} </if> <if test="judgeProcDateFrom != null and judgeProcDateFrom != '' and judgeProcDateTo != null and judgeProcDateTo != ''"> and a.JUDGE_PROC_DATE between #{judgeProcDateFrom} and #{judgeProcDateTo} </if> <if test="customerIntoTimeFrom != null and customerIntoTimeFrom != '' and customerIntoTimeTo != null and customerIntoTimeTo != ''"> and e.CUSTOMER_INTO_TIME between #{customerIntoTimeFrom} and #{customerIntoTimeTo} </if> <if test="loanCode != null and loanCode != ''"> and a.LOAN_CODE = #{loanCode} </if> <if test="loanCustomerName != null and loanCustomerName != ''"> and e.LOAN_CUSTOMER_NAME = #{loanCustomerName} </if> <if test="dictCaseResult != null and dictCaseResult != ''"> and a.DICT_CASE_RESULT = #{dictCaseResult} </if> <if test="dictBlackType != null and dictBlackType != ''"> and c.DICT_BLACK_TYPE = #{dictBlackType} </if> <if test="blackMsg != null and blackMsg != ''"> and c.BLACK_MSG = #{blackMsg} </if> <if test="orgName != null and orgName != ''"> and d.ORG_NAME = #{orgName} </if> <if test="productType != null and productType != ''"> and e.PRODUCT_TYPE = #{productType} </if> <if test="userName != null and userName != ''"> and f.USER_NAME = #{userName} </if> </select> <insert id="insert" parameterType="com.creditharmony.approve.antifraud.entity.BacklistAll" > insert into jk.t_jk_backlist_all (id, loan_code, dict_black_type, black_msg, dict_source, dict_result, create_by, create_time, modify_by, modify_time) values (#{id,jdbcType=NUMERIC}, #{loanCode,jdbcType=VARCHAR}, #{dictBlackType,jdbcType=VARCHAR}, #{blackMsg,jdbcType=VARCHAR}, #{dictSource,jdbcType=VARCHAR}, #{dictResult,jdbcType=VARCHAR}, #{createBy,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{modifyBy,jdbcType=VARCHAR}, #{modifyTime,jdbcType=TIMESTAMP}) </insert> <insert id="insertSelective" parameterType="com.creditharmony.approve.antifraud.entity.BacklistAll" > insert into jk.t_jk_backlist_all <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="loanCode != null" > loan_code, </if> <if test="dictBlackType != null" > dict_black_type, </if> <if test="blackMsg != null" > black_msg, </if> <if test="dictSource != null" > dict_source, </if> <if test="dictResult != null" > dict_result, </if> <if test="createBy != null" > create_by, </if> <if test="createTime != null" > create_time, </if> <if test="modifyBy != null" > modify_by, </if> <if test="modifyTime != null" > modify_time, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=NUMERIC}, </if> <if test="loanCode != null" > #{loanCode,jdbcType=VARCHAR}, </if> <if test="dictBlackType != null" > #{dictBlackType,jdbcType=VARCHAR}, </if> <if test="blackMsg != null" > #{blackMsg,jdbcType=VARCHAR}, </if> <if test="dictSource != null" > #{dictSource,jdbcType=VARCHAR}, </if> <if test="dictResult != null" > #{dictResult,jdbcType=VARCHAR}, </if> <if test="createBy != null" > #{createBy,jdbcType=VARCHAR}, </if> <if test="createTime != null" > #{createTime,jdbcType=TIMESTAMP}, </if> <if test="modifyBy != null" > #{modifyBy,jdbcType=VARCHAR}, </if> <if test="modifyTime != null" > #{modifyTime,jdbcType=TIMESTAMP}, </if> </trim> </insert> </mapper>
对应代码:
public interface BacklistAllDao extends CrudDao<BlacklistAllEx>{
public List<BlacklistAllEx> findByParams(Map<String,Object> params, PageBounds pageBounds);
}
service中的使用:
@Service
@Transactional(value="transactionManager",readOnly=true)
public class BlackListAllService extends CoreManager<BacklistAllDao,BlacklistAllEx> {
public Page<BlacklistAllEx> findByParams(Page<BlacklistAllEx> page,Map<String,Object> filter) {
String sortString = "id.asc";
PageBounds pageBounds = new PageBounds(page.getPageNo(),page.getPageSize(),Order.formString(sortString));
PageList<BlacklistAllEx> pageList = (PageList<BlacklistAllEx>)dao.findByParams(filter, pageBounds);
PageUtil.convertPage(pageList, page);
return page;
}
}
Map的构成(web中的使用):
@SuppressWarnings("unchecked")
@RequiresPermissions("approve:antifraud:view")
@RequestMapping(value ="findBlackListAll")
public String findBlackListAll(BlacklistAllEx blacklistAllEx, Model model,HttpServletRequest request, HttpServletResponse response) {
Page<BlacklistAllEx> page = new Page<BlacklistAllEx>(request,response);
Map<String,Object> filter = new HashMap<String, Object>();
filter = jsonMapper.convertValue(blacklistAllEx, Map.class);
Page<BlacklistAllEx> findByParams = blackListAllService.findByParams(page, filter);
model.addAttribute("judgeProcDateFrom", blacklistAllEx.getJudgeProcDateFrom());
model.addAttribute("judgeProcDateTo", blacklistAllEx.getJudgeProcDateTo());
model.addAttribute("customerIntoTimeFrom", blacklistAllEx.getCustomerIntoTimeFrom());
model.addAttribute("customerIntoTimeTo", blacklistAllEx.getCustomerIntoTimeTo());
model.addAttribute("page", findByParams);
return "/approve/antifraud/blackList";
}
Map简单构成方式:
@RequestMapping(value = {"findfields"})
public @ResponseBody List<SqData> findfields(Model model,String tablename) {
Map<String,Object> filter = new HashMap<String, Object>();
filter.put("tablename", tablename);
List<SqData> fieldList = sqdataService.query(filter);
return fieldList;
}
4、Mapper的类型,上面是多条件查询的情况;还有单个参数的:
<select id="getAntifraudBlacklist" parameterType="String" resultType="com.creditharmony.approve.antifraud.entity.AntifraudBlacklist" > select <include refid="Base_Column_List" /> from jk.t_jk_antifraud_blacklist where r_offend_id = #{rid} </select>
parameterType="String"
浙公网安备 33010602011771号