mapper.xml文件映射配置
一、导入约束
为全局配置文件绑定dtd约束:
1)联网会自动绑定
2)没网的时候【/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd】:解压mybatis 的jar包然后在eclipse中绑定
window——preperances——XML Catalog ——Add——设置key为http://mybatis.org/dtd/mybatis-3-mapper.dtd,绑定本地文件位置location:
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
二、mapper映射文件结构
<mapper namespace="mapper.buyrecordMapper"> <!--namespace 必须配置成接口的全限定名。Mybatis内部就是通过这讲接口和mapepr.xml关联起来的。接口中方法和mapper.xml 中的id对应。否则会报错--> resultMap (自定义映射结果集) sql标签 <select id=""></select>查询标签 <insert id=""></insert>插入标签 <update id=""></update>更新标签 <delete id=""></delete>删除标签
</mapper>
三、resultMap (自定义映射结果集)
<!--resultMap将数据库中的列(字段名)和entity中属性的名字建立映射关联,id 这个resultMap的唯一标识,type表示要映射的实体-->
<resultMap type="Goods" id="Map">
column:数据库字段,property:映射属性
id列:主键列,result普通列
<id column="id" property="id" />
<result column="name" property="name" />
<result column="flag" property="flag" />
多表查询:
一对一,多对一assonciation标签,用javatype映射
一对多,collection标签,用oftype映射
<association property="GoodsType" javaType="GoodsType">
<id column="id" property="id"/>
<result column="tname" property="name"/>
</association>
<!--***********分割线**************-->
<collection property="employees" ofType="employees">
<result column="ename" property="name" />
</collection>
</resultMap>
四、sql标签
<resultType="###"> <resultMap="###"> 二选一,没配置或配置错,执行程序必报错
<select id="finduser" resultType="User">
select * from account ;
</select>
<insert id="adduser" parameterType="user">
insert into
account(name,money) values(#{name},#{money})
</insert>
<update id="updateuser" parameterType="user">
update account set
name=#{name},money=#{money} where id=#{id}
</update>
<delete id="deluser" parameterType="int">
delete from account where
id=#{id}
</delete>
五、动态sql
< where >和< if >标签
< where > : 主要用来替换sql语句中的where字段,他的作用主要是用来简化sql语句中where条件判断的书写的
< if >:条件判断标签,配置属性test=" 条件字符串 ",判断是否满足条件,满足则执行,不满足则跳过
<select id="deptif" resultType="dept" parameterType="dept">
select * from dept
<where>
<if test="dname!=null and dname.trim()!=''">
and dname like concat('%',#{dname},'%')
</if>
<if test="loc!=null and loc.trim()!=''">
and loc=#{loc}
</if>
</where>
</select>
< set >标签
< set > : 主要用来替换sql语句中的set字段,一般在update中使用。
<update id="update" parameterType="Dept">
update dept
<set>
<if test="dname!=null">
dname=#{dname},
</if>
<if test="loc!=null">
loc=#{loc}
</if>
</set>
where id=#{id}
</update>
< trim>标签
< trim > : 是一个格式化的标记,可以完成set或者是where标记的功能
prefix:前缀
prefixoverride:去掉第一个and或者是or
<select id="depttrim" resultType="Dept" parameterType="Dept">
select * from dept
<trim prefix="where" suffixOverrides="and">
<if test="dname!=null and dname.trim()!=''">
dname like concat('%',#{dname},'%') and
</if>
<if test="loc!=null and loc.trim()!=''">
loc=#{loc}
</if>
</trim>
</select>
< choose >标签
< where > : choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。
当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。
类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。
<select id="deptchoose" resultType="Dept" parameterType="Dept">
select * from dept
<where>
<choose>
<when test="loc!=null">
loc=#{loc}
</when>
<when test="dname!=null and dname.trim()!=''">
dname like concat('%',#{dname},'%')
</when>
<otherwise>
id=#{id}
</otherwise>
</choose>
</where>
</select>
<foreach>标签
foreach标签经常用于遍历集合,构建in条件语句或者批量操作语句。
<select id="deptforeach" resultType="Dept">
select * from dept
<where>
<if test="list!=null and list.size()>0">
<foreach collection="list" separator="," open="id in("
close=")" item="uid">
#{uid}
</foreach>
</if>
</where>
</select>
<insert id="addProperties" parameterType="java.util.List">
INSERT INTO tdt_sync_instance_properties (name,instance_id) VALUES
<foreach collection="list" item="prop" separator=",">
(#{prop.name},#{prop.instance_id})
</foreach>
;
</insert>
mapper.xml文件映射配置的更多相关文章
- MyBatis的mapper.xml文件的参数问题:org.apache.ibatis.builder.IncompleteElementException: Could not find parameter map
配置参数类型有两种选择,即:parameterType和parameterMap 不管参数是否是基本数据类型还是map类型,都是使用parameterType. 版权声明:本文为博主原创文章,未经博主 ...
- (转)MyBatis框架的学习(四)——Mapper.xml文件中的输入和输出映射以及动态sql
http://blog.csdn.net/yerenyuan_pku/article/details/71893689 前面对MyBatis框架的学习中,我们对Mapper.xml映射文件多少有些了解 ...
- mybatis mapper xml文件配置resultmap时,id行和result行有什么区别?
mybatis mapper xml文件配置resultmap时,id行和result行有什么区别? <resultMap id = "CashInvoiceMap" typ ...
- Java数据持久层框架 MyBatis之API学习六(Mapper XML 文件详解)
对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...
- Java数据持久层框架 MyBatis之API学习五(Mapper XML 文件)
对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...
- 【MyBatis】Mapper XML 文件
Mapper XML文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立 ...
- MyBatis——Mapper XML 文件
Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会 ...
- Mybatis学习笔记(五) —— Mapper.xml(输入映射和输出映射)
一.parameterType(输入类型) 1.1 传递简单类型 <!-- 根据用户id查询用户 --> <select id="queryUserById" p ...
- 解决使用intellij idea开发MAVEN项目在target目录下不存在mapper.xml文件
原 解决使用intellij idea开发MAVEN项目在target目录下不存在mapper.xml文件 原文章链接:https://blog.csdn.net/beauxie/article/de ...
随机推荐
- 添加nginx新模块,获取客户真实ip
当前是客户端登录软件后台获取不到客户的真实ip而是云盾的代理ip 为了获取到真实ip后来发现通过配置nginx的read_ip模块就可以了 获取客户的真实ip使用Nginx的realip模块 当Ngi ...
- 国家集训队 部落战争 网络流最小路径覆盖 洛谷P2172
洛谷AC传送门! step1: 题目大意 有一张M x N的网格图,有一些点为“ * ”可以走,有一些点为“ x ”不能走,每走一步你都可以移动R * C 个格子(参考象棋中马的走法),且不能回头,已 ...
- 是时候扔掉cmder, 换上Windows Terminal
作为一个Windows的长期用户,一直没有给款好用的终端,知道遇到了 cmder,它拯救一个习惯用Windows敲shell命令的人. 不用跟我安利macOS真香!公司上班一直用macOS,一方面确实 ...
- POJ 2671 Jimmy's Bad Day题解(很详细很友好,类似区间dp)
有问题的话欢迎在评论区提出 题意: 题目链接 你是一个送快递的,现在给你一个环,环的边有权值,代表走这条边所花的时间,每个点代表一个地点,点有点权,代表这个点上有多少货物需要你送.初始时间\(t=0\ ...
- jchdl - RTL实例 - MOS6502 ALU
https://mp.weixin.qq.com/s/nMxYVC2djk7DdAforerZPA 使用jchdl RTL实现MOS6502 CPU的ALU. 参考链接 https://git ...
- Java实现 LeetCode 823 带因子的二叉树(DP)
823. 带因子的二叉树 给出一个含有不重复整数元素的数组,每个整数均大于 1. 我们用这些整数来构建二叉树,每个整数可以使用任意次数. 其中:每个非叶结点的值应等于它的两个子结点的值的乘积. 满足条 ...
- Java实现 LeetCode 636 函数的独占时间(栈)
636. 函数的独占时间 给出一个非抢占单线程CPU的 n 个函数运行日志,找到函数的独占时间. 每个函数都有一个唯一的 Id,从 0 到 n-1,函数可能会递归调用或者被其他函数调用. 日志是具有以 ...
- Java实现 LeetCode 450 删除二叉搜索树中的节点
450. 删除二叉搜索树中的节点 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变.返回二叉搜索树(有可能被更新)的根节点的引 ...
- SQL server 常用的数据库 DDL语言
use (数据库名) //切换到目标数据库 if exists (select * from sysdatabases where name='数据库名') //如果括号里面是查看有没有这个数据库 d ...
- java实现 洛谷 P1014 Cantor表
题目描述 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 - 2/1 2/2 2/3 2/4 - ...