<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace的名字需要跟接口的类名一致 -->
<mapper namespace="cn.bdqn.dao.UserMapper">
<!--
1、resultMap属性:type为java实体类;id为此resultMap的标识
2、resultMap的子元素:
id – 一般对应到数据库中该行的ID,设置此项可以提高Mybatis性能.
result – 映射到JavaBean 的某个“简单类型”属性,String,int等.
association – 映射到JavaBean 的某个“复杂类型”属性,其他JavaBean类.
collection –复杂类型集合 (演示示例2)
--> <!--根据roleId获取用户列表: 当数据库中的字段信息与对象的属性不一致时需要通过resultMap来映射 -->
<!-- <resultMap type="User" id="seachUserResult">
<result property="id" column="id"/>
<result property="userCode" column="userCode"/>
<result property="userName" column="userName"/>
<result property="roleId" column="roleId"/>
<result property="roleName" column="roleName"/>
</resultMap> <select id="getUserListByRoleId" parameterType="Role" resultMap="seachUserResult">
select u.*,r.roleName as roleName from user u,role r where u.roleId = r.id and u.roleId = #{id}
</select> --> <!-- 根据roleId获取用户列表 association start-->
<resultMap type="User" id="seachUserResult">
<result property="id" column="id"/>
<result property="userCode" column="userCode" />
<result property="userName" column="userName" />
<result property="roleId" column="roleId" />
    //property=“属性名” javaType=“属性名的类型” ----(一对一)
<!-- <association property="role" javaType="Role" >
<result property="id" column="id"/>
<result property="roleCode" column="roleCode"/>
<result property="roleName" column="roleName"/>
</association> -->
<association property="role" javaType="Role" resultMap="roleMap"/>
</resultMap> <resultMap type="Role" id="roleMap">
<result property="id" column="id"/>
<result property="roleCode" column="roleCode"/>
<result property="roleName" column="roleName"/>
</resultMap> <select id="getUserListByRoleId" parameterType="Role" resultMap="seachUserResult">
select u.*,r.roleCode as roleCode,r.roleName as roleName from user u,role r where u.roleId = r.id and u.roleId = #{id}
</select> <!-- association end--> <!-- 获取指定用户的地址列表(user表-address表:1对多关系) collection start-->
<resultMap type="User" id="userMap">
<id property="id" column="userId"/>
    //property是属性名 ofType是List《Address》的类对象
