当前位置:   article > 正文

SpringBoot使用Mybatis-puls实现增删改查_springbootmybatisplus增删改查

springbootmybatisplus增删改查

一、Mybatis和Mybatis-Plus

  • Mybatis是一个优秀的持久性框架,它简化了 jdbc 的代码,可以使用简单的 xml 或注解来配置来映射;
  • mybatis-plus 是 mybatis 的增强工具,它在 mybatis 的基础上又添加了许多的功能,在 mybatis-plus 上既可以使用自身特有的功能,还可以使用 mybatis 的原生功能;所以说mybatis-plus 是为简化开发,提高效率而生。它封装了基本的增删改查操作,使我们自己不需要再去写很多重复的代码,大大解放了生产力!

二、目录结构

三、pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <parent>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-parent</artifactId>
  7. <version>2.3.2.RELEASE</version>
  8. <relativePath/> <!-- lookup parent from repository -->
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <groupId>com.llh</groupId>
  12. <artifactId>spring-boot-demo</artifactId>
  13. <version>1.0.0</version>
  14. <name>spring-boot-demo</name>
  15. <description>springboot project description</description>
  16. <properties>
  17. <mybatis-spring-boot.version>2.1.4</mybatis-spring-boot.version>
  18. <mybatis-plus.version>3.4.2</mybatis-plus.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>mysql</groupId>
  31. <artifactId>mysql-connector-java</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.mybatis.spring.boot</groupId>
  35. <artifactId>mybatis-spring-boot-starter</artifactId>
  36. <version>${mybatis-spring-boot.version}</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>com.baomidou</groupId>
  40. <artifactId>mybatis-plus-boot-starter</artifactId>
  41. <version>${mybatis-plus.version}</version>
  42. </dependency>
  43. </dependencies>
  44. </project>

四、application.properties

application.properties配置文件

  1. server:
  2. port: 8080
  3. spring:
  4. datasource:
  5. driver-class-name: com.mysql.cj.jdbc.Driver
  6. url: jdbc:mysql://localhost:3306/budongchan?serverTimezone=UTC&userUnicode=true&useSSL=false
  7. username: root
  8. password: 123456
  9. servlet:
  10. multipart:
  11. max-file-size: 5MB # 限制文件大小为5MB
  12. mvc:
  13. pathmatch:
  14. matching-strategy: ant_path_matcher

五、逻辑代码

SpringBootDemoApplication 类

  1. package com.example.budongchan;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.scheduling.annotation.EnableScheduling;
  6. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  7. @SpringBootApplication
  8. @MapperScan("com.example.budongchan.mapper")
  9. public class BudongchanApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(BudongchanApplication.class, args);
  12. }
  13. }

实体类:OrderPosition 类

  1. package com.example.budongchan.po;
  2. import com.baomidou.mybatisplus.annotation.IdType;
  3. import com.baomidou.mybatisplus.annotation.TableId;
  4. import lombok.Data;
  5. import lombok.EqualsAndHashCode;
  6. import lombok.experimental.Accessors;
  7. import java.io.Serializable;
  8. /**
  9. * <p>
  10. *
  11. * </p>
  12. *
  13. * @author yixin
  14. * @since 2023-05-06
  15. */
  16. @Data
  17. @EqualsAndHashCode(callSuper = false)
  18. @Accessors(chain = true)
  19. public class OrderPosition implements Serializable {
  20. private static final long serialVersionUID = 1L;
  21. /**
  22. * 主键
  23. */
  24. @TableId(value = "id", type = IdType.AUTO)
  25. private Long id;
  26. /**
  27. * 省
  28. */
  29. private String provice;
  30. /**
  31. * 市
  32. */
  33. private String city;
  34. /**
  35. * 预约中心
  36. */
  37. private String center;
  38. /**
  39. * 窗口数量
  40. */
  41. private Integer windows;
  42. }

Mapper 类:OrderPositionMapper类(dao层基本的增删改查操作)

  1. package com.example.budongchan.mapper;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.example.budongchan.po.OrderPosition;
  4. /**
  5. * <p>
  6. * Mapper 接口
  7. * </p>
  8. *
  9. * @author yixin
  10. * @since 2023-05-06
  11. */
  12. public interface OrderPositionMapper extends BaseMapper<OrderPosition> {
  13. }

