Springboot 整合mybatis-plus +代码生成器

2023-11-13

mybatis-plus官方文档:https://mp.baomidou.com/guide/
新建一个Springboot项目 ;代码生成结构如下
在这里插入图片描述一。添加依赖

<dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>
        <!-- 导入mysql驱动包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <!-- 导入数据源依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.9</version>
        </dependency>
         <!--利用fastJson替换掉jackson,且解决中文乱码问题-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.54</version>
        </dependency>
        <!-- 分页插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!--mybatisplus插件-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.1.1</version>
        </dependency>
        <!--生成策略-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.1.1</version>
        </dependency>
        <!-- 这是模板引擎依赖 -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
        </dependency>
        <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>

二。application.yml配置文件

server:
  port: 9017
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    druid:
      initialSize: 1
      minIdle: 1
      maxActive: 10
      maxWait: 10000
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      validationQuery: select 1
      testWhileIdle: true
      testOnBorrow: true
      testOnReturn: false
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
      filters: stat
  profiles:
        include: jdbc

mybatis-plus:
  # 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml
  # 如果是放在resource目录 classpath:/mapper/*Mapper.xml
  mapper-locations: classpath:mapper/*Mapper.xml
  #实体扫描,多个package用逗号或者分号分隔
  typeAliasesPackage: com.demo.updown.entity
  global-config:
    db-config:
      column-underline: false
      logic-delete-value: 1
      logic-not-delete-value: 0
    refresh: true
    configuration:
      map-underscore-to-camel-case: true
      cache-enabled: false
      jdbc-type-for-null: 'null'
      log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
      database-id: mysql
    configuration-properties:
      blobType: BLOB
      boolValue: TRUE
      prefix:

到这基本配置完成,下面是代码生成器

package com.demo.updown.until;



import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

public class MysqlGenertor {

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        // TODO 设置用户名
        gc.setAuthor("root");
        gc.setOpen(true);
        // service 命名方式
        gc.setServiceName("%sService");
        // service impl 命名方式
        gc.setServiceImplName("%sServiceImpl");
        // 自定义文件命名,注意 %s 会自动填充表实体属性!
        gc.setMapperName("%sDao");
        gc.setXmlName("%sMapper");
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        // XML 二级缓存
        gc.setEnableCache(false);
        // XML ResultMap
        gc.setBaseResultMap(true);
        // XML columList
        gc.setBaseColumnList(false);
        mpg.setGlobalConfig(gc);

        // TODO 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();

        dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        mpg.setDataSource(dsc);

        // TODO 包配置
        PackageConfig pc = new PackageConfig();
        //pc.setModuleName(scanner("模块名"));
        pc.setParent("com.demo.updown");//项目的包名
        pc.setEntity("entity");// 实体类
        pc.setService("service");//接口包
        pc.setMapper("mapper");//dao层
        pc.setServiceImpl("serviceImpl");//实现类
        mpg.setPackageInfo(pc);

        // 自定义需要填充的字段
        List<TableFill> tableFillList = new ArrayList<>();
        //如 每张表都有一个创建时间、修改时间
        //而且这基本上就是通用的了,新增时,创建时间和修改时间同时修改
        //修改时,修改时间会修改,
        //虽然像Mysql数据库有自动更新几只,但像ORACLE的数据库就没有了,
        //使用公共字段填充功能,就可以实现,自动按场景更新了。
        //如下是配置
        //TableFill createField = new TableFill("gmt_create", FieldFill.INSERT);
        //TableFill modifiedField = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
        //tableFillList.add(createField);
        //tableFillList.add(modifiedField);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称 这里设置生成的mapper.xml文件的存放位置
                return projectPath + "/src/main/resources/mapper/"
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        // 设置逻辑删除键
        strategy.setLogicDeleteFieldName("deleted");
        // TODO 指定生成的bean的数据库表名
        strategy.setInclude("student");
        //strategy.setSuperEntityColumns("id");
        // 驼峰转连字符
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);
        // 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有!
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

}

```运行代码即可

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Springboot 整合mybatis-plus +代码生成器 的相关文章

随机推荐

  • 如何运行后缀名为.ipynb的文件

    打开cmd 输入 pip install jupyter notebook 安装截图 下载之后 输入 jupyter notebook 之后浏览器会弹出一个页面 如图 然后就可以打开电脑里的文件 如图 也可以选择upload你的文件 打开i
  • Android 下拉刷新实践

    1 手动实现一个下拉刷新功能 2 效果图 3 view结构 4 实现思路
  • linux系统调用线程

    1 基础概念 早期unix系统中 没有线程概念 后来才引入线程 linxu 为了迎合 windows引入了线程 linux 上进程是非常优秀了 linux 上用线程和进程的区别不大 老程序都是用进程 gdb不支持线程 因为gdb比线程出现了
  • net core 下的图形验证码

    首先 通过 Nuget 安装 dotnet add package Lazy Captcha Core 注册服务 默认使用了内存存储 AddDistributedMemoryCache builder Services AddCaptcha
  • 什么是IDP?---What Is an Internal Developer Platform (IDP)?

    The modern approach to software delivery is based on cloud native services and the DevOps culture entailing software dev
  • 项目-天气邮局

    一 项目背景 http协议被广泛使用 从移动端 pc端浏览器 http协议无疑是打开互联网应用窗口的重要协议 http在网络应用层中的地位不可撼动 是能准确区分前后台的重要协议 在学习完网络的有关知识后 HTTP服务器无疑是巩固及应用所学知
  • 怎么用linux查看xml文件格式,xml是什么格式?xml文件格式用什么软件可以打开

    xml是什么格式 xml文件是很多用户在电脑上看见过了 很多小伙伴看到了xml格式的文件都不知道这个是什么东东 其实这个xml也是一种比较有用的文件 可以用来存储软件数据 不过不是所有的软件都可以打开的 下面智能手机网就来科普一下xml是什
  • 各种虚拟机体验杂谈 --- 兼发布 google chrome os (chromiumos) vmware版本

    前两天赶时髦 把笔记本换上了win8 pro 换win8pro的原因 一个是价格真的很有诚意 另一个就是从DP版本开始就一直用 虽然兼容性问题多多 但作为宿主主机还行 而且xenclient也实在是让人窝火 号称裸机虚拟 其实硬盘速度慢如蜗
  • 读论文(五)MedDialog【参考性大】【可复现】

    Abstract 医疗对话系统有望帮助远程医疗增加医疗保健服务的可及性 提高患者护理质量并降低医疗成本 为促进医学对话系统的研发 我们构建了大规模的医学对话数据集 MedDialog 其中包含中文数据集340万条医患对话 英文数据集120条
  • 24 个 ES6 方法,解决实际开发的 JS 问题

    1 如何隐藏所有指定的元素 tips 本文主要介绍 24 中 es6 方法 这些方法都挺实用的 本本请记好 时不时翻出来看看 const hide el gt Array from el forEach e gt e style displ
  • 使用 Socket 通信实现 FTP 客户端程序

    转 https www ibm com developerworks cn linux l cn socketftp index html FTP FTP 概述 文件传输协议 FTP 作为网络共享文件的传输协议 在网络应用软件中具有广泛的应
  • python是一门面向过程的语言有哪些,python是面向过程的吗

    python是面向过程的吗 1 面向过程 核心是过程二字 过程指的是解决问题的步骤 好比如设计一条流水线 是一种机械式的思维方式 就是程序从上到下一步步执行 一步步从上到下 从头到尾的解决问题 基本设计思路就是程序一开始是要着手解决一个大的
  • 迷你Web文件服务器

    在开发Web程序的时候 有时候需要一个轻量级的Web服务器 用来响应前端的请求 前端一般的请求可以通过本地文件的方式显示 但是毕竟不是真正的Web服务器 有了这个需求 我们开发了一款迷你绿色通用的Web文件服务器 下载地址 WebServe
  • Ubuntu16.04.7+Qt15.5.0环境配置(一条龙讲解)

    目录 1 下载并安装Ubuntu 2 Qt下载与安装 3 Qt环境配置 4 设置编译套件 5 创建qt快速启动脚本 1 下载并安装Ubuntu Ubuntu16 04 7下载链接https releases ubuntu com xenia
  • ipconfig bash: ipconfig: command not found...

    在使用linux查看端口的时候 应该用ifconfig Windows才使用ipconfig
  • Qt 操作SQLite数据库

    一 SQLite 介绍 Sqlite 数据库作为 Qt 项目开发中经常使用的一个轻量级的数据库 可以说是兼容性相对比较好的数据库之一 Sqlite就像Qt的亲儿子 如同微软兼容Access数据库一样 Qt5 以上版本可以直接使用 Qt自带驱
  • 09字符串排序

    给定两个字符串 从字符串2中找出字符串1中的所有字符 去重并按照ASCII码值从小到大排列 输入字符串1长度不超过1024 字符串2长度不超过100 字符范围满足ASCII编码要求 按照ASCII由小到大排序 输入描述 bach bbaac
  • CBAM:融合通道和空间注意力的注意力模块

    点击上方 AI公园 关注公众号 选择加 星标 或 置顶 作者 Sik Ho Tsang 编译 ronghuaiyang 导读 使用CBAM加持的MobileNetV1 ResNeXt ResNet WRN优于使用SENet的网络 在这篇文章
  • java:统计数组中元素出现的个数

    问题描述 定义一个方法传入一个int类型数组 输出这个数组中每一个数字及其出现的个数 例如 传入数组 1 2 2 2 3 3 4 4 4 4 打印结果 数字1出现了1次 数字2出现了3次 算法思想 这里主要是在实现数组元素的遍历过程中 如果
  • Springboot 整合mybatis-plus +代码生成器

    mybatis plus官方文档 https mp baomidou com guide 新建一个Springboot项目 代码生成结构如下 一 添加依赖