<collection property="addressList" ofType="Address">
<id property="id" column="a_id"/>
<result property="postCode" column="postCode"/>
<result property="addressContent" column="addressContent"/>
</collection>
</resultMap> <select id="getAddressListByUserId" parameterType="User" resultMap="userMap">
select *,a.id as a_id from user u,address a where u.id=a.userId and u.id=#{id}
</select>
<!-- collection end --> <select id="count" resultType="int">
select count(1) from user
</select> <insert id="add" parameterType="User">
insert into user (userCode,userName,userPassword)
values (#{userCode},#{userName},#{userPassword})
</insert> <update id="update" parameterType="User">
update user set userCode=#{userCode},userName=#{userName},
userPassword=#{userPassword} where id=#{id}
</update> <delete id="delete" parameterType="User">
delete from user where id=#{id}
</delete> <select id="getUserList" resultType="User">
select * from user
</select>
</mapper>

接口 UserMapper

public interface UserMapper {

    public int count();

    public void add(User user);

    public void update(User user);

    public void delete(User user);

    public List<User> getUserList();

    //根据roleId获取用户列表
public List<User> getUserListByRoleId(Role role); //获取指定用户的地址列表(user表-address表:1对多关系)
public User getAddressListByUserId(User user); }

接口 RoleMapper

public interface RoleMapper {

    public void add(Role role);

    public void update(Role role);

    public void delete(Role role);

    public List<Role> getRoleList();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.bdqn.dao.RoleMapper">
<select id="getRoleList" resultType="Role">
select * from role
</select> <insert id="add" parameterType="Role">
insert into role (roleCode,roleName)
values (#{roleCode},#{roleName})
</insert> <update id="update" parameterType="Role">
update role set roleCode=#{roleCode},roleName=#{roleName}
where id=#{id}
</update> <delete id="delete" parameterType="Role">
delete from role where id=#{id}
</delete> </mapper>

User.java

public class User {
private Integer id;
private String userName;
private String userCode;
private String userPassword;
private Integer roleId;
//private String roleName; //collection
private List<Address> addressList; //association
private Role role; public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public List<Address> getAddressList() {
return addressList;
}
public void setAddressList(List<Address> addressList) {
this.addressList = addressList;
}
public Integer getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
/**
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
*/
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
} }

address.java

public class Address {
private Integer id;
private Integer postCode;
private String addressContent;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPostCode() {
return postCode;
}
public void setPostCode(Integer postCode) {
this.postCode = postCode;
}
public String getAddressContent() {
return addressContent;
}
public void setAddressContent(String addressContent) {
this.addressContent = addressContent;
} }

mybatis 使用resultMap实现关联数据的查询(association 和collection )的更多相关文章

  1. myBatis系列之四:关联数据的查询

    myBatis系列之三:增删改查是基于单表的查询,如果联表查询,返回的是复合对象,需要用association关键字来处理. 如User发表Article,每个用户可以发表多个Article,他们之间 ...

  2. mybatis实战教程(mybatis in action)之四:实现关联数据的查询

    有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如最常见到的多对一,一对多等.这些查询是如何处理的呢,这一讲就讲这个问题.我们首先创建一个Article 这个表 ...

  3. 4. mybatis实战教程(mybatis in action)之四:实现关联数据的查询

    转自:https://www.cnblogs.com/shanheyongmu/p/5653599.html 有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如 ...

  4. mybatis实战教程(mybatis in action)之四:实现关联数据的查询

    有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如最常见到的多对一,一对多等.这些查询是如何处理的呢,这一讲就讲这个问题.我们首先创建一个Article 这个表 ...

  5. Mybatis学习(4)实现关联数据的查询

    有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如最常见到的多对一,一对多等.这些查询是如何处理的呢,这一讲就讲这个问题.我们首先创建一个Article 这个表 ...

  6. SSM框架开发web项目系列(三) MyBatis之resultMap及关联映射

    前言 在上篇MyBatis基础篇中我们独立使用MyBatis构建了一个简单的数据库访问程序,可以实现单表的基本增删改查等操作,通过该实例我们可以初步了解MyBatis操作数据库需要的一些组成部分(配置 ...

  7. MyBatis3-实现多表关联数据的查询

    前提: 1.新建Article表和增加模拟数据,脚本如下: Drop TABLE IF EXISTS `article`; Create TABLE `article` ( `id` ) NOT NU ...

  8. Mybatis关联查询<association> 和 <collection>

    一.背景 1.在系统中一个用户存在多个角色,那么如何在查询用户的信息时同时把他的角色信息查询出来啦? 2.用户pojo: public class SysUser { private Long id; ...

  9. Mybatis:resultMap的使用总结

    resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中. resultMap包含的元素: <!--column不做限制,可以为任意 ...

随机推荐

  1. PowerDesigner打开设计文件后提示failed to read the fileXXX的解决办法

    擦,一身盗汗.一向的设计信息都在设计图里!竟然坏了,坏了!!!!! 惊.怒.悲 固然可以经由过程数据库当前状况反向工程.然则那么注解.我写的提示这些器材都邑消散. 比来的备份是10天前,恢复也会有必然 ...

  2. python 线性回归示例

    说明:此文的第一部分参考了这里 用python进行线性回归分析非常方便,有现成的库可以使用比如:numpy.linalog.lstsq例子.scipy.stats.linregress例子.panda ...

  3. JS 之DOM range对象

    DOM范围 DOM中的range对象是DOM2中新定义的接口.通过这个对象可以选择文档中的某个区域,而不必考虑节点的界限. 创建范围 document.createRange()创建一个范围,这个范围 ...

  4. 大前端时代已经到来!传智播客2015之WEB前端视频教程(全套教程共15G)

    大前端时代已经到来!传智播客2015之WEB前端视频教程(全套教程共15G)大前端时代已经到来!如今,前端开发工程师的职责,不是只有切图.制作网页这么简单哦! G:\传智播客2015-WEB前端视频教 ...

  5. GEOS库学习之五:与GDAL/OGR结合使用

    要学习GEOS库,肯定绕不开地理方面的东西.如果需要判断的两个多边形或几何图形,不是自己创建的,而是来自shapefile文件,那就得将GEOS库和GDAL/OGR库结合使用了.实际上只需要OGR就行 ...

  6. mysql命令行

    mysql -u root -p create database bookstore; drop database bookstore; use bookstore create table user ...

  7. 客户端禁用cookies后session是否还起效果

    设置session和cookies的代码(webform1.aspx) if (txtName.Text == "wlzcool") { Session["uid&quo ...

  8. 关于那些难改的bug

    多年的测试经验中,经常发现有这么一种现象:总有些提了的bug不能顺利的被修复.这些bug往往有4个走向: 1.在被发现的版本中最终被解决,但中途花费较多周折. 2.有计划的在后续的版本中被解决. 3. ...

  9. Linux下高频命令分类辑录(基本使用篇)

    本文目的:总结linux下常用命令的基本使用方法 文件权限: 文档权限设置命令:chmod 数字模式: 文档权限由-rwxrwxrwx十个字符组成,其中第一个代表文档类型,后面九个字符按照顺序分为三组 ...

  10. python3 入门 (二) 列表的使用

    列表用于组织其它数值,即写在方括号之间.用逗号分隔开的数值列表.列表内的项目不必全是相同的类型. 列表的定义 student = ['Tom', 'Jack', 'Avril'] 添加元素 将另一个列 ...