MyBatsi-Mapper映射文件
Mapper映射文件
- cache – 给定命名空间的缓存配置。
- cache-ref – 其他命名空间缓存配置的引用。
- resultMap – 是最复杂也是最强大的元素,用来描述如何从数据库结果集中来加载对象。
- parameterMap – 已废弃!老式风格的参数映射。内联参数是首选,这个元素可能在将来被移除,这里不会记录。
- sql – 可被其他语句引用的可重用语句块。
- insert – 映射插入语句
- update – 映射更新语句
- delete – 映射删除语句
- select – 映射查询语句
mapper文件与mapper 接口的配置说明
<mapper namespace="vallue " ></mapper>
- 针对Configuration中不同mapper映射器,这里的名称空间定义不同:
当使用相对于类路径的资源引用和完全限定资源定位符(URL)映射器时,namespace=”valeu” value的值可以任意指定,并且mapper映射文件的位置可以任意指定.
<mapper namespace="nnn" ></mapper>
- 当使用使用映射器接口实现类的完全限定类名和包内的映射器接口实现全部注册为映射器时, namespace=”valeu” value的值必须指定为mapper接口类型的权限名称且mapper映射文件必须和mapper接口在一个包中
<mapper namespace="com.lifeibai.dao.UserDao" ></mapper>

元素说明
<mapper namespace="vallue " > 这里是mapper的元素</mapper>
第一部分insert update delete
- insert – 映射插入语句
- update – 映射更新语句
- delete – 映射删除语句

主键生成策略

第二部分select

第三部分sql 与字符串拼接

Sql
<!-- 自定义条件查询用户列表 -->
<sql id="sometable">
${prefix}Table
</sql>
<sql id="someinclude">
from
<include refid="${include_target}"/>
</sql>
<select id="select" resultType="map">
select
field1, field2, field3
<include refid="someinclude">
<property name="prefix" value="Some"/>
<property name="include_target" value="sometable"/>
</include>
</select>
字符串拼接替换#{}和${}
<!-- 自定义条件查询用户列表 -->
<select id="findUserByUsername" parameterType="java.lang.String"
resultType="cn.itcast.mybatis.po.User">
select * from user where username like '%${value}%'
</select>
#{}和${}
#{}表示一个占位符号,通过#{}可以实现preparedStatement向占位符中设置值,自动进行java类型和jdbc类型转换,#{}可以有效防止sql注入。 #{}可以接收简单类型值或pojo属性值。 如果parameterType传输单个简单类型值,#{}括号中可以是value或其它名称。
${}表示拼接sql串,通过${}可以将parameterType 传入的内容拼接在sql中且不进行jdbc类型转换, ${}可以接收简单类型值或pojo属性值,如果parameterType传输单个简单类型值,${}括号中只能是value。
第四部分resultMap

resultMap的解决问题:
1) po类字段与数据表中的列名不一致
2) 一对一关系
3) 一对多关系
4) 多对多
5) 根据条件封装不同结果
ResultMap的子元素
- constructor - 用于在实例化类时,注入结果到构造方法中
- idArg - ID 参数;标记出作为 ID 的结果可以帮助提高整体性能
- arg - 将被注入到构造方法的一个普通结果
- id – 一个 ID 结果;标记出作为 ID 的结果可以帮助提高整体性能
- result – 注入到字段或 JavaBean 属性的普通结果
- association – 一个复杂类型的关联;许多结果将包装成这种类型
- 嵌套结果映射 – 关联可以指定为一个 resultMap 元素,或者引用一个
- collection – 一个复杂类型的集合
- 嵌套结果映射 – 集合可以指定为一个 resultMap 元素,或者引用一个
- discriminator – 使用结果值来决定使用哪个 resultMap
- case – 基于某些值的结果映射
- 嵌套结果映射 – 一个 case 也是一个映射它本身的结果,因此可以包含很多相 同的元素,或者它可以参照一个外部的 resultMap。
- case – 基于某些值的结果映射
id&result/constructor

一对一association

一对多collection

鉴别器discriminator

<resultMap id="vehicleResult" type="Vehicle">
<id property="id" column="id" />
<result property="vin" column="vin"/>
<result property="year" column="year"/>
<result property="make" column="make"/>
<result property="model" column="model"/>
<result property="color" column="color"/>
<discriminator javaType="int" column="vehicle_type">
<case value="1" resultType="carResult">
<result property="doorCount" column="door_count" />
</case>
<case value="2" resultType="truckResult">
<result property="boxSize" column="box_size" />
<result property="extendedCab" column="extended_cab" />
</case>
<case value="3" resultType="vanResult">
<result property="powerSlidingDoor" column="power_sliding_door" />
</case>
<case value="4" resultType="suvResult">
<result property="allWheelDrive" column="all_wheel_drive" />
</case>
</discriminator>
</resultMap>
第五部分cache

