mybatis-plus自动生成代码的调用用法(mybatisplus批量新增)

一、介绍本教程将介绍如何使用mybatis-plus工具自动给我们生成Controller、Service、Entity、Mapper、Mapper.xml层代码要求:①生成的Controller类,需要继承BaseController②生成的Entity类,需要继承BaseEntity③生成的Service,默认名称下是以I开头的接口,在生成Se…

大家好,又见面了,我是你们的朋友全栈君。

一、介绍

本教程将介绍如何使用 mybatis-plus 工具自动给我们生成 Controller、Service、Entity、Mapper、Mapper.xml 层代码

要求:

① 生成的Controller类,需要继承 BaseController

② 生成的Entity 类,需要继承 BaseEntity 

③ 生成的 Service,默认名称下是以 I 开头的接口, 在生成Service层代码中需要把这个 I 去掉

二、实现步骤

① 在数据库中创建好 数据库 与 要生成代码对应的表

这里拿 user 表举例

DROP TABLE IF EXISTS user;

CREATE TABLE user
(
	id BIGINT(20) NOT NULL COMMENT '主键ID',
	name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
	age INT(11) NULL DEFAULT NULL COMMENT '年龄',
	email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
	PRIMARY KEY (id)
);

DELETE FROM user;

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

② 创建 一个SpringBoot 项目,其中 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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.11.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lcy</groupId>
    <artifactId>mybatis-plus-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis-plus-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

③、BaseController 和  BaseEntity 类

package com.lcy.demo.sys.controller;

public class BaseController {
}


package com.lcy.demo.sys.entity;

public class BaseEntity {
}

④、 创建代码生成类

package com.lcy.demo.generator;

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 MysqlGenerator {


    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();
        //1、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");  //生成路径(一般都是生成在此项目的src/main/java下面)
        gc.setAuthor("liangcy"); //设置作者
        gc.setOpen(false);
        gc.setFileOverride(true); //第二次生成会把第一次生成的覆盖掉
        gc.setServiceName("%sService"); //生成的service接口名字首字母是否为I,这样设置就没有
        gc.setBaseResultMap(true); //生成resultMap
        mpg.setGlobalConfig(gc);

        //2、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/mybatis-plus-demo?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);

        // 3、包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("sys");
        pc.setParent("com.lcy.demo");
        mpg.setPackageInfo(pc);

        // 4、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setSuperControllerClass("com.lcy.demo.sys.controller.BaseController");
        strategy.setSuperEntityClass("com.lcy.demo.sys.entity.BaseEntity");
        // strategy.setTablePrefix("t_"); // 表名前缀
        strategy.setEntityLombokModel(true); //使用lombok
        strategy.setInclude("user");  // 逆向工程使用的表   如果要生成多个,这里可以传入String[]
        mpg.setStrategy(strategy);

        //5、执行
        mpg.execute();
    }

}

补充说明:

  • 代码生成过程中用到了 Lombok 插件, 因此需要在IDEA 中安装好 Lombok插件
  • 如果我们删除了生成的代码, 包名可能会飘红, 这时我们需要 更新一下maven工程,飘红就会消失
  • 代码将生成在我们设置的 父包名 + 模块名中

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/128470.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 博途scl编程实例_博途plc编程流程

    博途scl编程实例_博途plc编程流程SCL:StructuredContorlLanguage,结构化控制语言。在TIA博途软件中,默认支持SCL语言,在建立程序块时可以直接选择SCL语言。SCL语言类似计算机高级语言,如果你有C、Java、C++、Python这种高级语言的学习经历,再学习SCL就会容易很多。在用SCL语言编程时,主要用IF…THEN/FOR/WHILE语句去构造条件、循环、判断这样的结构,在这些结构中再次添加指令,去实现逻辑判断。所有程序的编写都是在纯文本的环境下编辑,不像梯形图那么直观。能把

  • 大文件或目录复制时的信息统计脚本

    大文件或目录复制时的信息统计脚本

  • dirsearch安装和使用[通俗易懂]

    dirsearch安装和使用[通俗易懂]目录dirsearch介绍下载及安装如何使用简单用法递归扫描线程前缀/后缀黑名单筛选器原始请求Wordlist格式排除扩展扫描子目录代理报告其他命令小贴士选项选项强制性字典设置一般设置请求设置连接设置配置dirsearch介绍dirsearch是一个基于python3的命令行工具,常用于暴力扫描页面结构,包括网页中的目录和文件。相比其他扫描工具disearch的特点是:支持HTTP……

  • Php面试问题_php面试常问面试题

    Php面试问题_php面试常问面试题1、css的定位有哪些方式?以及用法position在英文中表示“位置”的意思 它主要是用于实现对元素的定位在CSS中定位分为三种:position:fixed         固定定位position:relatvie      相对定位position:absolute      绝对定位position:static      无特殊定位 (默认值)。注意:   在使用定位属…

  • VS2008工具栏看不到配置管理器或者解决方案配置

    VS2008工具栏看不到配置管理器或者解决方案配置Vs2008工具栏看不到配置管理器或者解决方案配置,很不方便,看不出来是是Debug还是Release。解决方法如下:1、工具-选项-项目和解决方案-常规,勾选显示高级生成配置。2、如果第一步没有效果,那么可以在工具栏上点击右键,选择自定义,在命令选项卡中找到生成,然后在右侧的命令中找到这两项,直接拖动到工具栏。

  • Gerrit使用教程详解[通俗易懂]

    Gerrit使用教程详解[通俗易懂]个人觉得这几篇博客介绍挺详细的,收藏转发分享:1、gerrit使用教程(一)https://www.cnblogs.com/111testing/archive/2018/08/09/9450530.html2、git上传本地代码到远程仓库https://www.cnblogs.com/111testing/p/7663229.html3、gitlog查看提交历史https:/…

    2022年10月23日

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号