飞道的博客

【MyBatis】查询语句汇总

376人阅读  评论(0)

定义一个Car类:


   
  1. /**
  2. * 封装汽车相关信息的 pojo类
  3. */
  4. public class Car {
  5. // 数据库表当中的字段应该和pojo类的属性一一对应
  6. // 建议使用包装类, 这样可以防止null的问题
  7. private Long id;
  8. private String carNum;
  9. private String brand;
  10. private Double guidePrice;
  11. private String produceTime;
  12. private String carType;
  13. public Car () {
  14. }
  15. public Car (Long id, String carNum, String brand, Double guidePrice, String produceTime, String carType) {
  16. this.id = id;
  17. this.carNum = carNum;
  18. this.brand = brand;
  19. this.guidePrice = guidePrice;
  20. this.produceTime = produceTime;
  21. this.carType = carType;
  22. }
  23. public Long getId () { return id;}
  24. public void setId (Long id) { this.id = id;}
  25. @Override
  26. public String toString () {
  27. return "Car{" +
  28. "id=" + id +
  29. ", carNum='" + carNum + '\'' +
  30. ", brand='" + brand + '\'' +
  31. ", guidePrice=" + guidePrice +
  32. ", produceTime='" + produceTime + '\'' +
  33. ", carType='" + carType + '\'' +
  34. '}';
  35. }
  36. public String getCarNum () { return carNum;}
  37. public void setCarNum (String carNum) { this.carNum = carNum;}
  38. public String getBrand () { return brand;}
  39. public void setBrand (String brand) { this.brand = brand;}
  40. public Double getGuidePrice () { return guidePrice;}
  41. public void setGuidePrice (Double guidePrice) { this.guidePrice = guidePrice;}
  42. public String getProduceTime () { return produceTime;}
  43. public void setProduceTime (String produceTime) { this.produceTime = produceTime;}
  44. public String getCarType () { return carType;}
  45. public void setCarType (String carType) { this.carType = carType;}
  46. }

1. 查询之返回Car:


   
  1. // CarMapper.xml
  2. // 必须要指定返回结果的类型
  3. <select id= "selectById" resultType= "Car">
  4. <!-- select * from car where id = #{id}-->
  5. // 列名要和pojo类中的属性名要对的上, 否则查出来为null
  6. select
  7. id, car_num as carNum, brand, guide_price as guidePrice,
  8. produce_time as produceTimme,
  9. car_type as catType
  10. from
  11. car
  12. where
  13. id = #{id}
  14. </select>
  15. // 接口
  16. public interface CarMapper{
  17. // 根据id查询Car信息
  18. Car selectById (Long id);
  19. }
  20. // @test
  21. public static void main (String[] args) {
  22. SqlSession sqlSession = SqlSessionUtil.openSession();
  23. CarMapper mapper = sqlSession.getMapper(CarMapper.class);
  24. Car car = mapper.selectById( 16L);
  25. System.out.println(car);
  26. sqlSession.close();
  27. }

2. 查询之返回多个Car:


   
  1. // CarMapper.xml
  2. // 必须要指定返回结果的类型
  3. <select id= "selectAll" resultType= "Car">
  4. <!-- select * from car where id = #{id}-->
  5. // 列名要和pojo类中的属性名要对的上, 否则查出来为null
  6. select
  7. id, car_num as carNum, brand, guide_price as guidePrice,
  8. produce_time as produceTimme,
  9. car_type as catType
  10. from
  11. car
  12. </select>
  13. // 接口
  14. public interface CarMapper{
  15. // 查询所有Car信息
  16. List<Car> selectAll ();
  17. }
  18. // @test
  19. public static void main (String[] args) {
  20. SqlSession sqlSession = SqlSessionUtil.openSession();
  21. CarMapper mapper = sqlSession.getMapper(CarMapper.class);
  22. List<Car> cars = mapper.selectAll();
  23. cars.forEach(car -> System.out.println(car));
  24. sqlSession.close();
  25. }

3. 查询之返回Map:

  • map的key都是数据库的列名


   
  1. // CarMapper.xml
  2. // resultType="java.util.Map" 有别名: "map"
  3. <select id= "selectByIdRetMap" resultType= "map">
  4. // 返回的类型是个map, 不需要和Car类属性匹配了
  5. select * from car where id = #{id}
  6. </select>
  7. // 接口
  8. public interface CarMapper{
  9. // 根据id获取汽车信息, 将信息放到Map集合中
  10. Map<String, Object> selectByIdRetMap (Long id);
  11. }
  12. // @test
  13. public static void main (String[] args) {
  14. SqlSession sqlSession = SqlSessionUtil.openSession();
  15. CarMapper mapper = sqlSession.getMapper(CarMapper.class);
  16. Map<String, Object> car = mapper.selectByIdRetMap( 16L);
  17. System.out.println(car);
  18. sqlSession.close();
  19. }

4. 查询之返回多个Map:


   
  1. // CarMapper.xml
  2. // resultType="map", 不能写"list"
  3. <select id= "selectAllRetListMap" resultType= "map">
  4. // 返回的类型是个map, 不需要和Car类属性匹配了
  5. select * from car
  6. </select>
  7. // 接口
  8. public interface CarMapper{
  9. // 查询所有的car信息, 返回一个存放Map集合的List集合
  10. List<Map<String, Object>> selectAllRetListMap ();
  11. }
  12. // @test
  13. public static void main (String[] args) {
  14. SqlSession sqlSession = SqlSessionUtil.openSession();
  15. CarMapper mapper = sqlSession.getMapper(CarMapper.class);
  16. List<Map<String, Object>> maps = mapper.selectAllRetListMap();
  17. maps.forEach(map -> System.out.println(map));
  18. sqlSession.close();
  19. }

