[03] mapper.xml的基本元素概述
1、select
public interface GirlDao {
List<Girl> findByAge(int age);
Girl findById(long id);
int insertGirl(Girl girl);
int updateGirl(Girl girl);
int deleteGirl(long id);
}
public interface GirlDao {
List<Girl> findByAge(int age);
Girl findById(long id);
int insertGirl(Girl girl);
int updateGirl(Girl girl);
int deleteGirl(long id);
}
<mapper namespace="dulk.learn.mybatis.dao.GirlDao">
<select id="findById" parameterType="long" resultType="dulk.learn.mybatis.pojo.Girl">
SELECT * FROM girl WHERE id = #{id}
</select>
</mapper>
<mapper namespace="dulk.learn.mybatis.dao.GirlDao">
<select id="findById" parameterType="long" resultType="dulk.learn.mybatis.pojo.Girl">
SELECT * FROM girl WHERE id = #{id}
</select>
</mapper>
- select 标签的 id 属性用来标记和区别该 select,如果需要对应接口则 id 和接口方法名要一致
- parameterType 表示输入参数的类型
- resultType 表示输出结果的类型
- #{ } 表示传入的动态参数的占位符
String findById = "SELECT * FROM girl WHERE id = ?";
PreparedStatement ps = conn.prepareStatement(findById);
ps.setLong(1, id);
String findById = "SELECT * FROM girl WHERE id = ?";
PreparedStatement ps = conn.prepareStatement(findById);
ps.setLong(1, id);
1.1 输入参数 parameterType
<insert id="insertUser" parameterType="User">
insert into users (id, username, password)
values (#{id}, #{username}, #{password})
</insert>
<insert id="insertUser" parameterType="User">
insert into users (id, username, password)
values (#{id}, #{username}, #{password})
</insert>
1.2 输出结果 resultType / resultMap
- resultType - 返回的期望类型的类的完全限定名或别名(注:若是集合情形,那应该是集合可包含的类型,而不能是集合本身)
- resultMap - 外部 resultMap 的命名引用
<mapper namespace="dulk.learn.mybatis.dao.GirlDao">
<!--定义resultMap-->
<resultMap id="girlResultMap" type="dulk.learn.mybatis.pojo.Girl">
<!--id表查询结果中的唯一标识-->
<id property="id" column="id" />
<!--result表示对普通列名的映射,propertyi表对象属性名,column表查询出的列名-->
<result property="age" column="age" />
</resultMap>
<!--引用外部resultMap,即girlResultMap-->
<select id="findById" parameterType="long" resultMap="girlResultMap">
SELECT * FROM girl WHERE id = #{id}
</select>
</mapper>
<mapper namespace="dulk.learn.mybatis.dao.GirlDao">
<!--定义resultMap-->
<resultMap id="girlResultMap" type="dulk.learn.mybatis.pojo.Girl">
<!--id表查询结果中的唯一标识-->
<id property="id" column="id" />
<!--result表示对普通列名的映射,propertyi表对象属性名,column表查询出的列名-->
<result property="age" column="age" />
</resultMap>
<!--引用外部resultMap,即girlResultMap-->
<select id="findById" parameterType="long" resultMap="girlResultMap">
SELECT * FROM girl WHERE id = #{id}
</select>
</mapper>
2、insert, update 和 delete
<!--使用了useGeneratedKeys和keyProperty来将生成的主键设置到对象属性中-->
<insert id="insertGirl" parameterType="dulk.learn.mybatis.pojo.Girl" useGeneratedKeys="true" keyProperty="id">
INSERT INTO girl (age)
VALUES (#{age})
</insert>
<update id="updateGirl" parameterType="dulk.learn.mybatis.pojo.Girl">
UPDATE girl
SET age = #{age}
WHERE id = #{id}
</update>
<delete id="deleteGirl" parameterType="long">
DELETE FROM girl
WHERE id = #{id}
</delete>
<!--使用了useGeneratedKeys和keyProperty来将生成的主键设置到对象属性中-->
<insert id="insertGirl" parameterType="dulk.learn.mybatis.pojo.Girl" useGeneratedKeys="true" keyProperty="id">
INSERT INTO girl (age)
VALUES (#{age})
</insert>
<update id="updateGirl" parameterType="dulk.learn.mybatis.pojo.Girl">
UPDATE girl
SET age = #{age}
WHERE id = #{id}
</update>
<delete id="deleteGirl" parameterType="long">
DELETE FROM girl
WHERE id = #{id}
</delete>
- insert / update / delete 都没有 resultType 或 resultMap,他们的返回值是受影响的行数
- 若数据库支持自动生成主键,可设置 useGeneratedKeys 为 true,并使用 keyProperty 将生成的主键值设置到目标属性上(如上例的insert)
public class Test {
@org.junit.Test
public void testMyBatis() throws IOException {
//读取配置文件
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//获取工厂类
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//获取SqlSession数据库会话对象
SqlSession sqlSession = factory.openSession();
//获取Dao
GirlDao girlDao = sqlSession.getMapper(GirlDao.class);
Girl girl = new Girl();
girl.setAge(18);
//insert a girl with age 18
int resultInsert = girlDao.insertGirl(girl);
Assert.assertEquals(1, resultInsert);
Assert.assertEquals(18, girlDao.findById(girl.getId()).getAge());
//update girl's age from 18 to 20
girl.setAge(20);
int resultUpdate = girlDao.updateGirl(girl);
Assert.assertEquals(1, resultUpdate);
Assert.assertEquals(20, girlDao.findById(girl.getId()).getAge());
//delete girl
int resultDelete = girlDao.deleteGirl(girl.getId());
Assert.assertEquals(1, resultDelete);
Assert.assertNull(girlDao.findById(girl.getId()));
}
}
public class Test {
@org.junit.Test
public void testMyBatis() throws IOException {
//读取配置文件
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//获取工厂类
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//获取SqlSession数据库会话对象
SqlSession sqlSession = factory.openSession();
//获取Dao
GirlDao girlDao = sqlSession.getMapper(GirlDao.class);
Girl girl = new Girl();
girl.setAge(18);
//insert a girl with age 18
int resultInsert = girlDao.insertGirl(girl);
Assert.assertEquals(1, resultInsert);
Assert.assertEquals(18, girlDao.findById(girl.getId()).getAge());
//update girl's age from 18 to 20
girl.setAge(20);
int resultUpdate = girlDao.updateGirl(girl);
Assert.assertEquals(1, resultUpdate);
Assert.assertEquals(20, girlDao.findById(girl.getId()).getAge());
//delete girl
int resultDelete = girlDao.deleteGirl(girl.getId());
Assert.assertEquals(1, resultDelete);
Assert.assertNull(girlDao.findById(girl.getId()));
}
}
3、sql
<mapper namespace="dulk.learn.mybatis.dao.GirlDao">
<resultMap id="girlResultMap" type="dulk.learn.mybatis.pojo.Girl">
<id property="id" column="id" />
<result property="age" column="age" />
<result property="cupSize" column="cup_size" />
</resultMap>
<!--定义可重用的sql片段-->
<sql id="girlColumn">
id, age
</sql>
<select id="findById" parameterType="long" resultMap="girlResultMap">
<!--通过include引用外部sql片段-->
SELECT <include refid="girlColumn" />
FROM girl WHERE id = #{id}
</select>
</mapper>
<mapper namespace="dulk.learn.mybatis.dao.GirlDao">
<resultMap id="girlResultMap" type="dulk.learn.mybatis.pojo.Girl">
<id property="id" column="id" />
<result property="age" column="age" />
<result property="cupSize" column="cup_size" />
</resultMap>
<!--定义可重用的sql片段-->
<sql id="girlColumn">
id, age
</sql>
<select id="findById" parameterType="long" resultMap="girlResultMap">
<!--通过include引用外部sql片段-->
SELECT <include refid="girlColumn" />
FROM girl WHERE id = #{id}
</select>
</mapper>
[03] mapper.xml的基本元素概述的更多相关文章
- mybatis自动生成model、dao及对应的mapper.xml文件
背景: 日常开发中,如果新建表,手动敲写model.dao和对应的mapper.xml文件,费时费力且容易出错, 所以采用mybatis自动生成model.dao及对应的mapper.xml文件.代码 ...
- MyBatis Mapper.xml文件中 $和#的区别
MyBatis Mapper.xml文件中 $和#的区别 网上有很多,总之,简略的写一下,作为备忘.例子中假设参数名为 paramName,类型为 VARCHAR . 1.优先使用#{paramN ...
- Mybatis学习错误之:重复加载mapper.xml
学习mybatis的时候,突然遇到测试出错.测试mapper代理失败,现在钻研少了,不喜欢看未知的错误了,立即改正.错误打印说mapper.xml已经注册,仔细查看SQLMapConfig.xml发现 ...
- mybatis公用代码抽取到单独的mapper.xml文件
同任何的代码库一样,在mapper中,通常也会有一些公共的sql代码段会被很多业务mapper.xml引用到,比如最常用的可能是分页和数据权限过滤了,尤其是在oracle中的分页语法.为了减少骨架性代 ...
- Mybatis学习--Mapper.xml映射文件
简介 Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心. 映射文件中有很多属性,常用的就是parameterType(输入类型 ...
- nested exception is java.lang.RuntimeException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'daoSupport': ...
- mybatis mapper.xml 配置文件问题(有的错误xml是不报的) 导致服务无法启动 。
转载自 开源编程 一舟mybatsi xml编译报错,tomcat启动一直循环,导致内存溢出,启动失败 mapper.xml怎么知道有没有编译错误,哪个位置有错误 这应该是mybatis的一个bug, ...
- mybatis之mapper.xml分析
select: id:方法名,在同一个mapper.xml中,要保持唯一 parameterType:指定输入的参数类型,不是必须的,如果不指定,mybatis会自动识别(推荐指定). resultT ...
- spring mybatis 整合问题Error parsing Mapper XML. Cause: java.lang.NullPointerException
14:30:40,872 DEBUG SqlSessionFactoryBean:431 - Parsed configuration file: 'class path resource [myba ...
随机推荐
- cdn原理的理解
今天要做个小笔记,浅谈一下对cdn的一些理解,在工作中我们经常用到cdn代理访问,那他的原理是什么不知道大家有没有考虑过 CDN的基本原理是广泛采用各种缓存服务器,将这些缓存服务器分布到用户访问相对集 ...
- 探讨PHP页面跳转几种实现技巧 转自# 作者:佚名 来源:百度博客 #
Web系统中,从一个网页跳转到另一个网页,是LAMP项目中最常用的技术之一.页面跳转可能是由于用户单击链接.按钮等引发的,也可能是系统自动产生的. 此处介绍PHP中常用的实现页面自动跳转的方法. PH ...
- input 输入框 change 事件和 blur 事件
输入框的 change 和 blur 事件绝大多数情况下表现是一致的,输入结束后离开输入框会先后触发 change 和 blur.那么这两个事件的区别在哪呢? 当文本框获得焦点后,没有输入任何内容, ...
- LeetCode题解之 Find Mode in Binary Search Tree
1.题目描述 2.问题分析 使用map记录元素出现的次数. 3.代码 vector<int> v; map<int,int> m; vector<int> find ...
- Ionic目录结构
目录下有以下文件: hooks //google之后这个目录应该是在编译cordova时自定义的脚本命令,方便整合到我们的编译系统和版本控制系统中plugins //cordova插件的目录,插件的安 ...
- 第七章 Hyper-V 2012 R2 授权管理
当企业或组织的规模越来越大时,维护某一项单独的应用可能会由特定的运维人员进行管理.考虑到安全风险的问题,一般特定的运维人员不会拥有域管理员权限.自 Windows Server 2012 开始,操作系 ...
- sftp 建立用户
1.创建sftp组:#groupadd sftp 2.创建测试账户:#useradd -g sftp -s /bin/false testuser 修改密码:# passwd sftp 3.修改测试账 ...
- MySQL基本简单操作02
MySQL基本简单操作 先进入Mysql容器. [root@promote ~]# docker exec -it mysql /bin/bash root@30d60b852cf5:/# mysql ...
- WINDOWS 下设置单独的java环境
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_65set PATH=%JAVA_HOME%\bin;%PATH%;D:cd D:\JavaProjectcm ...
- 布局:高度已知,布局一个三栏布局,左栏和右栏宽度为200px,中间自适应
需求:高度已知为200px,写出三栏布局,左栏和右栏各位200px,中间自适应,如下图所示: 方法一:float浮动布局 原理是:定义三个区块,需要注意的是中间的区块放在右边区块的下面,统一设置高度为 ...