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不做限制,可以为任意 ...
随机推荐
- 使用ObjectAnimator设置动画
ObjectAnimator是ValueAnimator的子类,他本身就已经包含了时间引擎和值计算,所以它拥有为对象的某个属性设置动画的功能.这使得为任何对象设置动画更加的容易.你不再需要实现 Val ...
- MVC5 + EF6 + Bootstrap3 (8) HtmlHelper用法大全(上)
文章来源:Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-httphelper-part1.html 上一节 ...
- WPF 得到子指定元素方法和得到指定子元素集合方法MvvM得到焦点
public class UIHelper { /// <summary> /// 在Visual里找到想要的元素 /// childName可为空,不为空就按名字找 /// </s ...
- 无光驱安装原版 windows server2008,win7 的方法,64位的。
这几天要对一台服务器进行安装 windows server2008的系统,64位.尼玛在网上买了一个光驱迟迟不到所以只能用U盘来了 以前安装ghost的系统U盘分分钟搞定.安装原版的iso文件遇到了一 ...
- Xamarin.Forms 现已开启对 UWP 的支持
Xamarin.Forms 现已升级到 2.0.0.6482 , 正式开启了对 UWP 的支持. 要创建 UWP 项目, 必须是 VS2015, WIN8.1 下也可以, 但是只有 Windows 1 ...
- 关于python中PIL的安装
python 的PIL安装是一件很蛋痛的事, 如果你要在python 中使用图型程序那怕只是将个图片从二进制流中存盘(例如使用Scrapy 爬网存图),那么都会使用到 PIL 这库,而这个库是出名的难 ...
- Dropbox的可用Hosts文件
108.160.167.203 www.dropbox.com 108.160.167.203 dropbox.com 108.160.165.211 dl-client677.dropbox.com ...
- Bootstrap系列 -- 14. 表单控件输入框input
每一个表单都是由表单控件组成.离开了控件,表单就失去了意义.接下来的我们简单的来了解Bootstrap框架中表单控件的相关知识. 单行输入框,常见的文本输入框,也就是input的type属性值为tex ...
- 多个相同name的文本输入框,输入其中一个后,使剩下的不能输入值
可以用blur或keyup事件响应: 实现一: <body> <input type="text" id="AfterOtOt1" name= ...
- .net截取两个字符串中间的内容
做模拟登录时,需要截取html代码中的名字,返回的字符串内容如下 <span class="welcome">您好<span style="font-s ...