<?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;//collectionprivate List<Address> addressList;//associationprivate 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;}}

转载于:https://www.cnblogs.com/xuerong/p/5000233.html

mybatis 使用resultMap实现关联数据的查询(association 和collection )相关推荐

  1. 空值判断,双表关联数据统计查询

    SQL空值判断 oracle的是NVL(value,nullvalue) pgsql的是COALESCE(value,nullvalue) mysql的是IFNULL(value,nullvalue) ...

  2. MyBatis关联嵌套映射(association、collection、discriminator)

    1.association属性(关联的嵌套映射) 第一个属性是"关联的嵌套映射",该属性的标签名称为"association".在resultMap中,当映射t ...

  3. Mybatis实现多表关联多条件查询

    第一种实现方式: 1.在实体类中添加需要展示的但在数据库表结构中字段 2.在mapper.xml的 <result column="project_name" jdbcTyp ...

  4. EF Core 关联数据

    开发环境 ASP.NET Core 3.0 + Entity Framework 3.0 正文 Entity Framework Core 通过实体类的导航属性来加载相关数据.有三种常见的方式: 预先 ...

  5. MyBatis使用resultMap自定义映射规则与关联映射

    一.写在前面 在MyBatis 的全局配置文件中我们可以通过在settings标签中设置 <setting name="mapUnderscoreToCamelCase" v ...

  6. MyBatis 实践 -动态SQL/关联查询

    MyBatis 实践 标签: Java与存储 动态SQL 动态SQL提供了对SQL语句的灵活操作,通过表达式进行判断,对SQL进行拼接/组装. if 对查询条件进行判断,如果输入参数不为空才进行查询条 ...

  7. 使用mybatis的resultMap进行复杂查询

    记录下mybatis的集合查询中碰到的问题 https://jaychang.iteye.com/blog/2357143 MyBatis ofType和javaType区别 https://blog ...

  8. MyBatis实现中间表关联查询

    MyBatis实现中间表关联查询 通常构建数据多对多模型的关系时,我们需要再建立一张中间表来关联另外两张表. 下面介绍一下,怎么用MyBatis来实现中间表关联查询并封装到实体类对象中. 现在有三张表 ...

  9. mybatis实现多对多关联查询(超详细版)

    mybatis实现多对多关联查询XML实现 ​ 在开发过程中,持久层架mybatis为我们封装了SQL操作,只需要提供相应的SQL语句即可查询出结果,若结合逆向工程插件便可免去写一些简单SQL的繁琐工 ...

最新文章

  1. rmd文件怎么转换html文件,提取.Rmd文件的html依赖项(包含htmlwidgets)
  2. 技术宅硬核跨年,DIY墨水屏日历:自动刷新位置、天气,随机播放2000多条「毒鸡汤」| 开源...
  3. Leetcode No.145 **
  4. 俄罗斯最大搜索引擎Yandex开源了一款梯度提升机器学习库CatBoost
  5. 删除python的注册表_Python操作注册表详细步骤介绍
  6. c++ 每半个小时打印一次_有了3D打印机,后期该如何维护呢
  7. 今日恐慌与贪婪指数为72 贪婪程度与昨日持平
  8. Broadwell I7-5775c/5675c BSOD 蓝屏问题
  9. window对象与document对象的区别
  10. 27款实用高效的腾讯热门开源项目推荐
  11. Ural 1043 Cover the Arc
  12. ODBC连接数据库实例
  13. 统一认证 ldap mysql_LDAP简述及统一认证说明
  14. 基于libVLC的视频播放器之五:抽帧
  15. 最新解决谷歌浏览器崩溃的方法
  16. 如何在 Android 上恢复删除屏幕截图/照片的四种方式
  17. 力扣(415.482)补8.28
  18. 苹果手用计算机解锁手机密码,苹果手机密码忘了怎么解锁(解决方法大全)
  19. linux中的文件夹压缩文件,linux将文件拷贝到目录下Linux下文件的压缩与打包详解...
  20. 第一届PyCon China小记

热门文章

  1. 【HTML】CSS基础知识
  2. div 重新加载_使用React的HOC来完成模块的异步加载
  3. eof怎么结束输入_西门子S71500数字量输入模块如何编址?
  4. 组合总和Python解法
  5. python数据分析报告主题_【原创】python主题LDA建模和t-SNE可视化数据分析报告论文(代码数据)...
  6. html5表单新增的输入类型,HTML5之表单新增类型介绍
  7. python数据接口设计_Python接口与归一化设计
  8. 只会用单片机点灯,很丢人吗?
  9. 嵌入式开发中常用的几种通信接口总结
  10. 天线的起源与发展历史