创建springboot项目,选择图片中所示依赖



mybatis-plus生成的依赖


<!-- mybatis_plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>

自动生成代码


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import java.util.ArrayList; /**
* 自动生成
*/
public class generate {
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");//在当前路径上加上这个路径
gc.setAuthor("name");//添加作者信息
gc.setOpen(false);//是否打开资源管理器
gc.setFileOverride(false);//是否覆盖文件
gc.setServiceName("%sService");//去除服务接口service前缀
gc.setIdType(IdType.ID_WORKER);//使用默认主键
gc.setDateType(DateType.ONLY_DATE);//设置默认的时间格式
gc.setSwagger2(false); //实体属性 Swagger2 注解
mpg.setGlobalConfig(gc);//把全局配置放入代码生成器中 // 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
//在这里需要改成你自己的数据库
dsc.setUrl("jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");//用户名
dsc.setPassword("root");//密码
dsc.setDbType(DbType.MYSQL);//设置默认类型
mpg.setDataSource(dsc);//将数据源配置放入代码生成器中 // 包配置
PackageConfig pc = new PackageConfig();
//这里需要设置自己的包路径名
pc.setModuleName("generate");//设置模块名
pc.setParent("com");//设置包名,这样com.zjh.mybatis
pc.setEntity("entity");//设置实体类的包名
pc.setMapper("mapper");//设置持久层的包名
pc.setService("service");//设置业务层的包名
pc.setController("controller");//设置表现层(控制层)的包名
mpg.setPackageInfo(pc);//将包的配置放入到自动代码生成器中 // 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("leader_schedule_main");//设置要包含生成的表,生成多个表以逗号相隔,例如strategy.setInclude("表1","表2");
strategy.setNaming(NamingStrategy.underline_to_camel);//设置驼峰命名的自动映射
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//设置列名的驼峰命名自动映射
strategy.setEntityLombokModel(true);//设置是否支持lombok
strategy.setRestControllerStyle(true); strategy.setLogicDeleteFieldName("deleted");//自动配置逻辑删除字段
//自动填充配置
TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT);
//创建一个list集合,把两个自动填充策略加入到这个集合
ArrayList<TableFill> tableFill = new ArrayList<>();
tableFill.add(gmtCreate);
tableFill.add(gmtModified);
strategy.setTableFillList(tableFill);//把自动填充放入到配置策略
strategy.setVersionFieldName("version");//开启乐观锁
strategy.setRestControllerStyle(true);//配置restful的风格的驼峰命名
strategy.setControllerMappingHyphenStyle(true);//url多字段的/变成_下划线
mpg.setStrategy(strategy);//把所有策略放入自动代码生成器中
mpg.execute();//执行
}
}

配置文件

