• SpringBoot集成MyBatis-Plus


    SpringBoot集成MyBatis-Plus


    在这里插入图片描述
    懒得打一遍,直接copy: SpringBoot集成MyBatis-Plus

    application.yml

    # 端口
    server:
      port: 8080
    # 数据源
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/employee?characterEncoding=utf-8&useSSL=false
        username: 
        password: 
    
    # mybatis-plus设置
    mybatis-plus:
      mapper-locations: classpath:mapper/*.xml  #mapper.xml文件位置,如果没有映射文件,请注释掉。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    pom.xml

    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.3.2.RELEASEversion>
            <relativePath/> 
        parent>
        <groupId>com.examplegroupId>
        <artifactId>SpringBoot_Mybatis_plus_crud_pageartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>SpringBoot_Mybatis_plus_crud_pagename>
        <description>SpringBoot_Mybatis_plus_crud_pagedescription>
        <properties>
            <java.version>1.8java.version>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.bootgroupId>
                        <artifactId>spring-boot-starter-loggingartifactId>
                    exclusion>
                exclusions>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-log4j2artifactId>
            dependency>
            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-jdbcartifactId>
            dependency>
            
            <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>mybatis-plus-boot-starterartifactId>
                <version>3.1.2version>
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>8.0.30version>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
            dependency>
    
        dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
            plugins>
        build>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    pojo

    package com.example.springboot_mybatis_plus_crud_page.pojo;
    
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableField;
    import com.baomidou.mybatisplus.annotation.TableId;
    import com.baomidou.mybatisplus.annotation.TableName;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import java.io.Serializable;
    
    @Data
    @TableName(value = "t_emp") // 当实体类名与表名不一样时,使用
    @AllArgsConstructor
    @NoArgsConstructor
    public class Emp implements Serializable {
    
        //标识为主键
        @TableId(type = IdType.AUTO, value = "emp_id")
        private Integer empId;
        @TableField(value = "emp_name")
        private String empName;
        @TableField(value = "age")
        private String age;
        @TableField(value = "gender")
        private String gender;
        @TableField(value = "email")
        private String email;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    package com.example.springboot_mybatis_plus_crud_page.pojo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.experimental.Accessors;
    
    import java.io.Serializable;
    
    /**
     * @Author moon
     * @Date 2023/10/3 14:35
     * @Description
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Result implements Serializable {
        private boolean success;
        private String message;
        private Object data;
    
        public Result(boolean success, String message) {
            this.success = success;
            this.message = message;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    mapper

    package com.example.springboot_mybatis_plus_crud_page.mapper;
    
    import com.baomidou.mybatisplus.core.conditions.Wrapper;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.example.springboot_mybatis_plus_crud_page.pojo.Emp;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.List;
    
    public interface EmpMapper extends BaseMapper<Emp> {
    
        IPage<Emp> selectPage(Page page);
    
        Integer addEmp(Emp emp);
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.example.springboot_mybatis_plus_crud_page.mapper.EmpMapper">
        
        <select id="selectPage" resultType="com.example.springboot_mybatis_plus_crud_page.pojo.Emp">
            select * from t_emp
        select>
    
        
        <insert id="addEmp" keyProperty="empId">
            insert into t_emp values (null,#{empName},#{age},#{gender},#{email})
        insert>
    
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    service

    package com.example.springboot_mybatis_plus_crud_page.service;
    
    
    import com.baomidou.mybatisplus.core.conditions.Wrapper;
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.example.springboot_mybatis_plus_crud_page.pojo.Emp;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.List;
    
    public interface EmpService{
    
        List<Emp> finList();
    
        IPage<Emp> selectPage(Page<Emp> page);
    
        Emp selectById(Integer id);
    
        List<Emp> selectList(Wrapper<Emp> queryWrapper);
    
        Integer addEmp(Emp emp);
    
        Integer updateById(Emp emp);
    
        Integer deleteById(Integer id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    serviceimpl

    package com.example.springboot_mybatis_plus_crud_page.service.impl;
    
    import com.baomidou.mybatisplus.core.conditions.Wrapper;
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.example.springboot_mybatis_plus_crud_page.mapper.EmpMapper;
    import com.example.springboot_mybatis_plus_crud_page.pojo.Emp;
    import com.example.springboot_mybatis_plus_crud_page.service.EmpService;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    import java.util.List;
    
    /**
     * @Author moon
     * @Date 2023/10/3 14:03
     * @Description
     */
    @Service
    public class EmpServiceImpl implements EmpService {
    
        @Resource
        EmpMapper empMapper;
    
        @Override
        public List<Emp> finList() {
            return empMapper.selectList(null);
        }
    
        @Override
        public IPage<Emp> selectPage(Page<Emp> page) {
            return empMapper.selectPage(page);
        }
    
        @Override
        public Emp selectById(Integer id) {
            return empMapper.selectById(id);
        }
    
        @Override
        public List<Emp> selectList(Wrapper<Emp> queryWrapper) {
            return empMapper.selectList(queryWrapper);
        }
    
        @Override
        public Integer addEmp(Emp emp) {
            return empMapper.addEmp(emp);
        }
    
        @Override
        public Integer updateById(Emp emp) {
            return empMapper.updateById(emp);
        }
    
        @Override
        public Integer deleteById(Integer id) {
            return empMapper.deleteById(id);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    config

    package com.example.springboot_mybatis_plus_crud_page.config;
    
    import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    
    /**
     * @Author moon
     * @Date 2023/10/3 15:08
     * @Description
     */
    @EnableTransactionManagement
    @Configuration
    @MapperScan("com.example.springboot_mybatis_plus_crud_page.mapper")
    public class MyBatisPlusConfig {
    
        //分页插件
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
            return paginationInterceptor;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    utils

    package com.example.springboot_mybatis_plus_crud_page.utils;
    
    /**
     * 数据统一放回类
     */
    public class ResponseResult {
        private Integer state;
        private String msg;
        private Object data;
    
        public ResponseResult() {
            this.state = 200;
            this.msg = "成功";
        }
    
        public ResponseResult(Integer state, String msg) {
            this.state = state;
            this.msg = msg;
        }
    
        public ResponseResult(Integer state, String msg, Object data) {
            this.state = state;
            this.msg = msg;
            this.data = data;
        }
    
        public Integer getState() {
            return state;
        }
    
        public void setState(Integer state) {
            this.state = state;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
        public Object getData() {
            return data;
        }
    
        public void setData(Object data) {
            this.data = data;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    web

    package com.example.springboot_mybatis_plus_crud_page.web;
    
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.example.springboot_mybatis_plus_crud_page.pojo.Emp;
    import com.example.springboot_mybatis_plus_crud_page.pojo.Result;
    import com.example.springboot_mybatis_plus_crud_page.service.EmpService;
    import org.apache.catalina.User;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.*;
    
    import javax.annotation.Resource;
    import java.util.List;
    import java.util.Map;
    
    /**
     * @Author moon
     * @Date 2023/10/3 14:50
     * @Description
     */
    @RestController
    public class EmpController {
        @Resource
        private EmpService empService;
    
        //查询所有
        @GetMapping("emp/findList")
        public ResponseEntity<List<Emp>> findList() {
            List<Emp> empList = empService.finList();
            return ResponseEntity.ok(empList);
        }
    
        //根据id查询
        @GetMapping("emp/{id}")
        public ResponseEntity<Emp> findEmpById(@PathVariable Integer id) {
            return ResponseEntity.ok(empService.selectById(id));
        }
    
        //条件查询
        @GetMapping("emp/condition")
        public ResponseEntity<List<Emp>> findEmpByCondition(@RequestBody Map<String, String> map) {
            System.out.println(map.get("key") + map.get("value"));
            QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
            return ResponseEntity.ok(empService.selectList(queryWrapper.eq(map.get("key"),map.get("value"))));
    
        }
    
        //模糊查询
        @GetMapping("emp/like")
        public ResponseEntity<List<Emp>> findEmpLike(@RequestBody Map<String, String> map) {
            QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
            return ResponseEntity.ok(empService.selectList(queryWrapper.like(map.get("key"),map.get("value"))));
        }
    
        //插入
        @PostMapping("emp/add")
        public ResponseEntity<Integer> addEmpOne(@RequestBody Emp emp) {
            return ResponseEntity.ok(empService.addEmp(emp));
        }
    
        //通过id修改
        @PutMapping("emp/update/{id}")
        public ResponseEntity<Integer> update(@PathVariable Integer id) {
            Emp emp = empService.selectById(id);
            return ResponseEntity.ok(empService.updateById(emp));
        }
    
        //通过id删除
        @DeleteMapping("emp/delete/{id}")
        public ResponseEntity<Integer> deleteById(@PathVariable Integer id) {
            System.out.println(id);
            return ResponseEntity.ok(empService.deleteById(id));
        }
    
        @GetMapping("emp/selectPage")
        public IPage<Emp> selectPage() {
            IPage<Emp> empIPage = empService.selectPage(new Page<>(1,5));
            return empIPage;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
  • 相关阅读:
    基于java(ssm)大学生社团管理系统源码成品(java毕业设计)
    关于LoRa模块你需要知道的一切
    治愈系书单|林曦《只生欢喜不生愁》
    kubernetes的多租户管理实践
    计算机毕业设计Java健身房管理系统(源码+系统+mysql数据库+lw文档)
    【毕业设计】基于SSM的酒店客房信息管理系统 - java web
    如何隐藏Selenium特征实现自动化网页采集
    [含文档+PPT+源码等]精品基于Uniapp+SSM实现的安卓的掌上校园系统[包运行成功]计算机毕业设计Android项目源码
    Qt在工控行业的一些重点知识点
    背后的力量 | 开启智能化教学新体验 华云数据助力天长市工业学校打造新型IT实训室
  • 原文地址:https://blog.csdn.net/qq_41298636/article/details/134212354