The use of Mybatis Plus general enumeration

Enumeration types are often used in daily development. Using enumerations in Mybatis Plus can improve the readability of the code and save data type conversion, which is very convenient.

First add the dependent packages:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<!-- lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>
<!-- hutool -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.2.5</version>
</dependency>
<!-- mybatis plus -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.0.5</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-extension</artifactId>
    <version>3.4.0</version>
</dependency>
<!-- druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.21</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-commons</artifactId>
    <version>2.2.4.RELEASE</version>
</dependency>

Then follow the steps to configure step by step:

1. Define the enumeration type

package com.example.demo.enums;

import com.baomidou.mybatisplus.annotation.IEnum;
import lombok.Getter;

@Getter
public enum GenderTypeEnum implements IEnum<Integer> {
    
    

    /**
     * 女
     */
    WOMAN(0, "女"),

    /**
     * 男
     */
    MAN(1, "男"),

    /**
     * 保密
     */
    PRIVACY(2, "保密");

    private Integer value;

    private String message;

    GenderTypeEnum(Integer value, String message) {
    
    
        this.value = value;
        this.message = message;
    }

    @Override
    public Integer getValue() {
    
    
        return this.value;
    }

    @Override
    public String toString() {
    
    
        return this.message;
    }
}

2. Configure enumeration packet scanning

There are two ways to configure:

(1) Add configuration items in the configuration file

mybatis-plus.type-enums-package=com.example.demo.enums

(2) Add configuration items in the Java configuration class

	@Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
    
    
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        ......
        sqlSessionFactory.setTypeEnumsPackage("com.example.demo.enums");
        return sqlSessionFactory.getObject();
    }

The path here: The com.example.demo.enumsconfiguration is the name of the package where the enumeration class is located.

3. Create a database table

CREATE TABLE `user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `sex` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

4. Create an entity class that uses enumeration

package com.example.demo.entity;

import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import com.example.demo.enums.GenderTypeEnum;
import lombok.Data;
import org.springframework.data.annotation.Id;

/**
 * @author: jichunyang
 * @description: 用户实体类
 * @date: 2021/3/18 10:34
 **/
@Data
@TableName("user")
public class User {
    
    
    /**
     * 主键
     */
    @Id
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 名字
     */
    private String name;

    /**
     * 性别(定义为枚举类型)
     */
    private GenderTypeEnum sex = GenderTypeEnum.PRIVACY;
}

The gender here is defined as an enumerated type, and the actual library table stores an integer type, and the data content is as follows:
Insert picture description here
5. Create the service class used by the query

package com.example.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.User;
import com.example.demo.enums.GenderTypeEnum;
import com.example.demo.entity.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: jichunyang
 * @description: 服务类
 * @date: 2021/3/18 14:52
 **/
@Service
public class EnumService {
    
    

    @Autowired
    private UserMapper userMapper;

    public Map getAllUsers() {
    
    
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        // 只查询性别为“女”的用户
        queryWrapper.eq("sex", GenderTypeEnum.WOMAN);
        List<User> users = userMapper.selectList(queryWrapper);
        Map<String, List> map = new HashMap<>();
        map.put("info", users);
        return map;
    }
}

6. Create the control class used by the query

package com.example.demo.controller;

import com.example.demo.service.EnumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author: jichunyang
 * @description:
 * @date: 2021/3/18 10:24
 **/
@RestController
public class EnumController {
    
    

    @Autowired
    private EnumService enumService;

    @GetMapping("/getAllUsers")
    public Map getAllUsers() {
    
    
        return enumService.getAllUsers();
    }
}

After startup, the corresponding data can be obtained by accessing the path:
Insert picture description here

Guess you like

Origin blog.csdn.net/j1231230/article/details/114941839