1、SpringBoot整合MyBatis1.1 application.yml# 数据源配置spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/kh96_ssm_airms?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMTusername: rootpassword: root# mybatis 核心配置mybatis:configuration:map-underscore-to-camel-case: true # 下划线 映射 驼峰mapper-locations: classpath:mybatis/mapper/*.xml# 自定义mapper映射路径#config-location: classpath:mybatis-config.xml # mysql配置文件
1.2 实体类@Datapublic class Quality {//编号private Integer id;//地区idprivate Integer did;//检测时间@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone =" GMT+8")private Date monitorTime;//pm10private Integer pm10;//pm25private Integer pm25;//监测站private String monitorStation;//修改时间@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone =" GMT+8")private Date lastModifyTime;}
1.3 mapper 和 service1.3.1 QualityMapperpublic interface QualityMapper {//根据空气质量编号,修改空质量Integer updateQualityById(Quality qualityForm);}
1.3.2 QualityMapper.xml<!--根据空气质量编号,修改空质量Integer updateQualityById(Quality qualityForm);--><update id="updateQualityById">update air_quality<set><if test="id != null"> `id` = #{id},</if><if test="did != null"> `did` = #{did},</if><if test="monitorTime != null"> `monitor_time` = #{monitorTime},</if><if test="pm10 != null"> `pm10` = #{pm10},</if><if test="pm25 != null"> `pm25` = #{pm25},</if><if test="monitorStation != null"> `monitor_station` = #{monitorStation},</if><if test="lastModifyTime != null"> `last_modify_time` = #{lastModifyTime},</if></set>where `id` = #{id}</update>
1.3.3 service//接口public interface QualityService {boolean modifyQualityById(Quality qualityForm);}//实现类@Servicepublic class QualityServiceImpl implements QualityService {@Autowiredprivate QualityMapper qualityMapper;@Overridepublic boolean modifyQualityById(Quality qualityForm) {return qualityMapper.updateQualityById(qualityForm) > 0;}}
1.4 测试1.4.1 控制层【四 SpringBoot- 整合Mybatis,逆向工程,JPA】@Slf4j@RestControllerpublic class AirQualityController {@Autowiredprivate QualityService qualityService;//根据空气质量编号 , 修改空质量,使用xml映射文件@PostMapping("/modQuality")public Map<String,String> testModifyQualityMapperXml(@RequestBody Quality qualityForm){//返回集合Map<String,String> returnMap = new HashMap<>();//调用业务接口,修改空气详情if(qualityService.modifyQualityById(qualityForm)){returnMap.put("code","200");returnMap.put("msg","Success");return returnMap;}returnMap.put("code","9999");returnMap.put("msg","Fail");return returnMap;}}
1.4.2 Postman 测试测试:
文章插图
测试结果:
文章插图
2、逆向工程 better-mybatis-generator 插件2.1 下载插件 better-mybatis-generator
文章插图
2.2 自动生成代码2.2.1 idea连接数据库
文章插图
2.2.2生成代码2.2.2.1选中表 , 右键点击 mybatis-gengrate
文章插图
2.2.2.2生成代码 设置
文章插图
生成的代码:
文章插图
2.3 测试2.3.1 service2.3.1.1 接口
public interface AirQualityService { //根据条件查询空气质量列表List<AirQuality> getQualityListByExample(AirQualityExample airQualityExample);}
2.3.1.2 实现类@Servicepublic class AirQualityServiceImpl implements AirQualityService {@Autowired(required = false)private AirQualityMapper airQualityMapper;@Overridepublic List<AirQuality> getQualityListByExample(AirQualityExample airQualityExample) {return airQualityMapper.selectByExample(airQualityExample);}}
2.3.1 测试代码@GetMapping("/airQualities")public List<AirQuality> testAirQualityListUseBackward(){log.info("------ 根据 查询条件,查询空气质量的列表 --------");//创建查询条件对象AirQualityExample airQualityExample = new AirQualityExample();//如果需要去重 , 增加条件airQualityExample.setDistinct(true);//如果需要排序,增加排序条件airQualityExample.setOrderByClause(" id desc ");//添加自定义查询 条件 , 监测站名称中包含检测的AirQualityExample.Criteria criteria =airQualityExample.createCriteria();//criteria.andMonitorStationLike("%"+monitorStation+"%");criteria.andMonitorStationLike("%监测%");//区域编号 大于1//criteria.andDidGreaterThan(1);//区域编号在指定列表中//criteria.andDidIn(Arrays.asList(1,2,3,4,5));//增加or的查询条件AirQualityExample.Criteria criteriaOr =airQualityExample.createCriteria();criteriaOr.andPm10GreaterThan(100);//拼接or的查询条件airQualityExample.or(criteriaOr);//如果需要分页 , 增加分页参数 limit ${offset}, ${limit}//起始行 (偏移量参数)airQualityExample.setOffset(2l);//返回数据量airQualityExample.setLimit(5);//调用业务接口,查询空气质量列表List<AirQuality> qualityListByExample = airQualityService.getQualityListByExample(airQualityExample);return qualityListByExample;}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 英文4分之一 四分之一英语
- 一篇文章带你掌握主流办公框架——SpringBoot
- 原神3.1百人一揆第四天怎么用试用通关
- 撸了一个简易的配置中心,顺带整合到了SpringCloud
- 三 SpringBoot - Slf4j+logback 日志,异步请求,定时任务
- 【Spring boot】整合tomcat底层原理
- ba拼音读法 拔草的拼音
- 麻将怎么两个人玩(麻将怎么四个人联机)
- 15 基于SqlSugar的开发框架循序渐进介绍-- 整合代码生成工具进行前端界面的生成
- Springboot 之 Filter 实现超大响应 JSON 数据压缩