5. 查询之返回Map<String,Map>:

  • 拿Car的id做key, 以后取出对应的Map集合时更加方便

  • 需要使用到@MapKey注解


   
  1. // CarMapper.xml
  2. // resultType="map", 不能写"list"
  3. <select id= "selectAllRetMap" resultType= "map">
  4. // 返回的类型是个map, 不需要和Car类属性匹配了
  5. select * from car
  6. </select>
  7. // 接口
  8. public interface CarMapper{
  9. // 查询所有的Car, 返回一个Map集合
  10. // Map集合的key是每条记录的主键值
  11. // Map集合的value是每条记录
  12. @MapKey("id") // 将查询结果的id值作为一个大map集合的key
  13. Map<Long,Map<String, Object>> selectAllRetMap ();
  14. }
  15. // @test
  16. public static void main (String[] args) {
  17. SqlSession sqlSession = SqlSessionUtil.openSession();
  18. CarMapper mapper = sqlSession.getMapper(CarMapper.class);
  19. Map<Long,Map<String, Object>> maps = mapper.selectAllRetMap();
  20. System.out.println(maps);
  21. sqlSession.close();
  22. }

6. resultMap结果映射:

查询结果的列名和Java对象的属性名对应不上怎么办?

  1. as给列起别名


   
  1. // CarMapper.xml
  2. // 必须要指定返回结果的类型
  3. <select id= "selectAll" resultType= "Car">
  4. select
  5. id,
  6. car_num as carNum,
  7. brand,
  8. guide_price as guidePrice,
  9. produce_time as produceTimme,
  10. car_type as catType
  11. from
  12. car
  13. </select>
  14. // 接口
  15. public interface CarMapper{
  16. // 查询所有Car信息
  17. List<Car> selectAll ();
  18. }
  19. // @test
  20. public static void main (String[] args) {
  21. SqlSession sqlSession = SqlSessionUtil.openSession();
  22. CarMapper mapper = sqlSession.getMapper(CarMapper.class);
  23. List<Car> cars = mapper.selectAll();
  24. cars.forEach(car -> System.out.println(car));
  25. sqlSession.close();
  26. }
  1. 使用resultMap进行结果映射


   
  1. // CarMapper.xml
  2. <resultMap id= "carResultMap" type= "Car">
  3. // 如果数据库表中有主键, 一般都是有主键的, 否则不符合数据库设计第一范式
  4. // 如果有主键, 建议这里面配置一个id标签, 这样是为了提高效率
  5. < id property = "id" column= "id"/>
  6. // property后面填写的是POJO类的属性名
  7. // column后面填写的是数据库的字段名
  8. <result property= "carNum" column= "car_num"/>
  9. // 如果属性名和字段名相同, 可以不配置
  10. <result property= "brand" column= "brand"/>
  11. <result property= "guidePrice" column= "guide_price"/>
  12. <result property= "produceTime" column= "produce_time"/>
  13. <result property= "carType" column= "car_type"/>
  14. </resultMap>
  15. // resultMap属性用来指定使用哪个结果映射, resultMap后面的值是resultMap的id
  16. <select id= "selectAllByResultMap" resultMap= "carResultMap">
  17. select * from car
  18. </select>
  19. // 接口
  20. public interface CarMapper{
  21. // 查询所有Car信息, 使用resultMap标签
  22. List<Car> selectAllByResultMap ();
  23. }
  24. // @test
  25. public static void main (String[] args) {
  26. SqlSession sqlSession = SqlSessionUtil.openSession();
  27. CarMapper mapper = sqlSession.getMapper(CarMapper.class);
  28. List<Car> cars = mapper.selectAllByResultMap();
  29. cars.forEach(car -> System.out.println(car));
  30. sqlSession.close();
  31. }
  1. 是否开启驼峰命名自动映射(配置settings)

  • 使用这种方式的前提是: 属性遵循java命名规范, 数据库的列名遵循sql的命名规范

  • sql命名规范: 全部小写, 单词之间采用下划线分割


   
  1. 如何启用该功能呢? 需要在mybatis-config.xml文件中配置
  2. // 放在properties标签后面
  3. <settings>
  4. <setting name= "mapUnderscoreToCameCase" value= "true">
  5. </settings>
  6. // CarMapper.xml
  7. <select id= "selectAllBymapUnderscoreToCameCase" resultType= "Car">
  8. select * from car
  9. </select>
  10. // 接口
  11. public interface CarMapper{
  12. // 查询所有Car信息, 启用驼峰命名自动映射机制
  13. List<Car> selectAllBymapUnderscoreToCameCase ();
  14. }
  15. // @test
  16. public static void main (String[] args) {
  17. SqlSession sqlSession = SqlSessionUtil.openSession();
  18. CarMapper mapper = sqlSession.getMapper(CarMapper.class);
  19. List<Car> cars = mapper.selectAllBymapUnderscoreToCameCase();
  20. cars.forEach(car -> System.out.println(car));
  21. sqlSession.close();
  22. }

7. 查询之返回总记录条数:


   
  1. // CarMapper.xml
  2. // resultType后面也可以写上java.lang.Long
  3. <select id= "selectTotal" resultType= "long">
  4. select count (*) from car
  5. </select>
  6. // 接口
  7. public interface CarMapper{
  8. // 获取Car的总记录条数
  9. Long selectTotal ();
  10. }
  11. // @test
  12. public static void main (String[] args) {
  13. SqlSession sqlSession = SqlSessionUtil.openSession();
  14. CarMapper mapper = sqlSession.getMapper(CarMapper.class);
  15. Long nums = mapper.selectTotal();
  16. System.out.println(nums);
  17. sqlSession.close();
  18. }

转载:https://blog.csdn.net/qq_68993495/article/details/128847409
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场