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

首先需要在在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. Symbol 小妙处

    input 框输入后发送异步请求,页面拿到响应进行渲染.但偶尔会遇到问题:响应内容和输入结果不一致.因为 http 无法保证响应到达的顺序. 如何解决呢?提供一个小思路. myRequest.js i ...

  2. python之路——阅读目录

    阅读目录 希望大家多多交流,有错误的地方请随时指正,笔记记得可能有点杂 一.python入门 计算机基础 编程语言发展史和python安装  二.数据类型.字符编码.文件处理 python基础数据类型 ...

  3. Python批量更改文件名

    一.问题在处理文件或者一些其他信息的时候我们需要更改文件名,那么我们可以写一个程序来修改这些文件名,以减少我们重复的做一件事. 二.解决本次使用的Python,利用的是Python中的OS模块,具体操 ...

  4. DataTable求列的最大值、最小值、平均值和样本数

    与sql聚合函数相似,会屏蔽null table.Compute("max(ColumnName)", "true"); table.Compute(" ...

  5. Identity和IdentityServer的区别及联系

    关于Identity和IdentityServer初学的时候可能会有一些疑惑(虽然我也不是很精深吧),但是,这里说一下自己关于这两者的一些理解,如有错误,欢迎指正 总体上, ASP.NET Core ...

  6. C#读取Excel文件,准换为list

    经常会用到,废话不多说直接贴代码 //读取Excel文件 public static DataTable ReadExcelToTable(string path)//excel存放的路径{try{ ...

  7. word,excel,ppt转pdf

    第一步 需要下载jar包和jacob-1.14.3-x64.dll * <dependency> * <groupId>net.sf.jacob-project</gro ...

  8. js如何保留两位小数,并进行四舍五入

    保留两位小数,并进行四舍五入使用js函数 toFixed() 函数传递一个参数(Number) Number就为需要保留小数的位数 具体实现代码 <script language="j ...

  9. 智能制造进入下半场?APS如何进行优化

    按照现在算法和计算机处理能力的发展,现在资源优化的方向已经逐渐摒弃,而是在更系统的“有限产能计划的”框架内一并解决产能和物料的问题. 我们所看到的新近涌现出来的很多APS系统.但碍于算法的复杂程度,在 ...

  10. 面试题:什么叫B树

    B是balanced的意思 是在二叉查找树  树的基础上增加了平衡的性质   导致它不是  二叉树了,变成了多叉树,但是叉几路又有了限制  否则怎么平衡呢 一棵m阶B树(balanced tree o ...