代码下载
解决字段名与属性名不一致
- ①使用别名emp_name empName解决字段名和属性名不一致
<select id="getAllEmpOld" resultType="Emp">
select eid,emp_name empName,age,sex,email from t_emp;
</select>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<resultMap id="empResultMap" type="Emp">
<id property="eid" column="eid"/>
<id property="empName" column="emp_name"/>
<id property="age" column="age"/>
<id property="sex" column="sex"/>
<id property="email" column="email"/>
</resultMap>
<select id="getAllEmp" resultMap="empResultMap">
select * from t_emp;
</select>
多对一映射关系
<resultMap id="empAndDeptResultMapOne" type="Emp">
<id property="eid" column="eid"/>
<id property="empName" column="emp_name"/>
<id property="age" column="age"/>
<id property="sex" column="sex"/>
<id property="email" column="email"/>
<id property="dept.did" column="did"/>
<id property="dept.deptName" column="dept_name"/>
</resultMap>
<select id="getEmpAndDept" resultMap="empAndDeptResultMapOne">
select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid=#{eid}
</select>
<resultMap id="empAndDeptResultMapTwo" type="Emp">
<id property="eid" column="eid"/>
<id property="empName" column="emp_name"/>
<id property="age" column="age"/>
<id property="sex" column="sex"/>
<id property="email" column="email"/>
<association property="dept" javaType="Dept">
<id property="did" column="did"/>
<id property="deptName" column="dept_name"/>
</association>
</resultMap>
<select id="getEmpAndDept" resultMap="empAndDeptResultMapTwo">
select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid=#{eid}
</select>
<mapper namespace="com.lotus.mybatis.mapper.DeptMapper">
<select id="getEmpAndDeptByStepTwo" resultType="Dept">
select * from t_dept where did=#{did}
</select>
</mapper>
<resultMap id="empAndDeptByStepResultMap" type="Emp">
<id property="eid" column="eid"/>
<id property="empName" column="emp_name"/>
<id property="age" column="age"/>
<id property="sex" column="sex"/>
<id property="email" column="email"/>
<association property="dept" select="com.lotus.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
column="did" fetchType="eager"></association>
</resultMap>
<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
select * from t_emp where eid=#{eid}
</select>
public interface EmpMapper {
Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);
}
public interface DeptMapper {
Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);
}
一对多映射关系
<resultMap id="deptAndEmpResultMap" type="Dept">
<id property="did" column="did"/>
<id property="deptName" column="dept_name"/>
<collection property="emps" ofType="Emp">
<id property="eid" column="eid"/>
<id property="empName" column="emp_name"/>
<id property="age" column="age"/>
<id property="sex" column="sex"/>
<id property="email" column="email"/>
</collection>
</resultMap>
<select id="getDeptAndEmp" resultMap="deptAndEmpResultMap">
select * from t_dept left join t_emp on t_dept.did = t_emp.did where t_dept.did=#{did}
</select>
Dept getDeptAndEmp(@Param("did") Integer did);
@Test
public void testGetDeptAndEmp() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
Dept dept = mapper.getDeptAndEmp(1);
System.out.println(dept);
}
<!---DeptMapper.xml>
<resultMap id="deptAndEmpByStepResultMap" type="Dept">
<id property="did" column="did"/>
<result property="deptName" column="dept_name"/>
<collection property="emps"
select="com.lotus.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
column="did"></collection>
</resultMap>
<select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpByStepResultMap">
select * from t_dept where did=#{did}
</select>
<select id="getDeptAndEmpByStepTwo" resultType="Emp">
select * from t_emp where did=#{did}
</select>
Dept getDeptAndEmpByStepOne(@Param("did") Integer did);
List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);
@Test
public void testGetDeptAndEmpStep() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
Dept dept = mapper.getDeptAndEmpByStepOne(1);
System.out.println(dept);
}