[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 ...
随机推荐
- Python 中 and 和 or 的短路原则
对于 and 来说: 如果第一个条件的结论为假,那么 and 前后两个条件组成的表达式计算结果一定为假,后面的条件计算机不会进行计算 对于 or 来说: 如果第一个条件的结论为真,那么 or 前后两个 ...
- JqGrid: Add,Edit,Del in asp.net
https://github.com/1rosehip/jplist https://github.com/free-jqgrid/jqGrid https://plugins.jquery.com/ ...
- python 中文件输入输出及os模块对文件系统的操作
整理了一下python 中文件的输入输出及主要介绍一些os模块中对文件系统的操作. 文件输入输出 1.内建函数open(file_name,文件打开模式,通用换行符支持),打开文件返回文件对象. 2. ...
- 在线客服兼容谷歌Chrome、苹果Safari、Opera浏览器的修改
纵览全网提供的众多号称兼容多浏览器的自动收缩在线客服,其实只兼容了IE和FF两种,当遇到谷歌Chrome.苹果Safari.Opera浏览器时鼠标还没点到客服按钮就会自动缩回,实用效果完全打折 以下代 ...
- Arrow模块生成时间
import arrow def isLeapYear(years): ''' 通过判断闰年,获取年份years下一年的总天数 :param years: 年份,int :return:days_su ...
- LaTeX:图形的填充(生成阴影图形)
将内网和外网看到的综合整理. 韦恩图Venn \documentclass{standalone} \usepackage{tikz} %导出为图片需要安装imagemagick %https://t ...
- Java并发编程(三)Thread类的使用
一.线程的状态 线程从创建到最终的消亡,要经历若干个状态.一般来说,线程包括以下这几个状态:创建(new).就绪(runnable).运行(running).阻塞(blocked).time wait ...
- Python:GUI之tkinter学习笔记3事件绑定
相关内容: command bind protocol 首发时间:2018-03-04 19:26 command: command是控件中的一个参数,如果使得command=函数,那么点击控件的时候 ...
- 如何使用C语言的面向对象
我们都知道,C++才是面向对象的语言,但是C语言是否能使用面向对象的功能? (1)继承性 typedef struct _parent { int data_parent; }Parent; type ...
- Oracle 表操作(转)
1.增加新字段:alter table table_name add (name varchar(20) default 'http://www.zangjing.net/');. 2.修改表字段:a ...