需求:查询数特定角色下的所有用户列表

首先需要在在User类中引用Role类,因为引用了复杂的数据类型,所以要使用association属性进行映射,其实起主要作用的还是resultMap属性。

/**
* 根绝用户的角色id,获取该角色下的所有用户的信息
* @param roleid
* @return
*/
public List<User> getUserListByRoleID(@Param("userRole1") Integer roleid);

<resultMap type="User" id="UserRoleResult">
<id property="id" column="id"/>
<result property="userCode" column="userCode" />
<result property="userName" column="userName" />
<result property="userRole" column="userRole" />
<!--User类中引用的Role类 -->
<association property="role" javaType="Role" resultMap="roleResult"/>

</resultMap>
<resultMap type="Role" id="roleResult">
<id property="id" column="b_id"/>
<result property="roleCode" column="roleCode"/>
<result property="roleName" column="roleName"/>
</resultMap>

<!--根绝用户的角色id,获取该角色下的所有用户的信息 -->
<select id="getUserListByRoleID" resultMap="UserRoleResult" parameterType="Integer">
SELECT a.*,b.id as b_id,b.rolename from smbms_user a,smbms_role b where a.userrole=b.id and b.id=#{userRole1}
</select>

 package cn.smbms.pojo;

 import java.util.Date;

 public class User {
private Integer id; //id
private String userCode; //用户编码
private String userName; //用户名称
private String userPassword; //用户密码
private Integer gender; //性别
private Date birthday; //出生日期
private String phone; //电话
private String address; //地址
private Integer userRole; //用户角色
private Integer createdBy; //创建者
private Date creationDate; //创建时间
private Integer modifyBy; //更新者
private Date modifyDate; //更新时间
private Role role;//用户角色 public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getUserRole() {
return userRole;
}
public void setUserRole(Integer userRole) {
this.userRole = userRole;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}
     @Test
public void testGetUserListByRoleId(){
SqlSession sqlSession = null;
List<User> userList = new ArrayList<User>();
try {
sqlSession = MyBatisUtil.createSqlSession(); userList = sqlSession.getMapper(UserMapper.class).getUserListByRoleID(2); } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
MyBatisUtil.closeSqlSession(sqlSession);
}
for(User user: userList){
logger.debug("testGetUserListByRoleId roleid: " + user.getUserCode() + " and userName: " + user.getUserName()+"and userRoleName:"+user.getRole().getRoleName());
}
}

mybatis框架-使用resultMap实现高级结果映射,association属性的更多相关文章

  1. mybatis框架-使用resultMap实现高级结果映射,collection属性的使用

    需求:获取指定用户的用户信息和地址列表 修改user实体类  添加集合的引用. /** * 根绝用户id,获取该角色下的地址信息 * @param userID * @return */ public ...

  2. (转)MyBatis框架的学习(五)——一对一关联映射和一对多关联映射

    http://blog.csdn.net/yerenyuan_pku/article/details/71894172 在实际开发中我们不可能只是对单表进行操作,必然要操作多表,本文就来讲解多表操作中 ...

  3. 使用resultMap实现高级结果映射

    使用resultMap实现高级结果映射 resultMap的属性: 1.属性 id:resultMap的唯一标识.type:resulMap的映射结果类型(一般为Java实体类).2.子节点 id:一 ...

  4. MyBatis 入门到精通(三) 高级结果映射

    MyBatis的创建基于这样一个思想:数据库并不是您想怎样就怎样的.虽然我们希望所有的数据库遵守第三范式或BCNF(修正的第三范式),但它们不是.如果有一个数据库能够完美映射到所有应用程序,也将是非常 ...

  5. oracle使用resultMap实现高级结果映射

    resultMap的属性: 1.属性 id:resultMap的唯一标识.type:resulMap的映射结果类型(一般为Java实体类).2.子节点 id:一般对应数据库的主键 id,设置此项可以提 ...

  6. MyBatis(八):高级结果映射

    本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出一遍就懂!b站搜索狂神说或点击下面链接 https://space.bilibili.com/95256449?spm_id_from=33 ...

  7. Mybatis框架基础支持层——反射工具箱之实体属性Property工具集(6)

    本篇主要介绍mybatis反射工具中用到的三个属性工具类:PropertyTokenizer.PropertyNamer.PropertyCopier. PropertyTokenizer: 主要用来 ...

  8. MyBatis框架之SQL映射和动态SQL

    使用MyBatis实现条件查询 1.SQL映射文件: MyBatis真正的强大之处就在于SQL映射语句,MyBatis专注于SQL,对于开发人员来说也是极大限度的进行SQL调优,以保证性能.下面是SQ ...

  9. mybatis学习记录四——输入、输出映射

    6      输入映射 通过parameterType指定输入参数的类型,类型可以是简单类型.hashmap.pojo的包装类型. 6.1.1     需求 完成用户信息的综合查询,需要传入查询条件很 ...

随机推荐

  1. html5 audio标签切换播放音乐的方法

    html5 audio标签切换播放音乐的方法<pre><audio id="music1" preload loop="loop">&l ...

  2. storm单节点问题(转载)

    一.storm nimbus 单节点问题概述 1.storm集群在生产环境部署之后,通常会是如下的结构: 从图中可以看出zookeeper和supervisor都是多节点,任意1个zookeeper节 ...

  3. [转帖]Kubernetes的部署策略

    Kubernetes的部署策略,你常用哪种? https://www.sohu.com/a/318731931_100159565?spm=smpc.author.fd-d.78.1574127778 ...

  4. Docker的安装与使用

    Docker的安装 (1)卸载老版本yum remove docker \                  docker-client \                  docker-clien ...

  5. Neo4j学习——基本操作(一)

    由于开始学习知识图谱,因此需要涉及到neo4j的使用一.介绍neo4j是一个图形数据库基于Java开发而成,因此需要配置jvm才可以运行配置请参考我前面的一篇blog:https://www.cnbl ...

  6. 【在 Nervos CKB 上做开发】Nervos CKB 脚本编程简介[4]:在 CKB 上实现 WebAssembly

    作者:Xuejie 原文链接:https://xuejie.space/2019_10_09_introduction_to_ckb_script_programming_wasm_on_ckb/ N ...

  7. 使用 Navicat Premium 将 sql server 的数据库迁移到 mysql 的数据库中

    步骤1,打开 Navicat Premium ,创建一个新的 mysql 数据库: 步骤2,选中刚刚创建的新数据库 ,双击选中后点击导入向导,然后选择 "ODBC",并点击下一步 ...

  8. SQL Server优化之SET STATISTICS开关(转载)

    一.准备工作 缓存对于某个查询的性能影响十分之大,所以优化之前要清空缓存. 清除Buffer Pool里面的所有缓存 DBCC DROPCLEANBUFFERS 清除Buffer Pool里的所有缓存 ...

  9. 2019 京东java面试笔试总结 (含面试题解析)

       本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.京东等公司offer,岗位是Java后端开发,因为发展原因最终选择去了京东,入职一年时间了,也成为了面试官, ...

  10. Beego 学习笔记15:布局页面

    页面布局 1>     一个html页面由:head部分,body部分,内部css,内部js,外联css,外联的js这几部分组成.因此,一个布局文件也就需要针对这些进行拆分. 2>     ...