一.SpringBoot使用mybatis-plus+自动代码生成

使用mybatis-plus可以自动帮我们生成通用的 controller,service,dao,mapper

二.加入依赖

          <!-- mybatisplus与springboot整合 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.5</version>
</dependency>
<!-- MP 核心库 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.1.8</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 模板引擎 代码生成 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>

  

三.在application.properties 配置

# 配置mybatis-plus
# 配置扫描xml
mybatis-plus.mapper-locations=classpath:mapper/*.xml
# 实体扫描,多个package用逗号或者分号分隔
mybatis-plus.type-aliases-package=com.example.demo.model #逻辑删除配置
mybatis-plus.global-config.sql-injector=com.baomidou.mybatisplus.mapper.LogicSqlInjector
#配置逻辑删除字段为1是删除
mybatis-plus.global-config.logic-delete-value=1
#配置逻辑删除字段为0是未删除
mybatis-plus.global-config.logic-not-delete-value=0

  

四.在test包下新建

CodeGeneration.java
package com.example.demo;

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.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; /**
*
* @ClassName: CodeGeneration
* @Description: 代码生成器
* @author yux
* @date 2018年1月25日 下午2:55:14
*/
public class CodeGeneration { /**
*
* @Title: main
* @Description: 生成
* @param args
*/
public static void main(String[] args) {
AutoGenerator mpg = new AutoGenerator(); // 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir("D://");  //输出文件路径
gc.setFileOverride(true);
gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
gc.setAuthor("yux");// 作者 // 自定义文件命名,注意 %s 会自动填充表实体属性!
gc.setControllerName("%sController");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
mpg.setGlobalConfig(gc); // 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
dsc.setUrl("jdbc:mysql://127.0.0.1:3306/springbootdemo");
mpg.setDataSource(dsc); // 策略配置
StrategyConfig strategy = new StrategyConfig();
// strategy.setTablePrefix(new String[] { "sys_" });// 此处可以修改为您的表前缀
strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
strategy.setInclude(new String[] { "user_info" }); // 需要生成的表 strategy.setSuperServiceClass(null);
strategy.setSuperServiceImplClass(null);
strategy.setSuperMapperClass(null); mpg.setStrategy(strategy); // 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.example.demo");
pc.setController("controller");
pc.setService("service");
pc.setServiceImpl("service.impl");
pc.setMapper("dao");
pc.setEntity("model");
pc.setXml("xml");
mpg.setPackageInfo(pc); // 执行生成
mpg.execute(); } }

  右击run CodeGeneration main();生成文件,然后放到你的项目中。

五.在启动主类上加上扫描dao层的包

@MapperScan("com.example.demo.dao")

六.注意

使用mybatis-plus+自动代码生成时候必需把第一章讲的MyBatisConfigurer.java的类删掉,否则报错!!!!!

(八)SpringBoot使用mybatis-plus+自动代码生成的更多相关文章

  1. SpringBoot 添加mybatis generator 自动生成代码插件

    自动生成数据层代码,提高开发效率 1.pom添加插件,并指定配置文件路径 <!-- mybatis generator 自动生成代码插件 --> <plugin> <gr ...

  2. SpringBoot中mybatis的自动生成

    1.在pom文件中加入自动生成的插件 <!-- mybatis generator 自动生成代码插件 --> <plugin> <groupId>org.mybat ...

  3. SpringBoot中mybatis配置自动转换驼峰标识没有生效

    mybatis提供了一个配置: #开启驼峰命名转换 mybatis.configuration.map-underscore-to-camel-case=true 使用该配置可以让mybatis自动将 ...

  4. springboot和mybatis集成,自动生成model、mapper,增加mybatis分页功能

    整体思路和http://www.cnblogs.com/mahuan2/p/5859921.html相同. 主要讲maven的pom.xml和一些配置变化,详细说明. 软件简介 Spring是一个流行 ...

  5. MyBatis学习总结_15_定制Mybatis自动代码生成的maven插件

    ==================================================================================================== ...

  6. Springboot 系列(十一)使用 Mybatis(自动生成插件) 访问数据库

    1. Springboot mybatis 介绍 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数获取 ...

  7. springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)

    这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...

  8. Springboot mybatis generate 自动生成实体类和Mapper

    https://github.com/JasmineQian/SpringDemo_2019/tree/master/mybatis Springboot让java开发变得方便,Springboot中 ...

  9. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

随机推荐

  1. 服务化之Netty

    关于Netty的介绍可参考:https://www.zhihu.com/question/24322387 Netty官网为:http://netty.io/ Git:https://github.c ...

  2. 10.19-10.20 test

    2016 10.19-10.20 两天  题目by mzx Day1: T1:loverfinding 题解:hash #include<iostream> #include<cst ...

  3. bzoj4670: 佛罗里达

    这题直接随机化+贪心就可以爆踩过去,我加了个退火增加容错率而已....其实你随机的次数够多根本不需要... 然后来自肉丝哥哥的正经做法: 先钦定D(A)>D(B),那么可以枚举D(A),然后再去 ...

  4. Ansible 动态获取主机列表

    参考文献: http://www.linuxidc.com/Linux/2016-12/138111.htm 附加 这个 include_vars 变量,可以 动态分别环境或者其他条件- hosts: ...

  5. HTML: 简单的悬停效果

    1. [图片] 捕获.jpg ​2. [代码][CSS]代码     body {    background: #000;    overflow-y: scroll;  }  .items {  ...

  6. 生成chm格式帮助文档的步骤

    开场前,道具先得被齐全了. 道具:struts2的开源代码(以生成struts2的帮助文档为例).chm格式生成工具jd2chm.exe(网上有) 好了,准备演出 1.在eclipse中新建一个jav ...

  7. 浅谈如何删除JSP编译后的空行

    当你在客户端用view source看JSP生成的代码时,会发现有很多空行,他们是由< %...% >后的回车换行而生成的,也就是说每一行由< %...% >包含的JSP代码到 ...

  8. TensorFlow 图像预处理(一) 图像编解码,图像尺寸调整

    from: https://blog.csdn.net/chaipp0607/article/details/73029923 TensorFlow提供了几类图像处理函数,下面介绍图像的编码与解码,图 ...

  9. codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)

    题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  10. 【UVA12779占位】Largest Circle

    几何题,希望有时间回来解决掉.