【MyBatis】查询语句汇总
创始人
2024-05-23 05:17:16
0

定义一个Car类:

/*** 封装汽车相关信息的 pojo类*/
public class Car {// 数据库表当中的字段应该和pojo类的属性一一对应// 建议使用包装类, 这样可以防止null的问题private Long id;private String carNum;private String brand;private Double guidePrice;private String produceTime;private String carType;public Car() {}public Car(Long id, String carNum, String brand, Double guidePrice, String produceTime, String carType) {this.id = id;this.carNum = carNum;this.brand = brand;this.guidePrice = guidePrice;this.produceTime = produceTime;this.carType = carType;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}@Overridepublic String toString() {return "Car{" +"id=" + id +", carNum='" + carNum + '\'' +", brand='" + brand + '\'' +", guidePrice=" + guidePrice +", produceTime='" + produceTime + '\'' +", carType='" + carType + '\'' +'}';}public String getCarNum() {return carNum;}public void setCarNum(String carNum) {this.carNum = carNum;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public Double getGuidePrice() {return guidePrice;}public void setGuidePrice(Double guidePrice) {this.guidePrice = guidePrice;}public String getProduceTime() {return produceTime;}public void setProduceTime(String produceTime) {this.produceTime = produceTime;}public String getCarType() {return carType;}public void setCarType(String carType) {this.carType = carType;}
}

1. 查询之返回Car:

// CarMapper.xml
// 必须要指定返回结果的类型
// 接口
public interface CarMapper{// 根据id查询Car信息Car selectById(Long id);
}// @test
public static void main(String[] args) {SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);Car car = mapper.selectById(16L);System.out.println(car);sqlSession.close();
}

2. 查询之返回多个Car:

// CarMapper.xml
// 必须要指定返回结果的类型
// 接口
public interface CarMapper{// 查询所有Car信息List selectAll();
}// @test
public static void main(String[] args) {SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);List cars = mapper.selectAll();cars.forEach(car -> System.out.println(car));sqlSession.close();
}

3. 查询之返回Map:

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

// CarMapper.xml
// resultType="java.util.Map" 有别名: "map"
// 接口
public interface CarMapper{// 根据id获取汽车信息, 将信息放到Map集合中Map selectByIdRetMap(Long id);
}// @test
public static void main(String[] args) {SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);Map car = mapper.selectByIdRetMap(16L);System.out.println(car);sqlSession.close();
}

4. 查询之返回多个Map:

// CarMapper.xml
// resultType="map", 不能写"list"
// 接口
public interface CarMapper{// 查询所有的car信息, 返回一个存放Map集合的List集合List> selectAllRetListMap();
}// @test
public static void main(String[] args) {SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);List> maps = mapper.selectAllRetListMap();maps.forEach(map -> System.out.println(map));sqlSession.close();
}

5. 查询之返回Map:

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

  • 需要使用到@MapKey注解

// CarMapper.xml
// resultType="map", 不能写"list"
// 接口
public interface CarMapper{// 查询所有的Car, 返回一个Map集合// Map集合的key是每条记录的主键值// Map集合的value是每条记录@MapKey("id") // 将查询结果的id值作为一个大map集合的keyMap> selectAllRetMap();
}// @test
public static void main(String[] args) {SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);Map> maps = mapper.selectAllRetMap();System.out.println(maps);sqlSession.close();
}

6. resultMap结果映射:

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

  1. as给列起别名

// CarMapper.xml
// 必须要指定返回结果的类型
// 接口
public interface CarMapper{// 查询所有Car信息List selectAll();
}// @test
public static void main(String[] args) {SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);List cars = mapper.selectAll();cars.forEach(car -> System.out.println(car));sqlSession.close();
}
  1. 使用resultMap进行结果映射

// CarMapper.xml// 如果数据库表中有主键, 一般都是有主键的, 否则不符合数据库设计第一范式// 如果有主键, 建议这里面配置一个id标签, 这样是为了提高效率// property后面填写的是POJO类的属性名// column后面填写的是数据库的字段名// 如果属性名和字段名相同, 可以不配置    
// resultMap属性用来指定使用哪个结果映射, resultMap后面的值是resultMap的id
// 接口
public interface CarMapper{// 查询所有Car信息, 使用resultMap标签List selectAllByResultMap();
}// @test
public static void main(String[] args) {SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);List cars = mapper.selectAllByResultMap();cars.forEach(car -> System.out.println(car));sqlSession.close();
}
  1. 是否开启驼峰命名自动映射(配置settings)

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

  • java命名规范: 首字母小写, 后面每个单词首字母大写, 遵循驼峰命名方式

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

如何启用该功能呢? 需要在mybatis-config.xml文件中配置// 放在properties标签后面

// CarMapper.xml
// 接口
public interface CarMapper{// 查询所有Car信息, 启用驼峰命名自动映射机制List selectAllBymapUnderscoreToCameCase();
}// @test
public static void main(String[] args) {SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);List cars = mapper.selectAllBymapUnderscoreToCameCase();cars.forEach(car -> System.out.println(car));sqlSession.close();
}

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

// CarMapper.xml
// resultType后面也可以写上java.lang.Long
// 接口
public interface CarMapper{// 获取Car的总记录条数Long selectTotal();
}// @test
public static void main(String[] args) {SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);Long nums = mapper.selectTotal();System.out.println(nums);sqlSession.close();
}

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...