第六部分 动态sql
- if
- choose (when, otherwise)
- trim (where, set)
- foreach
if
做条件判断的,如果我们不使用这个标签,我们肯定会在代码中判断如查询的元素是否为空,传入的元素是否为空,而这时我们直接使用这个标签,就减少了代码的书写
<!-- 传递pojo综合查询用户信息 -->
<select id="findUserList" parameterType="user" resultType="user">
select * from user
where 1=1
<if test="id!=null">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
concat(‘%’,#{username},’%’)
</if>
</select>
choose (when, otherwise)
对于这类标签,就是采用多个选项中找一个,就像单项选择题,但是你不会都选择,只会从中选择1个来作为条件。就有点类似于switch。。case。
<select id="findActiveBlogLike" resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
trim (where, set)
如果使用第一个if语句的sql有and的话,就会发现没有写where标签就会报错。而这类标签通常是搭配条件标签使用的。
<select id="findUserList" parameterType="user" resultType="user">
select * from user
<where>
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</where>
</select>
foreach

MyBatsi-Mapper映射文件的更多相关文章
- 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap
上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select ...
- 深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete
上篇文章<深入浅出Mybatis系列(六)---objectFactory.plugins.mappers简介与配置>简单地给mybatis的配置画上了一个句号.那么从本篇文章开始,将会介 ...
- 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap good
上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select ...
- mapper映射文件不发布
mapper映射文件不发布的问题:在pom.xml中配置,指定加载哪些资源 <resources> <resource> <directory>src/main/j ...
- IDEA maven项目下测试mybatis例子,使用mappper class或package引入mapper映射文件,总是报错Invalid bound statement(所有配置完全正确)
困扰几个小时,终于查到解决办法及原因(可以直接到最后看解决方案) 环境就是用IDEA搭建的maven项目,主要jar包引入配置如下 <dependencies> <dependenc ...
- Mybatis学习系列(二)Mapper映射文件
Mapper映射文件,作用是用来配置SQL映射语句,根据不同的SQL语句性质,使用不同的标签,mapper文件中常用的标签有<iselect>.<insert>.<upd ...
- 命令+mybatis-generator插件自己主动生成Mapper映射文件
学mybatis的时候,自己写各种 *Mapper.xml和 *Mapper.java,注意各种sql语句中的 id 是否匹配.xml中的namespace是否正确,非常麻烦有木有?今天博客内容就是高 ...
- 二、Mapper映射文件
Mapper映射文件 mapper.xml映射文件主要是用来编写SQL语句的,以及一些结果集的映射关系的编写,还有就是缓存的一些配置等等. 在映射文件里面可以配置以下标签: 元素名称 描述 备注 se ...
- MyBatis 体系结构、根配置文件、Mapper映射文件
一.MyBatis的体系结构 1.SqlSessionFactory对象 SqlSessionFactory对象是MyBatis的管理核心,它是单个数据库映射关系经过编译后的内存镜像,是创建SqlSe ...
- mybatis的mapper映射文件
1概述1.1应用架构 mybatis框架用于支持对关系数据库的操作,该体系的应用架构如下图所示: 在mybatis框架体系中,主要的组件是:SqlSessionFactoryBean和Mapp ...
随机推荐
- Boom!!!计算机系统,从理解到爆炸,Bomblab
进入文件夹下 ./bomb 開始执行炸弹 对于炸弹command not found之类的鬼畜情况: chmod 777 bomb 然后再执行炸弹 objdump -d bomb > bomb. ...
- CentOS笔记-yum
yum( Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器. yum [options] [command] [p ...
- MapReduce算法形式一:WordCount
MapReduce算法形式一:WordCount 这种形式可以做一些网站登陆次数,或者某个电商网站的商品销量啊诸如此类的,主要就是求和,但是求和之前还是要好好清洗数据的,以免数据缺省值太多,影响真实性 ...
- Rowkey is the Crux Rowkey Design
Apache HBase ™ Reference Guide http://hbase.apache.org/book.html#rowkey.design The Effect of ColumnF ...
- OOalv 实现带出栏位描述
.类定义 CLASS lcl_event_handler DEFINITION. PUBLIC SECTION. METHODS: handle_data_changed_finished FOR E ...
- express 中文文档
express() 创建一个express应用程序 var express = require('express'); var app = express(); app.get('/', functi ...
- 有关定时器setTimeout()、setInterval()详解
JavaScript提供定时执行代码的功能,叫做定时器(timer),主要由setTimeout()和setInterval()这两个函数来完成. setTimeout() setTimeout函数用 ...
- myeclipse8.6注册码
loveyLR8ZC-855550-69545856608357821
- Android Platform Version与API Level的对应表
Platform Version API Level VERSION_CODE Notes Android 6.0 23 M Platform Highlights Android 5.1 22 LO ...
- hel软工网络16个人作业1
1Task1:注册个人博客账号 1Task2:注册码云账号 1Task3:提出问题 3.1问题一:软件工程是什么? 在第一章中我们可以从P8得到: 1.软件工程就是把系统的.有序的.可量化的方法应用到 ...