准备工作
本文以实体类为Car对象进行统一分析处理,其对应的数据库中的表如下
java中的Car对象如下:
1. /** 2. * @author 风轻云淡 3. */ 4. @Data 5. public class Car { 6. /** 7. * 包装类防止null问题 8. */ 9. private Long id; 10. 11. private String carNum; 12. 13. private String brand; 14. 15. private Double guidePrice; 16. 17. private String produceTime; 18. 19. private String carType; 20. 21. 22. }
返回对象为pojo实体类(Car)
对应的接口
1. /** 2. * 根据id查询 3. * @param id 4. * @return Car对象 5. */ 6. Car selectById(Long id);
对应的xml文件
1. <select id="selectById" resultType="pojo.Car"> 2. select 3. id as id , 4. car_num as carNum, 5. brand as brand, 6. guide_price as guidePrice, 7. car_type as carType, 8. produce_time as produceTime 9. from t_car where id=#{id} 10. </select>
resultType我个人的理解就是我们查询sql语句返回的结构集的行的玩意,也就是一个记录
对应的测试代码:
1. @Test 2. public void test01(){ 3. SqlSession session = SqlSessionUtil.openSession(); 4. CarMapper mapper = session.getMapper(CarMapper.class); 5. Car car = mapper.selectById(3L); 6. System.out.println(car); 7. }
返回集合对象(List<Car>)
当查询的记录条数是多条的时候,必须使用集合接收。如果使用单个实体类接收会出现异常。如果返回的是一条记录可以用集合接收
对应的接口
1. /** 2. * 查询所有car 3. * @return 4. */ 5. List<Car> selectAll();
对应的xml文件
1. <select id="selectAll" resultType="pojo.Car"> 2. select 3. id as id , 4. car_num as carNum, 5. brand as brand, 6. guide_price as guidePrice, 7. car_type as carType, 8. produce_time as produceTime 9. from t_car 10. </select>
对应的测试代码:
1. @Test 2. public void test02(){ 3. SqlSession session = SqlSessionUtil.openSession(); 4. CarMapper mapper = session.getMapper(CarMapper.class); 5. List<Car> cars = mapper.selectAll(); 6. System.out.println(cars); 7. }
返回Map
当返回的数据,没有合适的实体类对应的话,可以采用Map集合接收。字段名做key,字段值做value。查询如果可以保证只有一条数据,则返回一个Map集合即可。
对应的接口
1. /** 2. * 通过id查询一条记录,返回Map集合 3. * @param id 4. * @return 5. */ 6. Map<String, Object> selectByIdRetMap(Long id);
对应的xml文件
1. <select id="selectByIdRetMap" resultType="map"> 2. select id,car_num carNum,brand,guide_price guidePrice,produce_time produceTime,car_type carType from t_car where id = #{id} 3. </select>
resultMap="map",这是因为mybatis内置了很多别名。【参见mybatis开发手册】
对应的测试代码:
1. @Test 2. public void test03(){ 3. SqlSession session = SqlSessionUtil.openSession(); 4. CarMapper mapper = session.getMapper(CarMapper.class); 5. Map<String, Object> map = mapper.selectByIdRetMap(3L); 6. System.out.println(map); 7. }
当然,如果返回一个Map集合,可以将Map集合放到List集合中吗?当然可以,这里就不再测试了。
反过来,如果返回的不是一条记录,是多条记录的话,只采用单个Map集合接收,这样同样会出现之前的异常:TooManyResultsException
返回List<Map>
查询结果条数大于等于1条数据,则可以返回一个存储Map集合的List集合。List<Map>等同于List<Car>
对应的接口
1. /** 2. * 查询所有的Car,返回一个List集合。List集合中存储的是Map集合。 3. * @return 4. */ 5. List<Map<String,Object>> selectAllRetListMap();
对应的xml文件
1. <select id="selectAllRetListMap" resultType="map"> 2. select 3. id,car_num 4. carNum,brand, 5. guide_price 6. guidePrice, 7. produce_time 8. produceTime,car_type carType 9. from t_car 10. </select>
对应的测试代码:
1. @Test 2. public void test04(){ 3. SqlSession session = SqlSessionUtil.openSession(); 4. CarMapper mapper = session.getMapper(CarMapper.class); 5. List<Map<String, Object>> list = mapper.selectAllRetListMap(); 6. System.out.println(list); 7. }
返回Map<String,Map>
拿Car的id做key,以后取出对应的Map集合时更方便。
对应的接口
1. /** 2. * 获取所有的Car,返回一个Map集合。 3. * Map集合的key是Car的id。 4. * Map集合的value是对应Car。 5. * @return 6. */ 7. @MapKey("id") 8. Map<Long,Map<String,Object>> selectAllRetMap();
@MapKey("id")指定一个字段作为返回Map中的key,一般使用唯一键来做key
对应的xml文件
1. <select id="selectAllRetMap" resultType="map"> 2. select 3. id, 4. car_num carNum, 5. brand, 6. guide_price guidePrice, 7. produce_time produceTime, 8. car_type carType 9. from t_car 10. </select>
对应的测试代码:
1. @Test 2. public void test05(){ 3. SqlSession session = SqlSessionUtil.openSession(); 4. CarMapper mapper = session.getMapper(CarMapper.class); 5. Map<Long, Map<String, Object>> map = mapper.selectAllRetMap(); 6. System.out.println(map); 7. }
返回总记录条数
需求:查询总记录条数
对应的接口
1. /** 2. * 获取总记录条数 3. * @return 4. */ 5. Long selectTotal();
对应的xml文件
1. <!--long是别名,可参考mybatis开发手册。--> 2. <select id="selectTotal" resultType="long"> 3. select count(*) from t_car 4. </select>
对应的测试代码:
1. @Test 2. public void testSelectTotal(){ 3. CarMapper carMapper = SqlSessionUtil.openSession().getMapper(CarMapper.class); 4. Long total = carMapper.selectTotal(); 5. System.out.println(total); 6. }
要点
- (1)resultType 属性可以指定结果集的类型,它支持基本类型和实体类类型。我们在前面的 CRUD 案例中已经对此属性进行过应用了。
- (2)需要注意的是,它和 parameterType 一样,如果注册过类型别名的,可以直接使用别名。没有注册过的必须使用全限定类名。例如:我们的实体类此时必须是全限定类名
- (3)同时,当是实体类名称是,还有一个要求,实体类中的属性名称必须和查询语句中的列名保持一致,否则无法实现封装。