# 应用名称
spring.application.name=mybatis_plus
# 应用服务 WEB 访问端口
server.port=8080 **加不加都行,后面这些是使用生成文件对数据库操作的**
#配置数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=root
# mybatis_plus配置
# 当为true时:mybatis-plus会将Java对象的驼峰式命名的常量转成下划线的方式,再与数据库表列字段进行匹配,这样会造成错误。 此时需要利用@TableField注解指定常量在表中的列名。
# 当为false时:此时就需要数据库里每列都是下划线的命名方式。
mybatis-plus.configuration.map-underscore-to-camel-case=true
# autoMappingBehavior有三个属性(默认是PARTIAL)
# NONE:取消自动映射
# PARTIAL:只会自动映射,没有定义嵌套结果集映射的结果集
# FULL:会自动映射任意复杂的结果集(无论是否嵌套)
# 自动映射的时候sql语句的结果集字段是不区分大小写的,所以映射的pojo成员变量也不需要区分大小写,都可以映射到
mybatis-plus.configuration.auto-mapping-behavior=full
# mybatis-plus输出日志配置
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# 配置mapper xml文件的路径(扫描mapper文件下的xml)因为导出的xml文件再 src/main/java/ 跟我们配置的扫描不匹配 所以把配置文件挪到,resources下
mybatis-plus.mapper-locations=classpath:xml/*.xml

springboot整合mybatis_plus经常遇见的错误

Invalid bound statement (not found)出现原因和解决方法
常见的错误如下:
1.mapper.xml中的namespace和实际的mapper文件位置不一致 2.mapper接口中的方法名和mapper.xml中的id标签不一致 3.可能没有构建到target中,如果不在可粘贴进去继续执行
添加以下代码到pom文件中,<build></build>中,这样就可以构建进去了 <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>

springboot中使用mybatis_plus逆向工程的更多相关文章

  1. SpringBoot+Mybatis+Maven+MySQL逆向工程实现增删改查

    SpringBoot+Mybatis+MySQL+MAVEN逆向工程实现增删改查 这两天简单学习了下SpringBoot,发现这玩意配置起来是真的方便,相比于SpringMVC+Spring的配置简直 ...

  2. SpringBoot中yaml配置对象

    转载请在页首注明作者与出处 一:前言 YAML可以代替传统的xx.properties文件,但是它支持声明map,数组,list,字符串,boolean值,数值,NULL,日期,基本满足开发过程中的所 ...

  3. 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧

    做WEB项目,一定都用过JSP这个大牌.Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的.当你从一个传统的Spring MVC项目转入一个Spring Boot ...

  4. springboot中swaggerUI的使用

    demo地址:demo-swagger-springboot springboot中swaggerUI的使用 1.pom文件中添加swagger依赖 2.从github项目中下载swaggerUI 然 ...

  5. spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架

    前言: 本项目基于maven构建,使用mybatis-spring-boot作为spring-boot项目的持久层框架 spring-boot中使用mybatis持久层框架与原spring项目使用方式 ...

  6. 由浅入深学习springboot中使用redis

    很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...

  7. Springboot中使用AOP统一处理Web请求日志

    title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...

  8. SpringBoot 中常用注解

    本篇博文将介绍几种SpringBoot 中常用注解 其中,各注解的作用为: @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注 ...

  9. SpringBoot中关于Mybatis使用的三个问题

    SpringBoot中关于Mybatis使用的三个问题 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8495453.html 原本是要讲讲PostgreSQL ...

  10. 在SpringBoot中配置aop

    前言 aop作为spring的一个强大的功能经常被使用,aop的应用场景有很多,但是实际的应用还是需要根据实际的业务来进行实现.这里就以打印日志作为例子,在SpringBoot中配置aop 已经加入我 ...

随机推荐

  1. Homework4

    书籍链接:https://www.ituring.com.cn/article/13466(why Software Development Methodologies Suck?) 问:读 why ...

  2. immutable 与 stable 函数的差异

    Stable 函数不能修改数据库,单个Query中所有行给定同样的参数确保返回相同的结果.这种稳定级别允许优化器将多次函数调用转换为一次.在索引扫描的条件中使用这种函数是可行的,因为索引扫描只计算一次 ...

  3. Docker安装Openresty总结

    1. 启动Docker systemctl start docker 2. 查询有没有openresty镜像 docker search openresty -s 30 -s 30 stars数大于3 ...

  4. paddleocr安装与图片识别快速开始

    本文首发我的个人博客:paddleocr安装教程快速开始 1. 安装Python环境 wget https://mirrors.huaweicloud.com/python/3.8.5/Python- ...

  5. Django 运行报异常:AttributeError: 'str' object has no attribute 'get'

    Technorati Tags: Python,Django,Web 在使用django.contrib.auth用户机制进行用户的验证.登录.注销操作时,遇到这个异常. 首先是写了一个登录的视图,要 ...

  6. Grafana 入门知识介绍

    通过[Configuration]>[Plugins]添加插件 通过[Configuration]>[Data Sources]添加数据源(分析对象) 通过[Server Admin]&g ...

  7. python中限定导入的子模块

    如果包定义文件__init__.py中存在一个叫做__all__的列表变量,那么在使用from package import *的时候就把这个列表中的所有名字作为要导入的模块名. 例如在example ...

  8. 利用python对websocket进行并发压测

    简述 产品经理鉴于运营反馈并对程序的websocket长连接保持怀疑的态度,让我对websocket服务器进行压力测试,我内心是拒绝的. 开发思路 查阅websocket的相关资料,查到python的 ...

  9. PAT (Basic Level) Practice 1020 月饼 分数 25

    月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部分库存.样 ...

  10. FEX-EMU Wine踩坑记录

    FEX是一个用于在ARM64平台运行X86软件的工具,比较成熟,但是网上资料很少,所以就写了这篇FEX运行Wine踩坑记录. Termux的Fex不能用(2022年5月) 要在debian系统安装fe ...