一、介绍
- 为了解决数据库字段列名和返回值POJO类属性不一致的问题,除了别名方式还可以使用resultMap
二、单表
1、接口
List<User> queryAll();
2、xml
<resultMap id="userResult" type="com.User">
<id column="user_id" property="userId" />
<result column="user_name" property="userName" />
</resultMap>
<select id="queryAll" resultMap="userResult">
select id,user_name from t_user
</select>
三、关联查询
1、接口
List<DeptVO> findAllByDeptno(Integer deptId);
2、xml
<resultMap id="deptVoMap" type="vo.DeptVO">
<id column="dep_id" property="deptId"/>
<result column="name" property="deptName"/>
<collection property="users" ofType="user.User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="dept_id" property="deptId"/>
</collection>
</resultMap>
<select id="queryAllByDeptId" resultMap="deptVoMap">
select
dept.id dep_id,
dept.name,
user.id,
user.username,
user.password,
user.dept_id
from t_user user
join t_dept dept on user.dept_id=dept.id
where t_dept.id=#{deptId}
</select>
3、返回值
{
"deptId":2,
"deptName":"c++",
"users":[
{
"id":13,
"username":"spring",
"password":"1234",
"deptId":2
},
{
"id":14,
"username":"mybatis",
"password":"12cxv34",
"deptId":2
}
]
}