一、导入约束

为全局配置文件绑定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文件映射配置的更多相关文章

  1. MyBatis的mapper.xml文件的参数问题:org.apache.ibatis.builder.IncompleteElementException: Could not find parameter map

    配置参数类型有两种选择,即:parameterType和parameterMap 不管参数是否是基本数据类型还是map类型,都是使用parameterType. 版权声明:本文为博主原创文章,未经博主 ...

  2. (转)MyBatis框架的学习(四)——Mapper.xml文件中的输入和输出映射以及动态sql

    http://blog.csdn.net/yerenyuan_pku/article/details/71893689 前面对MyBatis框架的学习中,我们对Mapper.xml映射文件多少有些了解 ...

  3. mybatis mapper xml文件配置resultmap时,id行和result行有什么区别?

    mybatis mapper xml文件配置resultmap时,id行和result行有什么区别? <resultMap id = "CashInvoiceMap" typ ...

  4. Java数据持久层框架 MyBatis之API学习六(Mapper XML 文件详解)

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

  5. Java数据持久层框架 MyBatis之API学习五(Mapper XML 文件)

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

  6. 【MyBatis】Mapper XML 文件

    Mapper XML文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立 ...

  7. MyBatis——Mapper XML 文件

    Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会 ...

  8. Mybatis学习笔记(五) —— Mapper.xml(输入映射和输出映射)

    一.parameterType(输入类型) 1.1 传递简单类型 <!-- 根据用户id查询用户 --> <select id="queryUserById" p ...

  9. 解决使用intellij idea开发MAVEN项目在target目录下不存在mapper.xml文件

    原 解决使用intellij idea开发MAVEN项目在target目录下不存在mapper.xml文件 原文章链接:https://blog.csdn.net/beauxie/article/de ...

随机推荐

  1. let面试题

    两次输出结构都是2 0 1

  2. 《机器学习Python实现_09_02_决策树_CART》

    简介 CART树即分类回归树(classification and regression tree),顾名思义,它即能用作分类任务又能用作回归任务,它的应用比较广泛,通常会用作集成学习的基分类器,总得 ...

  3. Python数据分析:pandas玩转Excel (一)

    目录 1 pandas简介 2 导入 3 使用 4 读取.写入 1 pandas简介 1.Pandas是什么? Pandas是一个强大的分析结构化数据的工具集: 它的使用基础是Numpy(提供高性能的 ...

  4. HttpSession之表单的重复提交 & 验证码

    如果采用 HttpServletResponse.sendRedirct() 方法将客户端重定向到成功页面,将不会出现重复提交问题 1.表单的重复提交 1). 重复提交的情况: ①. 在表单提交到一个 ...

  5. sqlmap中文手册

    Sqlmap中文手册  -Darren制作 零.前言 Sqlmap是十分著名的.自动化的SQL注入工具.为了较为系统地学习Sqlmap,我决定翻译一遍Sqlmap的用户手册,于是便有了此文.由于我英语 ...

  6. c#tcp多线程服务器实例代码

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  7. Java实现 LeetCode 229 求众数 II(二)

    229. 求众数 II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2, ...

  8. Java实现 蓝桥杯VIP 算法提高 多项式输出

    算法提高 多项式输出 时间限制:1.0s 内存限制:512.0MB 问题描述 一元n 次多项式可用如下的表达式表示: f(x)=a[n]xn+a[n-1]x(n-1)+-+a[1]x+a[0], a[ ...

  9. Java实现LeetCode 139 单词拆分

    public boolean wordBreak(String s, List<String> wordDict) { if(s.length() == 0){ return false; ...

  10. Linux rsyslogd日志服务

    日志基本格式 基本日志格式包含四列: 事件发生的时间 发生事件的服务器的主机名 产生事件的服务名或程序名 事件的具体信息 /etc/rsyslog.conf配置文件 mail.*            ...