后台(40)——MyBatis输出映射resultType以及resultMap
探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)— 核心基础 Android多分辨率适配框架(2)— 原理剖析 Android多分辨率适配框架(3)— 使用指南 自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onLayout源码详尽分析 自定义View系列教程04–Draw源码分析及其实践 自定义View系列教程05–示例分析 自定义View系列教程06–详解View的Touch事件处理 自定义View系列教程07–详解ViewGroup分发Touch事件 自定义View系列教程08–滑动冲突的产生及其处理 版权声明 本文原创作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl 我们知道:MyBatis通过resultType对sql的输出参数进行定义,参数的类型可以是:基本类型、HashMap、pojo。在此分别介绍为resultType传入三种类型的不同处理方式。 基本类型 在此,请看一个小例子:统计学生的女同学 先看mapper.xml <select id="countStudent" parameterType="String" resultType='int'> SELECT count(*) from student where gender=#{value} </select> 此处,resultType的类型是int 再来看mapper.java public int countStudent(String string); 最后来看一下测试代码: @Test public void countStudent() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); int count = studentMapper.countStudent("female"); System.out.println("count="+count); sqlSession.commit(); sqlSession.close(); } HashMap 把resultType的类型指定为hashmap时在执行完sql之后MyBatis将输出的字段名称作为map的key,value为字段值。现在,我们在上个例子的基础上稍加改造。 先看mapper.xml <select id="countStudentByHashMap" parameterType="String" resultType='hashmap'> SELECT count(*) as total from student where gender=#{value} </select> 此处,resultType的类型是hashmap。我们将查询的结果放在total列中 再来看mapper.java public HashMap<String, Object> countStudentByHashMap(String string); 嗯哼,返回的类型是一个HashMap 最后来看一下测试代码: @Test public void countStudentByHashMap() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); HashMap<String, Object> hashMap = studentMapper.countStudentByHashMap("female"); System.out.println("total="+hashMap.get("total")); sqlSession.commit(); sqlSession.close(); } 我们从查询的结果hashmap中取出key为total对应的值 pojo 我们可将resultType指定为pojo,从而查询出对应的结果。比如,我们可以将resultType的类型指定为Student,从而查询出单个Student或者一个List,在此以查询单个Student为例 先来看mapper.xml <select id="selectStudentByID" parameterType="int" resultType="cn.com.Student"> SELECT * from student where id=#{value} </select> 在此指定resultType的类型是Student 再来看mapper.java public Student selectStudentByID(int id); 最后请看测试 @Test public void selectStudentByID() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student student = studentMapper.selectStudentByID(13); System.out.println(student); sqlSession.commit(); sqlSession.close(); } 在此请注意: 使用resultType进行输出映射时务必将查询的列名与pojo中的属性名保持一致! 看到这里,或许有人会问了:如果如果查询出来的列名和pojo的属性名不一致又怎么办呢?嗯哼,此时最好就不要再用resultType了,可以考虑使用resultMap,请继续往下看 resultMap 为了便于说明,我们来创建一个新的类_Student /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; import java.util.Date; public class _Student { private int _id; private String _name; private String _gender; private Date _birthday; public int get_id() { return _id; } public void set_id(int _id) { this._id = _id; } public String get_name() { return _name; } public void set_name(String _name) { this._name = _name; } public String get_gender() { return _gender; } public void set_gender(String _gender) { this._gender = _gender; } public Date get_birthday() { return _birthday; } public void set_birthday(Date _birthday) { this._birthday = _birthday; } @Override public String toString() { return "_Student [_id=" + _id + ", _name=" + _name + ", _gender=" + _gender + ", _birthday=" + _birthday + "]"; } } 首先在mapper.xml中定义一个resultMap <resultMap type="cn.com._Student" id="studentResultMap"> <id column="id" property="_id" /> <result column="name" property="_name" /> <result column="gender" property="_gender" /> <result column="birthday" property="_birthday" /> </resultMap> <resultMap>标签中的type属性表示pojo,请参见代码第1行 <resultMap>标签中的id属性表示resultMap的名字,请参见代码第1行 <id>标签表示数据库表中的主键与pojo的属性的映射关系;比如,此处,将表中的id字段映射为pojo中的_id,请参见代码第2行 <restult>标签表示数据库表中除了主键以外的其他字段与pojo的属性的映射关系,请参见代码第3-5行 然后请看mapper.xml中的SQL语句 <select id="_selectStudentByID" parameterType="int" resultMap="studentResultMap"> SELECT * from student where id=#{value} </select> 此处利用resultMap指定了返回的类型为我们刚才定义的studentResultMap 再来看mapper.java public Student _selectStudentByID(int id); 最后来瞅瞅测试代码 @Test public void _selectStudentByID() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); _Student _student = studentMapper._selectStudentByID(13); System.out.println(_student); sqlSession.commit(); sqlSession.close(); } 输出结果: _Student [_id=13, _name=大泽玛利亚, _gender=female, _birthday=Thu Mar 16 00:00:00 CST 2017]