Service 类:OrderPositionService 类

  1. package com.example.budongchan.service;
  2. import com.baomidou.mybatisplus.extension.service.IService;
  3. import com.example.budongchan.dto.PageDto;
  4. import com.example.budongchan.dto.Result;
  5. import com.example.budongchan.po.OrderPosition;
  6. import com.example.budongchan.po.OrderServetype;
  7. import javax.servlet.http.HttpServletRequest;
  8. /**
  9. * <p>
  10. * 服务类
  11. * </p>
  12. *
  13. * @author yixin
  14. * @since 2023-05-06
  15. */
  16. public interface OrderPositionService extends IService<OrderPosition> {
  17. public Result getmessage(PageDto pageDto) ;
  18. public Result getOneMessage(Long id);
  19. Result modifyCenter(OrderPosition orderPosition);
  20. }

ServiceImpl 类:OrderPositionServiceImpl 类

  1. package com.example.budongchan.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import com.example.budongchan.dto.PageDto;
  7. import com.example.budongchan.dto.Result;
  8. import com.example.budongchan.mapper.OrderPositionMapper;
  9. import com.example.budongchan.po.OrderPosition;
  10. import com.example.budongchan.po.OrderServetype;
  11. import com.example.budongchan.service.OrderPositionService;
  12. import com.example.budongchan.service.OrderServetypeService;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. /**
  16. * <p>
  17. * 服务实现类
  18. * </p>
  19. *
  20. * @author yixin
  21. * @since 2023-05-06
  22. */
  23. @Service
  24. public class OrderPositionServiceImpl extends ServiceImpl<OrderPositionMapper, OrderPosition> implements OrderPositionService {
  25. @Autowired
  26. OrderServetypeService orderServetypeService;
  27. @Override
  28. public Result getmessage(PageDto pageDto) {
  29. Page<OrderPosition> page = new Page<>(pageDto.getPageNum(), pageDto.getPageSize());
  30. IPage<OrderPosition> pageOrder = this.page(page,null);
  31. System.out.println(pageOrder.getRecords());
  32. return Result.success("查询成功",pageOrder);
  33. }
  34. @Override
  35. public Result getOneMessage(Long id) {
  36. OrderPosition one = this.getOne(new LambdaQueryWrapper<OrderPosition>().eq(OrderPosition::getId, id));
  37. return Result.success("成功",one);
  38. }
  39. @Override
  40. public Result modifyCenter(OrderPosition orderPosition) {
  41. boolean update = this.update(orderPosition, new LambdaQueryWrapper<OrderPosition>().eq(OrderPosition::getId, orderPosition.getId()));
  42. if(update){
  43. return Result.success("修改成功");
  44. }else{
  45. return Result.error("修改失败");
  46. }
  47. }
  48. }

Controller 类:OrderPositionController 类

  1. package com.example.budongchan.controller;
  2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  3. import com.example.budongchan.dto.PageDto;
  4. import com.example.budongchan.dto.Result;
  5. import com.example.budongchan.global.R;
  6. import com.example.budongchan.po.*;
  7. import com.example.budongchan.service.*;
  8. import io.swagger.annotations.Api;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.*;
  11. import javax.xml.ws.RequestWrapper;
  12. @Api(tags = "管理员")
  13. @RestController
  14. public class managetController {
  15. @Autowired
  16. OrderPositionService orderPositionService;
  17. @Autowired
  18. ManagerService managerService;
  19. @PostMapping("/getmessage")
  20. public Result getmessage(@RequestBody PageDto pageDto){
  21. return orderPositionService.getmessage(pageDto);
  22. }
  23. @GetMapping("/getonemessage/{id}")
  24. public Result getOneMessage(@PathVariable Long id){
  25. return orderPositionService.getOneMessage(id);
  26. }
  27. @PostMapping("/modifyCenter")
  28. public Result modifyCenter(@RequestBody OrderPosition orderPosition){
  29. return orderPositionService.modifyCenter(orderPosition);
  30. }
  31. @GetMapping("/getType/{position}")
  32. public Result getType(@PathVariable String position){
  33. return orderServetypeService.getType(position);
  34. }
  35. }
  • Mybatis属于半配置型的数据持久化框架,JPA是直接封装了所有的SQL操作,相对于JPA来说Mybatis更灵活,可以自定义SQL。
  • Mybatis-Plus又为你封装了常用的增删改查操作,这样既可以在特殊情况下自定义SQL,又避免了写大量重复的增删改查。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/598327
推荐阅读
相关标签
  

闽ICP备14008679号