MyBatis的强大,主要原于它强大映射功能,相对其它的jdbc,使用MyBatis,你会发现省掉很多代码。上一篇已经简单做出一个实例。今天就了解一下MyBatis的映射xml文件。

了解上一篇friendMapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--POJO的mapper接口如FriendMapper的类路径-->
<mapper namespace="com.yiming.mapper.FriendMapper">
<!--
insert映射插入语句,
id 唯一的标识符,必须与FriendMapper定义接口方法中相同
parameterType 参数类型,参数类的完全限定名或别名
-->
<insert id="inertFriend" parameterType="friend">
insert into friend(id,friendName, sex) values(#{id},#{friendName}, #{sex})
</insert>
<!--
delete映射删除语句
id 唯一的标识符,必须与FriendMapper定义接口方法中相同
parameterType 参数类型,参数类的完全限定名或别名
-->
<delete id="deleteFriend" parameterType="long">
delete from friend where id= #{id}
</delete>
<!--
update映射更新语句
id 唯一的标识符,必须与FriendMapper定义接口方法中相同
parameterType 参数类型,参数类的完全限定名或别名
-->
<update id="updateFriend" parameterType="friend">
update friend set friendName = #{friendName}, sex = #{sex} where id= #{id}
</update>
<!--
select映射查询语句
id 唯一的标识符,必须与FriendMapper定义接口方法中相同
parameterType 参数类型,参数类的完全限定名或别名
resultType 返回值类型,类的完全限定名或别名。注意如果是集合情形,
那应该是集合可以包含的类型,而不能是集合本身。
使用 resultType 或 resultMap,但不能同时使用。
-->
<select id="getFriend" parameterType="long" resultType="friend">
select id,friendName, sex from friend where id = #{id}
</select> <select id="getAllFrined" parameterType="string" resultType="friend">
select id, friendName, sex from friend
where friendName like concat('%', #{friendName}, '%')
</select>
</mapper>

引入friendMapper.xml映射文件方式

在说引入方式前,先要知道引入映射文件的位置,是mybatis-config.xml文件中:
 <!--映射文件-->
<mappers>
<mapper resource="com/yiming/mapper/friendMapper.xml"/>
</mappers>

1、文件路径引入映射文件

   <mappers>
<mapper resource="com/yiming/mapper/friendMapper.xml"/>
</mappers>
2、类注册方式引入
    <mappers>
<mapper class="com.yiming.mapper.FriendMapper"/>
</mappers>  
3、使用完全限定资源定位url引入
   <mappers>
<mapper url="file:///var/com/yiming/mapper/friendMapper.xml"/>
</mappers> 
4、批量引入,也叫包名引入
    <mappers>
<package name="com.yiming.mapper"/>
</mappers>
 

MyBatis学习总结(三)---映射文件及引入方式的更多相关文章

  1. Mybatis学习--Mapper.xml映射文件

    简介 Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心. 映射文件中有很多属性,常用的就是parameterType(输入类型 ...

  2. mybatis学习------打包xml映射文件

    编译mybatis时,idea不会将mybatis的xml映射文件一起打包进jar,即在编译好的jar包里缺少mybatis映射文件,导致网站加载失败 为解决这个问题,可在mybatis对应modul ...

  3. MyBatis学习系列三——结合Spring

    目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring MyBatis在项目中应用一般都要结合Spring,这一章主要把MyBat ...

  4. MyBatis学习 之 三、动态SQL语句

    目录(?)[-] 三动态SQL语句 selectKey 标签 if标签 if where 的条件判断 if set 的更新语句 if trim代替whereset标签 trim代替set choose ...

  5. 【转】MyBatis学习总结(三)——优化MyBatis配置文件中的配置

    [转]MyBatis学习总结(三)——优化MyBatis配置文件中的配置 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的con ...

  6. MyBatis 源码分析 - 映射文件解析过程

    1.简介 在上一篇文章中,我详细分析了 MyBatis 配置文件的解析过程.由于上一篇文章的篇幅比较大,加之映射文件解析过程也比较复杂的原因.所以我将映射文件解析过程的分析内容从上一篇文章中抽取出来, ...

  7. java之jvm学习笔记三(Class文件检验器)

    java之jvm学习笔记三(Class文件检验器) 前面的学习我们知道了class文件被类装载器所装载,但是在装载class文件之前或之后,class文件实际上还需要被校验,这就是今天的学习主题,cl ...

  8. python 全栈开发,Day108(客户管理之权限控制,客户管理之动态"一级"菜单,其他应用使用rbac组件,django static文件的引入方式)

    一.客户管理之权限控制 昨天的作业,有很多不完善的地方 下载代码,基本实现权限验证 https://github.com/987334176/luffy_permission/archive/v1.2 ...

  9. Objective-C:三种文件导入的方式以及atomic和nonatomic的区别

    一.三种文件导入的方式比较:   类的前项声明@class.import.include: 1.采用@class 类名的方式,它会告诉编译器有这么一个类,目前不需要知道它内部的实例变量和方法是如何定义 ...

随机推荐

  1. bzoj 4514: 数字配对

    题目大意 自己看 题解 我们打表观察规律发现一定能构成一张二分图 也就是不存在奇环 所以我们一般保证费用非负的最大流即可. #include <cstdio> #include <c ...

  2. 【Lintcode】076.Longest Increasing Subsequence

    题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...

  3. Ubuntu下Apache重启错误:Could not reliably determine解决

    错误信息:apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 ...

  4. 深入浅出Javascript的正则表达式

    深入浅出的javascript的正则表达式学习教程 阅读目录 了解正则表达式的方法 了解正则中的普通字符 了解正则中的方括号[]的含义 理解javascript中的元字符 RegExp特殊字符中的需要 ...

  5. Python3解leetcode Best Time to Buy and Sell Stock II

    问题描述: Say you have an array for which the ith element is the price of a given stock on day i. Design ...

  6. 将hive搭建到spark上

    1. 首先搭建好spark和hive,参见相关文档 2. 在spark/conf下创建hive-site.xml <configuration> <property> < ...

  7. maven+eclpse+jmeter+jenkis

    1.maven配置:http://www.cnblogs.com/AlanLee/p/6133189.html 2.在eclipse中创建maven项目:https://jingyan.baidu.c ...

  8. 如何使用Git命令将项目从github或者服务器上克隆下来

    在本地新建一个文件夹,作为本地仓库,如“demo”.单击右键git Bush here,打开git,输入命令: cd /c/Users/Administrator/Desktop/demo  然后按回 ...

  9. 【机器学习】k近邻算法(kNN)

    一.写在前面 本系列是对之前机器学习笔记的一个总结,这里只针对最基础的经典机器学习算法,对其本身的要点进行笔记总结,具体到算法的详细过程可以参见其他参考资料和书籍,这里顺便推荐一下Machine Le ...

  10. web API请求与参数获取

    总结webAPI的常用请求方法与后台参数的获取: 一:get请求:(会将所以参数拼接到URL里面) 1:基础类型:string  a=“hello” , 前端无论你是写到ajax里面的data属性还是 ...