Create a new springboot project

        Record the process of creating a new springboot project, configure mybatis-plus related dependencies, and read the data in the database. The data table createTime and updateTime fields are automatically generated and modified when adding data and modifying data

1. Select the components to create springboot in the idea

insert image description here

2. Enter the name in Group and Artifact and select the java version

insert image description here

3. Select the plug-ins required for the project

        If it is a front-end and back-end separation project, you don’t need to chooseThymeleaf
insert image description here

4. Select the file save path

insert image description here

5. After the creation is successful, wait for the project initialization

insert image description here

6. Introduce the dependency of mybatis-plus in pom.xml ( you cannot use mybatis-plus, you must use mybatis-plus-boot-starter , because it is in the springboot project and needs to have a corresponding relationship)

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3.1</version>
</dependency>

7. Change the application.properties file under resources to application.yml, and add data source configuration

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/DataBaseName?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root

# 打印sql
mybatis-plus:
  configuration:
    mapUnderscoreToCamelCase: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

8. mybatis-plus automatically generates code according to the data table, including entity class, Mapper, Service, ServiceImpl, Controller

  • Introduce dependencies
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.3.2</version>
</dependency>
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    <version>1.7</version>
</dependency>
  • Create a startup class
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class Main {
    
    
    public static void main(String[] args){
    
    
        // 创建generator对象
        AutoGenerator autoGenerator = new AutoGenerator();
        // 设置数据源
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("root");
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/DataBaseName?useUnicode=true&characterEncoding=UTF-8");
        autoGenerator.setDataSource(dataSourceConfig);
        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOpen(false);
        globalConfig.setOutputDir(System.getProperty("user.dir")+"/src/main/java");
        globalConfig.setAuthor("ws");
        globalConfig.setServiceName("%sService");
        autoGenerator.setGlobalConfig(globalConfig);
        // 包信息
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.test.allstart");
        packageConfig.setEntity("entity");
        packageConfig.setMapper("mapper");
        packageConfig.setController("controller");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        autoGenerator.setPackageInfo(packageConfig);
        // 配置策略
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        autoGenerator.setStrategy(strategyConfig);
        autoGenerator.execute();
    }
}

9. Add @MapperScan("com.test.allstart.mapper") to the startup class, otherwise the Mapper bean cannot be loaded

insert image description here

10. Add a simple code test to the user file in the Controller, and browse to "localhost:8080/user/a" to view the data in the database @RestController

@RequestMapping("/user")
public class UserController {
    
    
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/a")
    public List<User> display(){
    
    
        List<User> user = userMapper.selectList(null);
        return user;
    }

}

11. Automatically add createTime and updateTime to the data table

  • Add the following code to the entity class
@TableField(fill = FieldFill.INSERT)
private Date createTime;

@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
  • Add autofill handler
package com.test.allstart.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class addTime implements MetaObjectHandler {
    
    
    @Override
    public void insertFill(MetaObject metaObject) {
    
    
        this.setFieldValByName("createTime",new Date(),metaObject);
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
    
    
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }
}
  • test code
@Test
void insert(){
    
    
    User user = new User();
    user.setUserName("Tom");
    user.setPassword("123");
    user.setMoneryAdd("aaabbbccc");
    user.setBalancePrice(new BigDecimal(100));
    user.setHeadImage("./1.jpg");
    userMapper.insert(user);
}

@Test
void update(){
    
    
    User user = userMapper.selectById(20);
    user.setBalancePrice(new BigDecimal(200));
    QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.eq("id",20);
    userMapper.update(user,queryWrapper);
}

Guess you like

Origin blog.csdn.net/qq_43707926/article/details/127651111