com.baomidou mybatis-plus-boot-starter3.5.3.2 org.mybatis mybatis-springorg.mybatis mybatis-spring3.0.3 com.mysql mysql-connector-jorg.projectlombok lombok
配置属性信息
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: XXX password: XXX url: jdbc:mysql://localhost:3306/XXX?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&serverTimezone=Asia/Shanghai mybatis-plus: mapper-locations: classpath:/mapper/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
编写业务逻辑测试代码
package org.coding.java.controller; import com.baomidou.mybatisplus.core.metadata.IPage; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import org.coding.java.api.CommonResult; import org.coding.java.domain.user.UserDO; import org.coding.java.query.PageQueryVO; import org.coding.java.service.UserService; import org.springframework.web.bind.annotation.*; import java.util.List; @Tag(name = "用户管理") @RestController @RequestMapping("api/v1/user") public class UserController { @Resource private UserService userService; @PostMapping("/saveUser") @Operation(summary = "新增用户") public CommonResultsaveUser(@RequestBody UserDO userDO) { return userService.saveUser(userDO); } @DeleteMapping("/deleteUser/{id}") @Operation(summary = "根据ID删除用户") public CommonResult deleteUser(@PathVariable("id") Long id) { return userService.deleteUserById(id); } @GetMapping("/selectList") @Operation(summary = "查询用户信息") public CommonResult > selectList() { return userService.selectList(); } @PostMapping("/selectPage") @Operation(summary = "分页查询用户信息") public CommonResult
> selectPage(@RequestBody PageQueryVO queryVO) { return userService.selectPage(queryVO); } @PutMapping("/updateUser") @Operation(summary = "更新用户信息") public CommonResult updateUser(@RequestBody UserDO userDO) { return userService.updateUser(userDO); } }
package org.coding.java.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import jakarta.annotation.Resource; import org.coding.java.api.CommonResult; import org.coding.java.api.ResponseMessage; import org.coding.java.domain.user.UserDO; import org.coding.java.mapper.UserMapper; import org.coding.java.query.PageQueryVO; import org.coding.java.service.UserService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.Collections; import java.util.List; import java.util.Objects; @Service @Transactional public class UserServiceImpl implements UserService { @Resource private UserMapper userMapper; @Override public CommonResultsaveUser(UserDO userDO) { return CommonResult.ok(ResponseMessage.MessageCode.SAVE_SUCCESS.code(), ResponseMessage.MessageCode.SAVE_SUCCESS.message(), userMapper.insert(userDO)); } @Override public CommonResult deleteUserById(Long id) { int count = userMapper.deleteById(id); if (count > 0) { return CommonResult.ok(ResponseMessage.MessageCode.DELETE_SUCCESS.code(), ResponseMessage.MessageCode.DELETE_SUCCESS.message(), count); } return CommonResult.failed(ResponseMessage.MessageCode.DELETE_FAILED.code(), ResponseMessage.MessageCode.DELETE_FAILED.message(), count); } @Override public CommonResult > selectList() { if (getUserList().size() > 0) { return CommonResult.ok(ResponseMessage.MessageCode.SELECT_SUCCESS.code(), ResponseMessage.MessageCode.SELECT_SUCCESS.message(), getUserList()); } return CommonResult.failed(ResponseMessage.MessageCode.SELECT_FAILED.code(), ResponseMessage.MessageCode.SELECT_FAILED.message(), Collections.emptyList()); } @Override public CommonResult
> selectPage(PageQueryVO queryVO) { IPage page = new Page<>(queryVO.getPageNo(), queryVO.getPageSize()); IPage pageList = getUserPageList(queryVO, page); if (pageList.getRecords().size() > 0) { return CommonResult.ok(ResponseMessage.MessageCode.SELECT_SUCCESS.code(), ResponseMessage.MessageCode.SELECT_SUCCESS.message(), pageList); } else if (pageList.getRecords().size() == 0) { return CommonResult.failed(ResponseMessage.MessageCode.SELECT_NON_CONFORMANCE.code(), ResponseMessage.MessageCode.SELECT_NON_CONFORMANCE.message(), pageList); } return CommonResult.failed(ResponseMessage.MessageCode.SELECT_FAILED.code(), ResponseMessage.MessageCode.SELECT_FAILED.message(), pageList); } @Override public CommonResult updateUser(UserDO userDO) { int update = userMapper.updateById(userDO); if (update > 0) { return CommonResult.ok(ResponseMessage.MessageCode.UPDATE_SUCCESS.code(), ResponseMessage.MessageCode.UPDATE_SUCCESS.message(), update); } return CommonResult.failed(ResponseMessage.MessageCode.UPDATE_FAILED.code(), ResponseMessage.MessageCode.UPDATE_FAILED.message(), update); } private IPage getUserPageList(PageQueryVO queryVO, IPage page) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.like(Objects.nonNull(queryVO.getName()), UserDO::getName, queryVO.getName()); wrapper.eq(Objects.nonNull(queryVO.getPhone()), UserDO::getPhone, queryVO.getPhone()).or(); wrapper.eq(Objects.nonNull(queryVO.getEmail()), UserDO::getEmail, queryVO.getEmail()); return userMapper.selectPage(page, wrapper); } private List getUserList() { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.select("id", "name", "age", "email", "sex", "phone"); return userMapper.selectList(wrapper); } }
配置mybatis-plus分页插件
package org.coding.java.config.mybatisplus; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * @Description: mybatis常用配置 */ @Configuration @EnableTransactionManagement @MapperScan("org.coding.java.mapper") public class MybatisPlusConfig { /** * 实现分页配置 * @return */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); paginationInnerInterceptor.setDbType(DbType.MYSQL); interceptor.addInnerInterceptor(paginationInnerInterceptor); return interceptor; } }
配置mybatis-plus之属性自动填充
package org.coding.java.config.mybatisplus; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import org.apache.ibatis.reflection.MetaObject; import org.springframework.stereotype.Component; import java.time.LocalDateTime; /** * @Description: mybatis自动填充功能 */ @Component public class MyMetaObjectHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); } @Override public void updateFill(MetaObject metaObject) { this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); } }
如图所示
1、添加依赖
org.springframework.boot spring-boot-starter-webmysql mysql-connector-java8.0.32 com.github.xiaoymin knife4j-openapi3-jakarta-spring-boot-starter4.3.0 org.coding boot-cmmon0.0.1-SNAPSHOT org.mybatis mybatis3.5.13 com.github.pagehelper pagehelper-spring-boot-starter1.4.6 org.mybatis mybatis-spring3.0.3
2、配置属性
server: port: 18082 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: XXX password: XXX url: jdbc:mysql://localhost:3306/XXX?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&serverTimezone=Asia/Shanghai mybatis: mapper-locations: classpath:/mapper/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl pagehelper: helper-dialect: mysql # 指定数据库类型 reasonable: true params: count=countSql support-methods-arguments: true # springdoc-openapi项目配置 springdoc: swagger-ui: path: /swagger-ui.html tags-sorter: alpha operations-sorter: alpha api-docs: path: /v3/api-docs group-configs: - group: 'mybatis' paths-to-match: '/**' packages-to-scan: org.coding.java.controller # knife4j的增强配置,不需要增强可以不配 knife4j: enable: true setting: language: zh_cn logging: level: org.coding.java.mapper: debug
3、编写测试代码
package org.coding.java.service.impl; import com.github.pagehelper.IPage; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import jakarta.annotation.Resource; import org.coding.java.api.CommonResult; import org.coding.java.api.ResponseMessage; import org.coding.java.domain.user.UserDO; import org.coding.java.mapper.UserMapper; import org.coding.java.query.QueryPageVO; import org.coding.java.service.UserService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service @Transactional public class UserServiceImpl implements UserService { @Resource private UserMapper userMapper; @Override public CommonResultsaveUser(UserDO userDO) { return CommonResult.ok(ResponseMessage.MessageCode.SAVE_SUCCESS.code(), ResponseMessage.MessageCode.SAVE_SUCCESS.message(), userMapper.saveUser(userDO)); } @Override public CommonResult > selectList() { List
list = userMapper.selectList(); if (list.size() > 0) { return CommonResult.ok(ResponseMessage.MessageCode.SELECT_SUCCESS.code(), ResponseMessage.MessageCode.SELECT_SUCCESS.message(), list); } return CommonResult.ok(ResponseMessage.MessageCode.SELECT_FAILED.code(), ResponseMessage.MessageCode.SELECT_FAILED.message(), list); } @Override public CommonResult deleteUserById(Long id) { Integer count = userMapper.deleteUserById(id); if (count > 0) { return CommonResult.ok(ResponseMessage.MessageCode.DELETE_SUCCESS.code(), ResponseMessage.MessageCode.DELETE_SUCCESS.message(), count); } return CommonResult.ok(ResponseMessage.MessageCode.DELETE_FAILED.code(), ResponseMessage.MessageCode.DELETE_FAILED.message(), count); } @Override public CommonResult updateUser(UserDO userDO) { Integer count = userMapper.updateUser(userDO); if (count > 0) { return CommonResult.ok(ResponseMessage.MessageCode.UPDATE_SUCCESS.code(), ResponseMessage.MessageCode.UPDATE_SUCCESS.message(), count); } return CommonResult.ok(ResponseMessage.MessageCode.UPDATE_FAILED.code(), ResponseMessage.MessageCode.UPDATE_FAILED.message(), count); } @Override public CommonResult selectPage(QueryPageVO pageVO) { PageHelper.startPage(pageVO.getPageNo(), pageVO.getPageSize()); List list = userMapper.selectPage(pageVO); PageInfo pageInfo = new PageInfo<>(list); if (pageInfo.getTotal() > 0) { return CommonResult.ok(ResponseMessage.MessageCode.SELECT_SUCCESS.code(), ResponseMessage.MessageCode.SELECT_SUCCESS.message(), pageInfo); } return CommonResult.ok(ResponseMessage.MessageCode.SELECT_FAILED.code(), ResponseMessage.MessageCode.SELECT_FAILED.message(), pageInfo); } }
package org.coding.java.controller; import com.github.pagehelper.IPage; import com.github.pagehelper.PageInfo; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import org.coding.java.api.CommonResult; import org.coding.java.domain.user.UserDO; import org.coding.java.query.QueryPageVO; import org.coding.java.service.UserService; import org.springframework.web.bind.annotation.*; import java.util.List; @Tag(name = "用户管理") @RestController @RequestMapping("/api/v1/user") public class UserController { @Resource private UserService userService; @PostMapping("/saveUser") @Operation(summary = "新增用户") public CommonResultsaveUser(@RequestBody UserDO userDO) { return userService.saveUser(userDO); } @GetMapping("/selectList") @Operation(summary = "查询用户【不分页】") public CommonResult > selectList() { return userService.selectList(); } @PostMapping("/selectPage") @Operation(summary = "查询用户【分页】") public CommonResult
selectPage(@RequestBody QueryPageVO pageVO) { return userService.selectPage(pageVO); } @DeleteMapping("/deleteUserById/{id}") @Operation(summary = "删除用户【根据ID】") public CommonResult deleteUserById(@PathVariable("id") Long id) { return userService.deleteUserById(id); } @PutMapping("/updateUser") @Operation(summary = "编辑用户【根据ID】") public CommonResult updateUser(@RequestBody UserDO userDO) { return userService.updateUser(userDO); } }
4、XML文件
insert into user(name,age,phone,sex,email) value(#{name},#{age},#{phone},#{sex},#{email}) delete from user where id = #{id} update user where id = #{id} name = #{name}, age = #{age}, phone = #{phone}, sex = #{sex}, email = #{email}, update_time = now()
5、测试数据是否能走通