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 ...
随机推荐
- [ES6系列-01]Class:面向对象的“新仇旧恨”
[原创]CoderPower 大家好,这里是码路工人有力量,我是码路工人,你们是力量. 这是公众号(码路工人有力量)开通后的第二篇,写得还是有待改进吧.这次准备写一个关于ES6基础的短文系列,努力尽快 ...
- MVC案例之模糊查询与删除
查询操作: Servlet //1. 调用 CustomerDAO 的 getAll() 得到 Customer 的集合 List<Customer> customers ...
- 泛微 e-cology OA 前台SQL注入
poc https://github.com/AdministratorGithub/e-cology-OA-SQL 用法:python elog_sql.py http://target 不存在返回 ...
- 【docker系列2】docker 的前世今生
Docker 入门,共 3 篇,将带大家进入 Docker 的世界.首先了解 Docker 的发展历程, 然后快速掌握 Docker 的基本使用: Docker 版本及内核兼容性选择是这部分的重点内容 ...
- 【朝夕Net社区技术专刊】Core3.1 WebApi集群实战专题---WebApi环境搭建运行发布部署篇
欢迎大家阅读<朝夕Net社区技术专刊>第1期 原文是我在知乎发表的,现在在这里分享! 我们致力于.NetCore的推广和落地,为更好的帮助大家学习,方便分享干货,特创此刊!很高兴你能成为首 ...
- 跨域解决方案 - JSONP
目录 1. 定义 2. JSONP 解决跨域 3. 应用场景 4. 代码演示 1. 定义 在HTML 中, script 标签有两个个性质: script 标签可以不受同源策略的限制去访问服务器资源, ...
- js运算符和if语句,switch语句
逻辑运算符 类型 运算符 算数运算符 + - * / % ++ -- 赋值运算符 = 比较运算符 > < >= <= == != ...
- Java实现 LeetCode 817 链表组件(暴力)
817. 链表组件 给定一个链表(链表结点包含一个整型值)的头结点 head. 同时给定列表 G,该列表是上述链表中整型值的一个子集. 返回列表 G 中组件的个数,这里对组件的定义为:链表中一段最长连 ...
- Java实现 LeetCode 237 删除链表中的节点
237. 删除链表中的节点 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 – head = [4,5,1,9],它可以表示为: 示例 1: ...
- java中Dateformat类的详细使用(详解)
DateFormat其本身是一个抽象类,SimpleDateFormat 类是DateFormat类的子类,一般情况下来讲DateFormat类很少会直接使用,而都使用SimpleDateFormat ...