Generate code with MybatisPlus code generator

About the Author

Author name: Programming Ming Ming Shiyin
Introduction: CSDN blog expert, has been engaged in software development for many years, proficient in Java, JavaScript, bloggers also learn and grow step by step from scratch, know the importance of learning and accumulation, and like to play with the majority of ADCs Wild Upgrade, welcome your attention, and look forward to learning, growing, and taking off with you! QQ group: 798379137, welcome to join!

[Special Note] Applicable version: mybatis-plus-generator 3.5.1 and below

1. Create a Springboot project

In the previous IDEA column, there is already an article [Create a Springboot project] , so I won’t explain it separately.
insert image description here

2. Import related dependencies

Open pom.xml and fill in the following dependencies

<dependency>
    <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-boot-starter</artifactId>
     <version>3.4.1</version>
 </dependency>
 <dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-generator</artifactId>
     <version>3.4.1</version>
 </dependency>
 <dependency>
     <groupId>org.freemarker</groupId>
     <artifactId>freemarker</artifactId>
     <version>2.3.30</version>
 </dependency>
 <dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <version>1.18.6</version>
 </dependency>

3. Pull code generator code

Go directly to the [official website pull code]
attention:

  1. The path in the global configuration
  2. Data source configuration
  3. Module configuration in package configuration (the default configuration can also be modified)
 pc.setParent("com.wms")
            .setEntity("entity")
            .setMapper("mapper")
            .setService("service")
            .setServiceImpl("service.impl")
            .setController("controller");
  1. The parent class in the policy configuration can be deleted

Fourth, configure the database connection yml


server:
  port: 8090

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/ming?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root


Logging:
  level:
    com.wms: debug

insert image description here

Five, create a table

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `no` varchar(20) DEFAULT NULL COMMENT '账号',
  `name` varchar(100) NOT NULL COMMENT '名字',
  `password` varchar(20) NOT NULL COMMENT '密码',
  `age` int(11) DEFAULT NULL,
  `sex` int(11) DEFAULT NULL COMMENT '性别',
  `phone` varchar(20) DEFAULT NULL COMMENT '电话',
  `role_id` int(11) DEFAULT NULL COMMENT '角色 0超级管理员,1管理员,2普通账号',
  `isValid` varchar(4) DEFAULT 'Y' COMMENT '是否有效,Y有效,其他无效',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

6. Generate code

  1. Right-click the code generator class, click Execute, and enter the corresponding table name in the terminal console
    insert image description here

  2. After typing, press the Enter key, the effect is as follows
    insert image description here

  3. The code is generated as follows, and the corresponding controller, entity, mapper, service, and serviceImpl codes are automatically generated for us.
    insert image description here

Seven, write test code to run

  1. Modify UserController code
package com.springboot.demo.controller;
import com.springboot.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 明哥
 * @since 2022-10-30
 */
@RestController
@RequestMapping("/user")
public class UserController {
    
    

    @Autowired
    private IUserService userService;

    @GetMapping("listAll")
    public List listAll(){
    
    
        return userService.list();
    }
}

  1. Check if UserMapper is missing an annotation
    insert image description here
  2. start service
    insert image description here
  3. Browser verification
    Input address: http://localhost:8090/user/listAll
    insert image description here
  4. Try inserting a piece of data into the database
    insert image description here
    insert image description here

summary

This section summarizes "Using the MybatisPlus Code Generator to Generate Back-End Code", I hope it can be helpful to everyone, please help [Like] + [ Favorite ] , if you are interested in learning Java and front-end with Xiao Ming , [Follow a wave] Don't get lost.
Please go to the bottom of the article to help [One-click three links] Thank you!

insert image description here

Popular column recommendation

【1】Java mini-games (Tetris, Airplane Wars, Plants vs. Zombies, etc.)
【2】JavaWeb project combat (library management, online exams, dormitory management
, etc.) Code, etc.)
[4] 200 cases of getting started with Java Xiaobai
[5] Learning Java from zero, learning Java with fun
[6] Idea from zero to proficient
insert image description here

Guess you like

Origin blog.csdn.net/dkm123456/article/details/127596985
Recommended