mybatis之返回值总结
mybatis框架让我们能在编程中只需要编写一个接口,然后再编写mapper映射文件,无需编写接口的实现类就可以实现从数据库检索数据。这是mybatis通过动态代理,把mapper映射文件的内容转化为真正的执行部分。因此我们在编程中,需要特别关注接口和映射文件的编写。本节主要讲解接口方法的返回值类型在mapper文件中的编写方式。
我们思考下,返回值类型一般分为
- 数字类型,比如查询记录的个数
- 单个对象
- 多个对象,使用List封装
- 单个对象,使用map封装
- 多个对象,使用map封装
由于每次建立工程比较复杂,可以参考第一节:mybatis入门来搭建一个简单的工程,然后来测试本节内容。
注意本节内容关注点是mapper接口方法的返回值类型和mapper文件的resultType的编写。
1、返回值类型为数字类型
1、mapper接口,我们简单查询所有记录,返回Long类型的值。
public interface PersonMapper
{
Long getTotalNumberOfPerson();
}
2、mapper映射文件
<select id="getTotalNumberOfPerson" resultType="long">
select count(*) from person
</select>
注意在mapper文件中,只需要 resultType 为 long 类型即可。
3、测试
public class Main
{
public static void main(String[] args)
throws IOException
{
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
Long totalNumberOfPerson= mapper.getTotalNumberOfPerson();
System.out.println(totalNumberOfPerson); //10
sqlSession.close();
}
}
2、查询单个对象
这种查询直接返回和数据库表对应的相关实体,并且只有一条记录,一般都是按照id去查询,也就是第一节:mybatis入门中演示的样例,我们再次说明下。
1、mapper接口
public interface PersonMapper
{
Person getPerson(Integer id);
}
2、mapper映射文件
<select id="getPerson" resultType="com.yefengyu.mybatis.entity.Person">
select id, first_name firstName, last_name lastName, age, email, address from person where id = #{id}
</select>
3、测试
public class Main
{
public static void main(String[] args)
throws IOException
{
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
Person person = mapper.getPerson(1);
System.out.println(person);
sqlSession.close();
}
}
总结:在查询单个对象,返回的是实体类型的时候,只需要将resultType设置为全类名即可。
3、查询多个对象,使用list封装
这种查询一般是根据某种条件,查询出很多个结果,然后使用List封装起来。
1、mapper接口,根据address查询多个Person对象
public interface PersonMapper
{
List<Person> getPersons(String address);
}
2、mapper映射文件,注意对于mapper接口中返回List类型的,mapper映射文件的resultType只需要设置List所包含的对象的类型即可。
<select id="getPersons" resultType="com.yefengyu.mybatis.entity.Person">
select id, first_name firstName, last_name lastName, age, email, address from person where address = #{address}
</select>
3、测试
public class Main
{
public static void main(String[] args)
throws IOException
{
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
List<Person> persons = mapper.getPersons("beijing");
for (int i = 0; i < persons.size(); i++)
{
System.out.println(persons.get(i));
}
sqlSession.close();
}
}
4、结果:
Person{id=1, firstName='Schmitt', lastName='Carine', age=25, email='null', address='beijing'}
Person{id=2, firstName='King', lastName='Jean', age=36, email='Jean@163.com', address='beijing'}
Person{id=8, firstName='Gao', lastName='Diego', age=45, email='66666@qq.com', address='beijing'}
Person{id=9, firstName='Piestrzeniewicz', lastName='Schmitt', age=36, email='44444@qq.com', address='beijing'}
Person{id=10, firstName='Frdrique', lastName='Juri', age=25, email='99999@qq.com', address='beijing'}
4、查询单个对象,使用map封装
这种情况,类似第二小节,也是单个对象,只不过我们需要返回的是一个map,将单个对象封装成map,数据库的列名对应的属性名称作为key,返回的结果作为value.
1、mapper接口
public interface PersonMapper
{
Map<String, Object> getPersonMap(Integer id);
}
2、mapper映射文件,此时需要把resultType设置为map
<select id="getPersonMap" resultType="map">
select id, first_name firstName, last_name lastName, age, email, address from person where id = #{id}
</select>
3、测试
public class Main
{
public static void main(String[] args)
throws IOException
{
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
Map<String, Object> personMap = mapper.getPersonMap(1);
System.out.println(personMap);
sqlSession.close();
}
}
4、结果如下,注意在使用map接收返回值的时候,如果某个字段为null,那么就不会封装到map中,比如下面的结果中就没有Email字段的结果。
{firstName=Schmitt, lastName=Carine, address=beijing, id=1, age=25}
5、查询多个对象,使用map封装
这个情况,一般是查询多条记录,使用主键作为key,使用对象作为value,见下面mapper接口。
1、mapper接口
public interface PersonMapper
{
@MapKey("id")
Map<Integer, Person> getPersonsMap(String address);
}
2、mapper映射文件
<select id="getPersonsMap" resultType="map">
select id, first_name firstName, last_name lastName, age, email, address from person where address = #{address}
</select>
3、测试
public class Main
{
public static void main(String[] args)
throws IOException
{
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
Map<Integer, Person> personMap = mapper.getPersonsMap("beijing");
for (Map.Entry<Integer, Person> personEntry : personMap.entrySet())
{
System.out.println(personEntry.getKey() + " : " + personEntry.getValue());
}
sqlSession.close();
}
}
4、结果
1 : {firstName=Schmitt, lastName=Carine, address=beijing, id=1, age=25}
2 : {firstName=King, lastName=Jean, address=beijing, id=2, age=36, email=Jean@163.com}
8 : {firstName=Gao, lastName=Diego, address=beijing, id=8, age=45, email=66666@qq.com}
9 : {firstName=Piestrzeniewicz, lastName=Schmitt, address=beijing, id=9, age=36, email=44444@qq.com}
10 : {firstName=Frdrique, lastName=Juri, address=beijing, id=10, age=25, email=99999@qq.com}
注意:mapper接口需要使用MapKey注解指定将某个属性的值作为map的key。本例中使用person表的主键id对应的实体的属性id作为key。除此之外,mapper映射文件中resultType设置为map。此外如果某个字段为null,那么就不插入到map中。
6、总结
- 数字类型,比如查询记录的个数。resultType为 int 或者 long等。
- 单个对象。resultType为对象全类名
- 多个对象,使用List封装。resultType为对象全类名。
- 单个对象,使用map封装。resultType为map。
- 多个对象,使用map封装。resultType为map。注意mapper接口的方法需要使用MapKey注解指定key为哪个属性。
mybatis之返回值总结的更多相关文章
- Mybatis select返回值为map时,选取表字段的两列作为key,value
项目需要从ibatis升级到MyBatis,dao中有一个方法返回Map类型,具体是查询语句查询两个字段,将结果列表字段A的值作为key字段B的值作为value存入Map中作为结果返回: ibatis ...
- Mybatis(三)返回值
Mybatis返回值 MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则 ...
- mybatis update 返回值
mybatis sql: <update id="test" parameterType="map"> update test_0731 set n ...
- 深入了解MyBatis返回值
深入了解MyBatis返回值 想了解返回值,我们须要了解resultType,resultMap以及接口方法中定义的返回值. 我们先看resultType和resultMap resultType和r ...
- MyBatis中Mapper的返回值类型
insert.update.delete语句的返回值类型 对数据库执行修改操作时,数据库会返回受影响的行数. 在MyBatis(使用版本3.4.6,早期版本不支持)中insert.update.del ...
- Mybatis 实用篇(四)返回值类型
Mybatis 实用篇(四)返回值类型 一.返回 List.Map List<User> getUsers(); <select id="getUsers" re ...
- Mybatis执行sql(insert、update、delete)返回值问题
数据库:Mysql 在使用mybatis的过程中对执行sql的返回值产生疑问,顺手记录一下. 结论: insert: 插入n条记录,返回影响行数n.(n>=1,n为0时实际为插入失败) up ...
- mybatis Mapper 中resultType使用方法及返回值为Map的写法
mybatis学习(七)——resultType解析 resultType是sql映射文件中定义返回值类型,返回值有基本类型,对象类型,List类型,Map类型等.现总结一下再解释 总结: resul ...
- MyBatis查询结果resultType返回值类型详细介绍
一.返回一般数据类型 比如要根据 id 属性获得数据库中的某个字段值. mapper 接口: // 根据 id 获得数据库中的 username 字段的值 String getEmpNameById( ...
随机推荐
- 嵌入式C语言3.2 关键字---自定义数据类型
1. struct 结构体 基本语法 struct myabc{ unsigned int a; unsigned int b; unsigned int c; unsigned int d; } 调 ...
- PdgCntEditor系列教程一:基础知识
一.PdgCntEditor是什么? 二.为什么要用PdgCntEditor? 三.怎么用PdgCntEditor? 一.PdgCntEditor是什么? 这是一个目录编辑器,可以创建.编辑PDF.D ...
- javascript基础六(事件对象)
1.事件驱动 js控制页面的行为是由事件驱动的. 什么是事件?(怎么发生的) 事件就是js侦测到用户的操作或是页面上的一些行为 事件源(发生在谁身上) ...
- ThinkPhp学习
页面跳转 界面跳转是很常用的操作,所以基于ubuntu16系统,这周学习了ThinkPHP页面跳转和重定向. 页面跳转 系统的Think\Controller类内置了两个页面跳转方法err ...
- 企业微信上传 带中文名称的 临时素材资源 报错 44001:empty media data
错误原因:urllib3的老版本bug,卸载掉 requests,urllib3,从新安装最新版的requests(此包内部依赖urllib3): 我从新安装的是 requests==2.22.0 及 ...
- Linux系统分辨率设置
linux 设置分辨率 如果你需要在linux上设置显示屏的分辨率,分两种情况:分辨率模式存在与分辨率模式不存在,具体如下. 1,分辨率模式已存在 1)如何查询是否存在: 图形界面:在System S ...
- 软件安装 RPM SRPM YUM
RPM介绍 RPM是已经编译好的软件安装库.编译是有相应环境相适应的,包括系统,版本等相关信息都要跟编译版本一致才行,否则肯定会出现安装不成功的情况,强制安装的话,也会出现各种各样的问题. 在这种情况 ...
- 二、sqlyog的使用
1. 创建数据库. 注意字符集 2.创建表 注意 表名.引擎名.字符集
- Sass--嵌套选择器
Sass 中还提供了选择器嵌套功能,但这也并不意味着你在 Sass 中的嵌套是无节制的,因为你嵌套的层级越深,编译出来的 CSS 代码的选择器层级将越深,这往往是大家不愿意看到的一点.这个特性现在正被 ...
- 使用CSS3的@media来编写响应式的页面
首先要知道,我们为什么要写自适应的页面(响应式页面) [直接看干货] 众所周知,电脑.平板.手机的屏幕是差距很大的,假如在电脑上写好了一个页面,在电脑上看起来不错,但是如果放到手机上的话,那可能就会乱 ...