MyBatis学习笔记(三)查询集合配置
2015-08-30 14:47
246 查看
第一节中查询的方法返回类型为Student,当返回类型为List<Student>时怎么办呢?
这时需要配置resultMap标签.
这时需要配置resultMap标签.
<?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"> <!-- namespace:Mapper映射 --> <!-- 这个文件感觉就像StudentMapper接口的实现一样,只是从java文件变成了xml文件 充当了Dao类的功能 --> <mapper namespace="com.skymr.mybatis.mappers.StudentMapper"> <select id="getStudent" parameterType="int" resultType="Student"> <!-- mybatis要自己写sql语句 --> select * from mybatis_Student where id=#{id} </select> <!-- 查询所有学生 --> <!-- resultMap属性要对应 resultMap的id属性 --> <select id="getAllStudents" resultMap="studentMap"> select * from mybatis_Student </select> <resultMap type="Student" id="studentMap"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> </resultMap> </mapper>
package com.skymr.mybatis.mappers; import java.util.List; import com.skymr.mybatis.model.Student; public interface StudentMapper { public Student getStudent(int id); public List<Student> getAllStudents(); }
package com.skymr.mybatis.service; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.skymr.mybatis.mappers.StudentMapper; import com.skymr.mybatis.model.Student; import com.skymr.mybatis.util.MybatisUtil; public class StudentTest2 { private Logger logger = LoggerFactory.getLogger(StudentTest2.class); private SqlSession session; @Before public void beforeTest(){ session = MybatisUtil.openSession(); } @After public void afterTest(){ session.close(); } @Test public void testGetAllStudents(){ logger.info("测试取得所有学生"); StudentMapper mapper = session.getMapper(StudentMapper.class); List<Student> list = mapper.getAllStudents(); logger.info(list.toString()); } }
相关文章推荐
- 红黑树 B-树 B+树 数据库索引
- C#图片压缩方法
- adb uninstall卸载apk文件说明
- 跳水比赛,8个评委打分。运动员的成绩是8个成绩去掉一个最高分, 去掉一个最低分,剩下的6个分数的平均分就是最后得分。 使用一维数组 2.请把打最高分的评委和最低分的评委找出来。 3.找出最佳评委
- C++11 多线程
- The Java EE 6 Tutorial Accessing the Web Context
- Handsontable 学习笔记-Methods
- PAM <备忘>
- Deep Learning Face Representation by Joint Identification-Verification
- [leetcode]Count Primes C语言
- Java调用C/C++实现的DLL动态库——JNI
- 在 Android 中 Intent 的概念及应用
- 观察者模式
- 发送短信
- pod install 遇到 :[MT] DVTAssertions: ASSERTION FAILURE in ……
- The Java EE 6 Tutorial Invoking Other Web Resources
- linux screen 配置文件 超好用
- 阻塞型驱动设计
- Android中插件开发篇总结和概述
- Android基础入门教程——5.2.3 Fragment实例精讲——底部导航栏的实现(方法3)