一、输入映射和输出映射

1. parameterType(输入类型)

1.1 传递简单类型

 <select id="getUserById" parameterType="int" resultType="com.cenobitor.pojo.User">
  SELECT `id`,`username`,`birthday`,`sex`,`address` FROM `user` WHERE id = #{id}
</select>

1.2 传递pojo对象

 <insert id="insertUser" parameterType="com.cenobitor.pojo.User">
INSERT INTO USER (`username`,`birthday`,`sex`,`address`)
VALUES (#{username},#{birthday},#{sex},#{address})
</insert>

Mybatis使用ognl表达式解析对象字段的值,#{}或者${}括号中的值为pojo属性名称。

1.3 传递pojo包装对象

 <!-- 1、resultType:如果要返回数据集合,只需设定为每一个元素的数据类型
 2、 包装的pojo取值通过 "."来获取,如取包装的pojo中user属性对象里的username属性的表达式为:user.username
-->
<select id="getUserByQueryVo" parameterType="queryvo" resultType="com.cenobitor.mybatis.pojo.User">
  SELECT * FROM USER WHERE username LIKE '%${user.username}%'
</select>

2. resultType(输出类型)

2.1 输出简单类型

 <!-- 查询用户总记录数,演示返回简单类型 -->
<select id="getUserCount" resultType="int">
  SELECT COUNT(1) FROM USER
</select>

2.2 输出pojo对象:

 <select id="getUserById" parameterType="int" resultType="com.cenobitor.pojo.User">
  SELECT `id`,`username`,`birthday`,`sex`,`address` FROM `user` WHERE id = #{id}
</select>

2.3输出pojo列表

 <select id="getUserById" parameterType="int" resultType="com.cenobitor.pojo.User">
  SELECT `id`,`username`,`birthday`,`sex`,`address` FROM `user` WHERE sex = #{sex}
</select>

2.4 输出resultMap

表字段与pojo属性不一致时引出的resultMap

 <!-- resultMap入门
  type:映射成的pojo类型
  id:resultMap唯一标识
-->
<resultMap type="order" id="orderMap">   <!-- id标签用于绑定主键 -->
  <id property="id" column="id"/>   <!-- 使用result绑定普通字段 -->
  <result property="number" column="number"/>
  <result property="createtime" column="createtime"/>
  <result property="note" column="note"/>
</resultMap> <!-- 使用resultMap -->
<select id="getOrderListResultMap" resultMap="orderMap">
  SELECT * FROM `order`
</select>
二、动态sql

2.1  If

由多查询条件拼装引出if标签。

 <!-- 演示动态sql-if标签的使用情景 -->
<select id="getUserByWhere" parameterType="user" resultType="com.cenobitor.mybatis.pojo.User">
  SELECT * FROM USER where 1 = 1
  <!-- if标签的使用 -->
  <if test="id != null">
    and id = #{id}
  </if>
  <if test="username != null and username != ''">
    and username LIKE '%${username}%'
  </if>
</select>

2.2 Where

 <!-- 演示动态sql-where标签的使用情景 -->
<select id="getUserByWhere2" parameterType="user" resultType="com.cenobitor.mybatis.pojo.User">
  <!-- include:引入sql片段,refid引入片段id -->
  SELECT * FROM USER
  <!-- where会自动加上where同处理多余的and -->
  <where>
    <!-- if标签的使用 -->
    <if test="id != null">
      and id = #{id}
    </if>
    <if test="username != null and username != ''">
      and username LIKE '%${username}%'
    </if>
  </where>
</select>

2.3 Foreach

 <!-- 演示动态sql-foreach标签的使用情景 -->
<select id="getUserByIds" parameterType="queryvo" resultType="com.cenobitor.mybatis.pojo.User">
  SELECT * FROM USER
  <!-- where会自动加上where同处理多余的and -->
  <where>
    <!-- id IN(1,10,25,30,34) -->
    <!-- foreach循环标签
       collection:要遍历的集合,来源入参
       open:循环开始前的sql
       separator:分隔符
       close:循环结束拼接的sql
    -->
    <foreach item="uid" collection="ids" open="id IN(" separator="," close=")">
      #{uid}
    </foreach>
  </where>
</select>

2.4 Sql片段

演示通过select * 不好引出查询字段名,抽取共用sql片段。

① 定义

 <!-- sql片段 定义,id:片段唯一标识 -->
<sql id="user_column">
  `id`, `username`, `birthday`, `sex`, `address`, `uuid2`
</sql>

② 使用

 SELECT
<!-- sql片段的使用:include:引入sql片段,refid引入片段id -->
<include refid="user_column" />
FROM USER
三、关联查询

3.1 一对一关联

① 方法一,使用resultType

 <!-- 一对一关联查询,使用resultType -->
<select id="getOrderUser" resultType="orderuser">
  SELECT
    o.`id`, o.`user_id` userId, o.`number`, o.`createtime`, o.`note`, u.`username`, u.`address`
  FROM `order` o
  LEFT JOIN `user` u
  ON u.id = o.`user_id`
</select>

②方法二,使用resultMap

 <!-- 一对一关联查询-resultMap -->
<resultMap type="order" id="order_user_map">
  <!-- id标签用于绑定主键 -->
  <id property="id" column="id"/>
  <!-- 使用result绑定普通字段 -->
  <result property="userId" column="user_id"/>
  <result property="number" column="number"/>
  <result property="createtime" column="createtime"/>
  <result property="note" column="note"/>
  <!-- association:配置一对一关联
    property:绑定的用户属性
    javaType:属性数据类型,支持别名
   -->
  <association property="user" javaType="com.cenobitor.mybatis.pojo.User">
    <id property="id" column="user_id"/>
    <result property="username" column="username"/>
    <result property="address" column="address"/>
    <result property="sex" column="sex"/>
  </association>
</resultMap> <!-- 一对一关联查询-使用resultMap -->
<select id="getOrderUser2" resultMap="order_user_map">
  SELECT
  o.`id`,o.`user_id`, o.`number`, o.`createtime`, o.`note`, u.`username`, u.`address`, u.`sex`
  FROM `order` o
  LEFT JOIN `user` u
  ON u.id = o.`user_id`
</select>

3.2一对多关联

 <!-- 一对多关联查询 -->
<resultMap type="user" id="user_order_map">
  <id property="id" column="id" />
  <result property="username" column="username" />
  <result property="birthday" column="birthday" />
  <result property="address" column="address" />
  <result property="sex" column="sex" />
  <result property="uuid2" column="uuid2" />
  <!-- collection:配置一对多关系
    property:用户下的order属性
    ofType:property的数据类型,支持别名
  -->
  <collection property="orders" ofType="order">
    <!-- id标签用于绑定主键 -->
    <id property="id" column="oid"/>
    <!-- 使用result绑定普通字段 -->
    <result property="userId" column="id"/>
    <result property="number" column="number"/>
    <result property="createtime" column="createtime"/>
  </collection>
</resultMap>
<!-- 一对多关联查询 -->
<select id="getUserOrder" resultMap="user_order_map">
  SELECT
  u.`id`, u.`username`,u.`birthday`,u.`sex`,u.`address`,u.`uuid2`,o.`id` oid,o.`number`,o.`createtime`
  FROM `user` u
  LEFT JOIN `order` o
  ON o.`user_id` = u.`id`
</select>

MyBatis入门(二)—— 输入映射和输出映射、动态sql、关联查询的更多相关文章

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

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

  2. 【mybatis深度历险系列】mybatis中的输入映射和输出映射

    在前面的博文中,小编介绍了mybatis的框架原理以及入门程序,还有mybatis中开发到的两种方法,原始开发dao的方法和mapper代理方法,今天博文,我们来继续学习mybatis中的相关知识,随 ...

  3. Mybatis学习总结(四)——输入映射和输出映射

    在前面几篇文章的例子中也可以看到mybatis中输入映射和输出映射的身影,但是没有系统的总结一下,这篇博客主要对这两个东东做一个总结.我们知道mybatis中输入映射和输出映射可以是基本数据类型.ha ...

  4. 【MyBatis学习06】输入映射和输出映射

    在前面几篇博文的例子中也可以看到mybatis中输入映射和输出映射的身影,但是没有系统的总结一下,这篇博客主要对这两个东东做一个总结.我们知道mybatis中输入映射和输出映射可以是基本数据类型.ha ...

  5. (七)mybatis 的输入映射与输出映射

    目录 前言 输入映射 - - 传递 pojo 包装类型 (复杂查询 ) 输出映射 - - resultType 输出映射 - - resultMap 前言 通过 paramterType 指定输入参数 ...

  6. 8.mybatis动态SQL模糊查询 (多参数查询,使用parameterType)

    多参数查询,使用parameterType.实例: 用户User[id, name, age] 1.mysql建表并插入数据 2.Java实体类 public class User { public ...

  7. mybatis入门基础(四)----输入映射和输出映射

    一:输入映射 通过parameterType指定输入参数的类型,类型可以是简单类型.hashmap.pojo的包装类型. 1.1.传递pojo的包装对象 1.1.1.需求描述 完成用户信息的综合查询, ...

  8. mybatis基础系列(二)——基础语法、别名、输入映射、输出映射

    增删改查 mapper根节点及其子节点 mybatis框架需要读取映射文件创建会话工厂,映射文件是以<mapper>作为根节点,在根节点中支持9个元素,分别为insert.update.d ...

  9. Mybatis输入映射和输出映射

    本节内容: 输入参数映射 输出映射 resultMap Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心. 一.环境准备 复制 ...

随机推荐

  1. ng的点滴记录

    1,directive http://damoqiongqiu.iteye.com/blog/1917971/ 2,constructor  https://segmentfault.com/q/10 ...

  2. 深度了解git工具

    今天给大家介绍一个深度了解git的学习视频,视频讲的挺好的,可以听听. 深度了解git学习视频

  3. 【UOJ#435】【集训队作业2018】Simple Tree 分块+树链剖分

    题目大意: 有一棵有根树,根为 1 ,点有点权.现在有 m 次操作,操作有 3 种:1 x y w ,将 x 到 y 的路径上的点点权加上 w (其中 w=±1w=±1 ):2 x y ,询问在 x ...

  4. OAuth 2.0 - Authorization Code授权方式详解

    I:OAuth 2.0 开发前期准备 天上不会自然掉馅饼让你轻松地去访问到人家资源服务器里面的用户数据资源,所以你需要做的前期开发准备工作就是把AppKey, AppSecret取到手 新浪获取传送门 ...

  5. selenium+java iframe定位

      关于 driver.switchTo().frame(参数).这中间的参数表达有以下几种方式. driver.switchTo().frame(0):用<iframe>标签的位置数量来 ...

  6. nginx添加认证

    1.检查工具是否安装,如果未安装则使用yum安装 #htpasswd 有以上输出表示已经安装,如果没有按装,使用如下命令安装: #yum -y  install httpd-tools 2.htpas ...

  7. Java Web入门学习(三)Maven的配置与使用国内仓库

    一.Maven下载与配置 下载好的压缩包,压缩到D盘,或者其他盘符. 新建系统环境变量 MAVEN_HOME  值是压缩好的Maven目录. 比如: D:\apache-maven- 在path环境变 ...

  8. 2018春招-今日头条笔试题-第一题(python)

    题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) 解题思路: 要想得到输入的数字列中存在相隔为k的数,可以将输入的数字加上k,然后判断其在不在输入的数字列中即可. #-*- cod ...

  9. 暴力攻击 PHP 脚本 初探

    考虑下面的HTML表单: CODE: <form action="http://example.org/login.php" method="POST"& ...

  10. 常用CSS实例

    为表格设置合并边框模型: border-collapse:collapse 规定单元格之间的空间: cellspacing:0 规定内侧边框的哪个部分是可见的: rules:all