mybatis的动态sql中collection与assoction
User.java, Role.java,address.java为三个类
public class User {
.....//user自己的属性
//association一对一
private Role role;
//collection一对多
private List<Address> addressList;
}
//User的接口
//根据roleId获取用户列表assoction
public List<User> getUserListByRoleId(Role role);
//获取指定用户的地址列表collection
public User getAddressListByUserId(User user);
//sql
<!-- 根据roleId获取用户列表association start -->
<resultMap type="User" id="userMap">
<result property="id" column="id"/>
<result property="userCode" column="userCode"/>
<result property="userName" column="userName"/>
<!-- 方法一
<association property="role" javaType="Role">
<result property="id" column="r_id"/>
<result property="roleCode" column="roleCode"/>
<result property="roleName" column="roleName"/>
</association> -->
<!-- 方法二 -->
<association property="role" javaType="Role" resultMap="roleMap"></association>
</resultMap>
<resultMap type="Role" id="roleMap">
<result property="id" column="r_id"/>
<result property="roleCode" column="roleCode"/>
<result property="roleName" column="roleName"/>
</resultMap>
<select id="getUserListByRoleId" parameterType="Role" resultMap="userMap">
select u.*,r.id as r_id, r.roleName,r.roleCode from user u,role r
where u.roleId= r.id and r.id=#{id}
</select>
<!-- association end -->
<!-- 获取指定用户的地址列表collection 开始 -->
<resultMap type="User" id="searchMap">
<id property="id" column="userId"/>
<collection property="addressList" ofType="Address">
<!-- 此处id和result都可以 -->
<id property="id" column="a_id"/>
<result property="postCode" column="postCode"/>
<result property="addressContent" column="addressContent"/>
</collection>
</resultMap>
<select id="getAddressListByUserId" parameterType="User" resultMap="searchMap">
select *, a.id as a_id from user u,address a where u.id =a.userId and u.id=#{id}
</select>
<!-- collection end -->
为什么采用resultMap
1.resultMap仅用于联合查询并返回一个结果集,但属性名和数据库中的字段名不一致时
3.查询的属性结果集并不是都需要给用户
mybatis的动态sql中collection与assoction的更多相关文章
- mybatis在动态 SQL 中使用了参数作为变量,必须要用 @Param 注解
如果在动态 SQL 中使用了参数作为变量,那么就要用 @Param 注解,即使你只有一个参数.如果我们在动态 SQL 中用到了 参数作为判断条件,那么也是一定要加 @Param 注解的,例如如下方法: ...
- mybatis中的.xml文件总结——mybatis的动态sql
resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...
- MyBatis动态SQL中trim标签的使用
My Batis 官方文档 对 动态SQL中使用trim标签的场景及效果介绍比较少. 事实上trim标签有点类似于replace效果. trim 属性 prefix:前缀覆盖并增加其内容 suffix ...
- mybatis动态sql中的两个内置参数(_parameter和_databaseId)
mybatis动态sql中的两个内置参数(_parameter和_databaseId) <!-- mybatis动态sql的两个内置参数 不只是方法传递过来的参数可以被 ...
- mybatis动态sql中where标签的使用
where标记的作用类似于动态sql中的set标记,他的作用主要是用来简化sql语句中where条件判断的书写的,如下所示: <select id="selectByParams&qu ...
- MyBatis的动态SQL详解
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...
- mybatis 使用动态SQL
RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...
- (转)mybatis:动态SQL
概述:在mybatis中,动态语句是个非常强大和灵活的功能,并且动态语句可以放在sql的任何地方,利用该功能,我们可以写出非常灵活的代码.在mybatis的动态语句中常常可能会用到以下几个运算和逻辑判 ...
- MyBatis框架——动态SQL、缓存机制、逆向工程
MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...
随机推荐
- 非root用户Memcached repcached安装
安装memcached前先要确定系统是否安装了gcc: 1.解压安装包: tar -zxf memcached-1.2.8-repcached-2.2.tar.gz 2.编译: 系统应安装了libev ...
- centos7 安装R和Rstudio客户端
#官网下载R和Rstudio 我下载的是 R-3.2.1.tar.gz和rstudio-0.99.467-x86_64.rpm两个版本 rstudio没有看见有centos版的,下的这个RStudio ...
- oracle存储过程学习---包的概念
转自:http://www.iteye.com/topic/1111793 一.包的概念 类似于一个容器,能打包相应的Pl/SQL变量.常量.函数.过程.复合数据类型等元素到这个容器内.用来限制 ...
- xamarin mac 之 资料
中文社区 xamarin.form 30天学习计划
- jquery 编写插件入门
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <sc ...
- 解决Apache的错误日志巨大的问题以及关闭Apache web日志记录
调整错误日志的级别 这几天 apache错误日志巨大 莫名其妙的30G 而且 很多都是那种页面不存在的 网站太多了 死链接相应的也很多于是把错误警告调低了 因为写日志会给系统带来很大的损耗.关闭 ...
- 添加Action View
ActionBar上除了可以显示普通的Action Item之外,还可以显示普通的UI组件.为了在ActionBar上添加ActionView,可以使用如下两种方式. 定义ActionItem时使用a ...
- 去除 MyEclipse updating index
去除 MyEclipse updating index http://zhidao.baidu.com/link?url=OfHjTTxnNRoijnsaweBl3K3UTlnlFGdtHEQIvEW ...
- MyBatis 一级、二级缓存
一级 默认session就有一级缓存,session有C/U/D操作或者session.clearCache();session.commit();都会清空缓存: 二级在mapper.xml中添加&l ...
- FMS之Multi-point publishing技术
采用该技术,能large-scale你的直播系统,结构如图所示: A. Live Video B. Server 1 (New York City) C. Server 2 (Chicago) an ...