创建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. smile——Java机器学习引擎

    资源 https://haifengl.github.io/ https://github.com/haifengl/smile 介绍 Smile(统计机器智能和学习引擎)是一个基于Java和Scal ...

  2. 【设计模式】Java设计模式 - 单例模式

    [设计模式]Java设计模式 - 单例模式 不断学习才是王道 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 分享学习心得,欢迎指正,大家一起学习成长! 原创作品,更多关注我CSDN: ...

  3. KingbaseES V8R6单实例外部备份故障案例

    案例说明: 在KingbaseES V8R6单实例环境,配置外部备份服务器使用sys_backup.sh物理备份时,出现以下"WAL segment xxx was not archived ...

  4. KingbaseES 全局索引是否因为DDL操作而变为Unusable ?

    前言 Oracle 在对分区做DDL操作时,会使分区全局索引失效,需要加上关键字update global indexes.KingbaseES 同样支持全局索引.那么,如果对分区表进行DDL操作,那 ...

  5. Java学习笔记:基本输入、输出数据操作实例分析

    Java学习笔记:基本输入.输出数据操作.分享给大家供大家参考,具体如下: 相关内容: 输出数据: print println printf 输入数据: Scanner 输出数据: JAVA中在屏幕中 ...

  6. 华南理工大学 Python第7章课后小测-1

    1.(单选)以下程序对字典进行排序,按字典键值从小到大排序,空白处的代码是(  ): dt={'b':6, 'c':2, 'a':4} s=sorted(dt.items(),key=_____) p ...

  7. 如何在Windows中批量创建VMware的虚拟机

    在最近的工作中,需要创建一批类似的机器.在VMware中创建了模板,然后根据自义向导部署之后,发现可以快速的完成新vm的部署.系统中的计算机名,IP地址都可以自动的完成更新.唯一的缺点是,系统自带的向 ...

  8. 累加和为 K 的最长子数组问题

    累加和为 K 的最长子数组问题 作者:Grey 原文地址: 博客园:累加和为 K 的最长子数组问题 CSDN:累加和为 K 的最长子数组问题 题目描述 给定一个整数组成的无序数组 arr,值可能正.可 ...

  9. phpoffice文档笔记

    目录 phpword html转word phpexcel 从数据库导出 phpword html转word <?php namespace app\index\controller; use ...

  10. C# 中的那些锁,在内核态都是怎么保证同步的?

    一:背景 1. 讲故事 其实这个问题是前段时间有位朋友咨询我的,由于问题说的比较泛,不便作答,但想想梳理一下还是能回答一些的,这篇就来聊一聊下面这几个锁. Interlocked AutoResetE ...