一、首先是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

mybatis-plus 学习笔记的更多相关文章

  1. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL 如果使用JDBC或者其他框架,很多时候需要你根据需求手动拼装SQL语句,这是一件非常麻烦的事情.MyBatis提供了对SQL语句动态的组装能力,而且他只有 ...

  2. mybatis的学习笔记

    前几天学习了mybatis,今天来复习一下它的内容. mybatis是一个基于Java的持久层框架,那就涉及到数据库的操作.首先来提出第一个问题:java有jdbc连接数据库,我们为什么还要使用框架呢 ...

  3. 关于mybatis的学习笔记

    配置文件 贴出mybatis的配置文件,这里mybatis还未与spring做整合: <?xml version="1.0" encoding="UTF-8&quo ...

  4. MyBatis 3学习笔记

    MyBatis 3 一.MyBatis简介 优秀的持久层框架,支持支持自定义 SQL.存储过程以及高级映射,专注于SQL的编写. ​ 为什么不使用工具类进行数据库操作: ​ 功能简单,sql语句编写在 ...

  5. mybatis缓存学习笔记

    mybatis有两级缓存机制,一级缓存默认开启,可以在手动关闭:二级缓存默认关闭,可以手动开启.一级缓存为线程内缓存,二级缓存为线程间缓存. 一提缓存,必是查询.缓存的作用就是查询快.写操作只能使得缓 ...

  6. 3、MyBatis.Net学习笔记之增删改

    增删改之前先说一下笔记1里提到的一个无法创建ISqlMapper对象的问题. <resultMaps> <resultMap id="FullResultMap" ...

  7. MyBatis基础学习笔记--摘录

    1.MyBatis是什么? MyBatis源自于IBatis,是一个持久层框架,封装了jdbc操作数据库的过程,使得开发者只用关心sql语句,无需关心驱动加载.连接,创建statement,手动设置参 ...

  8. MyBatis基础学习笔记--自总结

    一.MyBatis和jdbc的区别 jdbc的过程包括: 1.加载数据库驱动. 2.建立数据库连接. 3.编写sql语句. 4.获取Statement:(Statement.PrepareStatem ...

  9. Mybatis进阶学习笔记——动态代理方式开发Dao接口、Dao层(推荐第二种)

    1.原始方法开发Dao Dao接口 package cn.sm1234.dao; import java.util.List; import cn.sm1234.domain.Customer; pu ...

  10. MyBatis入门学习笔记(一)

    一.什么是MyBatis? Mybatis是一种“半自动化”的ORM实现,支持定制化 SQL.存储过程以及高级映射. 二.hibernate和mybatis对比 共同:采用ORM思想解决了实体和数据库 ...

随机推荐

  1. Windows环境下redis 配置文件中设置的密码无效

    当我们安装了redis服务后,发现在其配置文件redis.windows.conf(或redis.conf)设置了密码:requirepass ****** 但是打开redis-cli.exe后输入命 ...

  2. vim学习相关链接

    1:http://blog.csdn.net/niushuai666/article/details/7275406 2:http://ju.outofmemory.cn/entry/79671 3. ...

  3. Asp.net MVC 实现JSONP

    JSONP可以帮我们解决跨域访问的问题.JSONP is JSON With Padding. 这里我们将不再解释其原理.我们来看在ASP.NET MVC 3 如何实现.首先我们需要定义一个Jsonp ...

  4. 【Python学习】Thread笔记(1)

    Python学习笔记 - Thread(1) 标签(空格分隔): python from threading import Thread num = 2000 id_list = [] def do_ ...

  5. BZOJ4036:按位或 (min_max容斥&高维前缀和)

    Description 刚开始你有一个数字0,每一秒钟你会随机选择一个[0,2^n-1]的数字,与你手上的数字进行或(c++,c的|,pascal 的or)操作.选择数字i的概率是p[i].保证0&l ...

  6. java运行jar命令提示没有主清单属性和找不到主类

    推荐一个java运行jar命令提示没有主清单属性的百度经验的链接:https://jingyan.baidu.com/article/db55b60990f6084ba30a2fb8.html jav ...

  7. BZOJ1407 NOI2002 Savage 【Exgcd】

    BZOJ1407 NOI2002 Savage Description Input 第1行为一个整数N(1<=N<=15),即野人的数目. 第2行到第N+1每行为三个整数Ci, Pi, L ...

  8. BZOJ2595 Wc2008 游览计划 【斯坦纳树】【状压DP】*

    BZOJ2595 Wc2008 游览计划 Description Input 第一行有两个整数,N和 M,描述方块的数目. 接下来 N行, 每行有 M 个非负整数, 如果该整数为 0, 则该方块为一个 ...

  9. GitHub项目为己所用

    1.下载 2.cmd  进入文件夹 3.mvn clean package 4.mvn install

  10. python(八):反射

    反射机制是通过python3内置的hasattr.getattr.setattr来实现的.即根据变量名的字符串形式来获取变量名的属性或方法. 一.通过反射查看已知对象的属性和方法 getattr(ob ...