springboot整合mybatis-plus实现增删改查功能

一、创建数据库

字段名称

中文

类型

长度

主键

自增

默认值

备注

Id

 

Int

 

Y

 

 

 

emp_name

员工姓名

varchar

 

 

 

 

 

login_name

登录帐号

Varchar

 

 

 

 

 

login_password

登录密码

 

 

 

 

 

 

age

年龄

Int

 

 

 

 

 

gender

性别

Varchar

 

 

 

 

 

addr

地址

varchar

 

 

 

 

 

dept_name

部门名称

Varchar

 

 

 

 

 

status

状态

tinyint

 

 

 

0

0工作

1休息

2离职

默认是工作状态

deleted

是否删除

tinyint

1

 

 

0

 

 

二、使用springboot整合mybatis-plus向上表中插入数据

三、查询上表中的所有数据,不显示员工登录密码

四、将所有部门为市场部的员工的部门名称,修改为销售部

五、将所有吉林市和长春市的员工,年龄不在3050之前的,状态全部修改为休息

六、将所有60岁以上的男性员工全部从数据表中删除

 

新建一个Spring Initializr的Moduel

 

添加Lombok,Spring Web,MySQL Driver依赖

 

 

配置好pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
    <groupId>com.xzit</groupId>
    <artifactId>day5_job</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>day5_job</name>
    <description>day5_job</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <!--spring web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--mybatis plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--junit-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.xzit.Day5JobApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

 

配置一个application.yml文件

 其中logic-delete-field:deleted 就是配置数据库逻辑删除的字段是deleted

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8
    username: root
    password: zengyu1234
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    #配置数据库逻辑删除的字段
    db-config:
      logic-delete-field: deleted

 

在主类中配置一个mapper扫描器的注解,写上mapper的路径com.xzit.mapper

package com.xzit;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan(basePackages = {"com.xzit.mapper"})
public class Day5JobApplication {

    public static void main(String[] args) {
        SpringApplication.run(Day5JobApplication.class, args);
    }

}

 

按照数据库表字段新建实体类Employee

package com.xzit.entity;

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 lombok.experimental.Accessors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@TableName("employee")
public class Employee {
    /*声明这个字段为自增长字段,插入值时会插入自增长数值*/
    @TableId(type = IdType.AUTO)
    private int id;
    private String emp_name;
    private String login_name;
    /*声明该密码字段查询不返回值*/
    @TableField(select = false)
    private String login_password;
    private int age;
    private String gender;
    private String addr;
    private String dept_name;
    private int status;
    private int deleted;
}

 

在测试类中实现新增方法,

@SpringBootTest声明该类为Junit测试类
package com.xzit;

import com.xzit.entity.Employee;
import com.xzit.mapper.EmployeeMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@SpringBootTest
class Day5JobApplicationTests {
    @Resource
    private EmployeeMapper mapper;

    /*二、使用springboot整合mybatis-plus向上表中插入数据*/
    @Test
    void save() {
        List<Employee> list = new ArrayList<>();
        Employee e1 = new Employee(0,"伊万","ivan","1234.abcd",26,"男","澳大利亚墨尔本","开发部",0,0);
        Employee e2 = new Employee(0,"丁老师","evan","1234.abcd",64,"男","吉林省长春市","市场部",0,0);
        Employee e3 = new Employee(0,"陈九歌","avan","1234.abcd",26,"女","吉林省吉林市","市场部",0,0);
        Employee e4 = new Employee(0,"孔乙己","bvan","1234.abcd",35,"男","吉林省长春市","市场部",0,0);

        list.add(e1);
        list.add(e2);
        list.add(e3);
        list.add(e4);

        for (Employee e:list){
            mapper.insert(e);
        }
    }

}

执行效果

 

 

 

 

在测试类中实现查询方法

/*三、查询上表中的所有数据,不显示员工登录密码*/
    @Test
    void selectList(){
        List<Employee> list = mapper.selectList(null);//查询所有数据时不带查询参数
        list.forEach(System.out::println);
    }

执行效果

 

 

实现修改方法

/*四、将所有部门为市场部的员工的部门名称,修改为销售部*/
    @Test
    void updateByDept(){
        //声明一个Lambda员工类,使用该类可以避免字段值打错
        LambdaUpdateWrapper<Employee> wrapper= new LambdaUpdateWrapper<>();
        wrapper.set(Employee::getDept_name,"销售部")
                .like(Employee::getDept_name,"市场部");
        mapper.update(null,wrapper);
    }

执行效果

 

 

 

 

实现按年龄区间查询的功能,要上苞米豆上查询一下mybatis plus条件构造器的使用方法,使用not between这个方法实现

 

 

实现按条件修改方法

/*五、将所有吉林市和长春市的员工,年龄不在30到50之前的,状态全部修改为休息*/
    @Test
    void updateByAge(){
        Employee employee=new Employee();
        employee.setStatus(1);//0工作 1休息 2离职 默认是工作状态
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.notBetween("age",30,50);
        mapper.update(employee,wrapper);
    }

执行效果

 

 

 

 

实现删除方法

    /*六、将所有60岁以上的男性员工全部从数据表中删除*/
    @Test
    void deleteByAge(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.ge("age",60);
        mapper.delete(wrapper);
    }

执行效果

 

 

 

posted @ 2022-08-22 23:49  伊万  阅读(1163)  评论(0编辑  收藏  举报