resultMap自定义某个javaBean的封装规则代码

<?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">
	<!--namespace:名称空间 -->
<mapper namespace="cn.bdqn.mybatis.dao.EmpMapperPlus">

<!--
自定义某个javaBean的封装规则
  type:要自定义规则的javaBean类型
  id:唯一标识,方便引用
  -->
	<resultMap type="cn.bdqn.mybatis.been.Emp" id="myEmp">
		<!--
		id定义主键会有底层优化
		指定主键列的封装规则 
		column:指定哪一列
		property:指定对应的javaBean属性
		 -->
		<id	column="id" property="id"/>
		<!--定义普通列封装规则  -->
		<result	column="last_name"		property="lastName"/>
		<!-- 其他不指定的列会自动封装,但是建议,如果我们写了resultMap,我们就把所有列的映射都写上 -->
		<result column="email"	property="email"/>
		<result column="gender" property="gender"/>
	</resultMap>
	
	
	<!-- public Emp getEmpById(Integer id); -->
	<!-- resultMap自定义结果集 resultType和resultMap只能二选一 -->
	<select id="getEmpById"	 resultMap="myEmp">
		select * from emp where id=#{id}
	</select>

</mapper>

  

posted @ 2017-09-24 13:21  懒人写博客  阅读(894)  评论(0编辑  收藏  举报