一、首先是POM

    <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.7.1</version>
</dependency>
<dependency><!--添加MySQL驱动依赖 -->
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency> <!--注意,MybatisPlus Generator默认使用Velocity,不添加此依赖会报错-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency> <!--注意,在Generator时需要,但是在运行Application时请将此注释掉-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.0.4</version>
</dependency> </dependencies>

未添加Velocity依赖会抛出:Caused by: java.lang.ClassNotFoundException: org.apache.velocity.context.Context

运行application,却未注释mybatis-plus-generator,会抛出:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [/Users/zhenghao/git/ampmybatisplus/target/classes/cn/jsfund/amp/mp/mapper/UserMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.lang.NoSuchMethodError: com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean.setConfiguration(Lcom/baomidou/mybatisplus/core/MybatisConfiguration;)V

二、代码生成

import com.baomidou.mybatisplus.annotation.IdType;
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; /**
* @Description:
* @Date 2019/2/21 22:05
*/
public class MPGenerator {
public static void main(String[] args) {
String projectPath = System.getProperty("user.dir");
GlobalConfig cfg = new GlobalConfig(); cfg.setActiveRecord(true).setAuthor("重八")
.setOutputDir(projectPath + "/src/main/java")
.setFileOverride(true)
.setIdType(IdType.AUTO)
.setServiceName("%sService")
.setBaseResultMap(true)
.setBaseColumnList(false); DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/db?useSSL=false");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123"); StrategyConfig stConfig = new StrategyConfig();
stConfig.setCapitalMode(true)
.setNaming(NamingStrategy.underline_to_camel)
.setRestControllerStyle(true)
.setEntityLombokModel(true)
.setInclude("user_info"); PackageConfig pkConfig = new PackageConfig();
pkConfig
.setParent("com.baomidou.xxx")
.setMapper("mapper")
.setService("service")
.setController("controller")
.setEntity("entity")
.setXml("xml"); AutoGenerator ag = new AutoGenerator();
ag.setGlobalConfig(cfg)
.setDataSource(dsc)
.setStrategy(stConfig)
.setPackageInfo(pkConfig); //6. 执行
ag.execute(); }
}

以上是mysql的生成,如果使用Oracle数据库的话,schema,table,colume全部需要大写,否则会抛异常;例如:

dsc.setSchemaName("SCOTT");

三、配置:

## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/db?useSSL=false
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver ## Mybatis-plus 配置 mybatis-plus.mapper-locations=classpath:xml/*.xml
mybatis-plus.typeAliasesPackage=classpath:xml/*.xml
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.cache-enabled=false

最新文章

  1. 多功能前台交互效果插件superSlide
  2. 初探JavaScript(二)——JS如何动态操控HTML
  3. 第十七课:js数据缓存系统的原理
  4. turing 项目引用
  5. Code Review的一些典型内容
  6. IOS game
  7. [LINQ]查询关键字
  8. 返璞归真 asp.net mvc (7) - asp.net mvc 3.0 新特性之 Controller
  9. MPLS VPN随堂笔记3
  10. 风趣的JavaScript面向对象入门课程一
  11. 乌龟棋dp
  12. postman的安装和使用
  13. day-01(html)
  14. JAVA追加写入文本文件
  15. Python学习笔记第二十周
  16. Netty 高性能之道 - Recycler 对象池的复用
  17. 合并果子(NOIP2004)
  18. python装饰器,其实就是对闭包的使用。
  19. spring-mvc----数据库数据到页面错误--tomcat启动不了
  20. Spring -- spring结合aop 进行 tx&amp;aspectj事务管理配置方法

热门文章

  1. recovery.conf文件详解
  2. c# 实体处理工具类
  3. python_安装第三方库
  4. ss-libev 源码解析udp篇 (1)
  5. PHP练习
  6. 【剑指offer】圆圈中最后剩下的数字(约瑟夫问题),C++实现
  7. Android 百度地图2.4.2版本标注动画效果
  8. stm32寄存器版学习笔记05 PWM
  9. BZOJ3688 折线统计【树状数组优化DP】
  10. BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 【LCT】【分块】