mybatis 使用resultMap实现关联数据的查询(association 和collection )
<?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 )的更多相关文章
- myBatis系列之四:关联数据的查询
myBatis系列之三:增删改查是基于单表的查询,如果联表查询,返回的是复合对象,需要用association关键字来处理. 如User发表Article,每个用户可以发表多个Article,他们之间 ...
- mybatis实战教程(mybatis in action)之四:实现关联数据的查询
有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如最常见到的多对一,一对多等.这些查询是如何处理的呢,这一讲就讲这个问题.我们首先创建一个Article 这个表 ...
- 4. mybatis实战教程(mybatis in action)之四:实现关联数据的查询
转自:https://www.cnblogs.com/shanheyongmu/p/5653599.html 有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如 ...
- mybatis实战教程(mybatis in action)之四:实现关联数据的查询
有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如最常见到的多对一,一对多等.这些查询是如何处理的呢,这一讲就讲这个问题.我们首先创建一个Article 这个表 ...
- Mybatis学习(4)实现关联数据的查询
有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如最常见到的多对一,一对多等.这些查询是如何处理的呢,这一讲就讲这个问题.我们首先创建一个Article 这个表 ...
- SSM框架开发web项目系列(三) MyBatis之resultMap及关联映射
前言 在上篇MyBatis基础篇中我们独立使用MyBatis构建了一个简单的数据库访问程序,可以实现单表的基本增删改查等操作,通过该实例我们可以初步了解MyBatis操作数据库需要的一些组成部分(配置 ...
- MyBatis3-实现多表关联数据的查询
前提: 1.新建Article表和增加模拟数据,脚本如下: Drop TABLE IF EXISTS `article`; Create TABLE `article` ( `id` ) NOT NU ...
- Mybatis关联查询<association> 和 <collection>
一.背景 1.在系统中一个用户存在多个角色,那么如何在查询用户的信息时同时把他的角色信息查询出来啦? 2.用户pojo: public class SysUser { private Long id; ...
- Mybatis:resultMap的使用总结
resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中. resultMap包含的元素: <!--column不做限制,可以为任意 ...
随机推荐
- JS实现点击跳转登陆邮箱
前言 注册的过程中往往需要填写邮箱,并登陆邮箱进行验证.利用JS可以实现针对不同的邮箱进行点击登录验证,以下为实现方案,很简单 代码 邮箱域名数据 1 2 3 4 5 6 7 8 9 10 11 ...
- PowerDesigner16建表在SQL SERVER 2008报 对象名 'sysproperties' 无效。
http://blog.itpub.net/30150152/viewspace-1454979/
- Java系列:关于Java中的桥接方法
这两天在看<Java核心技术 卷1>的泛型相关章节,其中说到了在泛型子类中override父类的泛型方法时,编译器会自动生成一个桥接方法,这块有点看不明白. 书上的例子代码如下: publ ...
- Linux操作系统基础(完结)
摘要 一.Linux操作系统概述 二.Linux操作系统安装 三.Linux文件系统及文件基础 四.Linux操作系统命令使用基础 五.Linux应用程序的安装与卸载基础 五.用户及进程 六.相关信息 ...
- [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点
2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...
- 我是怎么开发一个小型java在线学习网站的
2016/1/27 11:55:14 我是怎么开发一个小型java在线学习网站的 一直想做一个自己的网站(非博客),但是又不知道做什么内容的好,又一次看到了w3schools,就萌发了开发一个在线ja ...
- 我为什么期待M#?
前段时间的报导"微软将推新编程语言M#:系统编程级别的C#",第一眼看到并没有当初看到F#的那一种不安,反而感到欣喜,业界一直存在"语言论"讨论c#.java. ...
- LINQ浅析
在C# 3.0之前,我们对不同的数据源(数据集合.SQL 数据库.XML 文档等等)进行操作(查询.筛选.投影等等),会使用不同的操作方式. C# 3.0中提出了LINQ(Language Integ ...
- 八幅漫画理解使用JSON Web Token设计单点登录系统
用jwt这种token的验证方式,是不是必须用https协议保证token不被其他人拦截? 是的.因为其实只是Base64编码而已,所以很容易就被解码了.如果你的JWT被嗅探到,那么别人就可以相应地解 ...
- FusionCharts的使用方法
来源于:http://www.cnblogs.com/xuhongfei/archive/2013/04/12/3016882.html 今天统计价格变化规律的时候找到的一个很好的文档,很详细 一.简 ...