sql查询结果与表的entity不一致时,用xml的ResultMap来自定义字段,便可轻松解决! 统计自段也可以在sql语句中用 as 将自定义信息转成一个 result column,
配置在ResultMap,便可以直接装入result Object模型。
Mapper.xml
<resultMap id="BaseResultMap" type="com.cloud.model.User">
<id column="id" property="id" jdbcType="INTEGER" />
........
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
<result column="statistics_count" property="patientCount" jdbcType="INTEGER"/>
</resultMap>
<select id="getAccountListBytIdByPage" resultMap="BaseResultMap">
select a.*, count(b.id) as statistics_count
from account as a
left JOIN sys_patient as b ON b.admin_account_id = a.id
where a.t_id = #{id}
and a.del_flg = 0
group by a.id
order by a.create_time desc
</select>
Mapper.java
Page<User> getAccountListBytIdByPage(Integer id, Page<User> ipage)
ServiceImp.java
public Page<User> getAccountListBytIdByPage(Integer id, Page<User> ipage) {
//获取个人管理员的列表
Page<User> userList = accountMapper.getAccountListBytIdByPage(id, ipage);
return userList;
}
Controller.java
// 获取page信息
Long page = Long.valueOf(pagedRequest.getPage());
Long size = Long.valueOf(pagedRequest.getSize());
Page<UserInfo> ipage = new Page<UserInfo>(page, size);
Page<User> userList = service. getAccountListBytidByPage(id, ipage);