mybatis-plus 学习笔记
一、首先是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 学习笔记的更多相关文章
- MyBatis:学习笔记(4)——动态SQL
MyBatis:学习笔记(4)——动态SQL 如果使用JDBC或者其他框架,很多时候需要你根据需求手动拼装SQL语句,这是一件非常麻烦的事情.MyBatis提供了对SQL语句动态的组装能力,而且他只有 ...
- mybatis的学习笔记
前几天学习了mybatis,今天来复习一下它的内容. mybatis是一个基于Java的持久层框架,那就涉及到数据库的操作.首先来提出第一个问题:java有jdbc连接数据库,我们为什么还要使用框架呢 ...
- 关于mybatis的学习笔记
配置文件 贴出mybatis的配置文件,这里mybatis还未与spring做整合: <?xml version="1.0" encoding="UTF-8&quo ...
- MyBatis 3学习笔记
MyBatis 3 一.MyBatis简介 优秀的持久层框架,支持支持自定义 SQL.存储过程以及高级映射,专注于SQL的编写. 为什么不使用工具类进行数据库操作: 功能简单,sql语句编写在 ...
- mybatis缓存学习笔记
mybatis有两级缓存机制,一级缓存默认开启,可以在手动关闭:二级缓存默认关闭,可以手动开启.一级缓存为线程内缓存,二级缓存为线程间缓存. 一提缓存,必是查询.缓存的作用就是查询快.写操作只能使得缓 ...
- 3、MyBatis.Net学习笔记之增删改
增删改之前先说一下笔记1里提到的一个无法创建ISqlMapper对象的问题. <resultMaps> <resultMap id="FullResultMap" ...
- MyBatis基础学习笔记--摘录
1.MyBatis是什么? MyBatis源自于IBatis,是一个持久层框架,封装了jdbc操作数据库的过程,使得开发者只用关心sql语句,无需关心驱动加载.连接,创建statement,手动设置参 ...
- MyBatis基础学习笔记--自总结
一.MyBatis和jdbc的区别 jdbc的过程包括: 1.加载数据库驱动. 2.建立数据库连接. 3.编写sql语句. 4.获取Statement:(Statement.PrepareStatem ...
- Mybatis进阶学习笔记——动态代理方式开发Dao接口、Dao层(推荐第二种)
1.原始方法开发Dao Dao接口 package cn.sm1234.dao; import java.util.List; import cn.sm1234.domain.Customer; pu ...
- MyBatis入门学习笔记(一)
一.什么是MyBatis? Mybatis是一种“半自动化”的ORM实现,支持定制化 SQL.存储过程以及高级映射. 二.hibernate和mybatis对比 共同:采用ORM思想解决了实体和数据库 ...
随机推荐
- keepererrorcode = connectionloss for 错误处理
自己的环境在虚拟机上,于是使用同事的环境调试问题,发现无法初始化成功,提示keepererrorcode = connectionloss for,于是上网查了下资料整理如下: 1.对比代码中引用的j ...
- 给SqlParameter参数指定或不指定:@变量标识符的区别是什么?
对于sql语句中一个@的参数@au_id,在sqlParameter中,可以用@au_id,也可以用au_id. 要是bt点的,用两个@,你就不能随便省略@了. 对于sql语句 : select * ...
- HTML, CSS. JS的各种奇葩bug
1. block包含inline-block的时候, inline-block 没有内容的时候,会根据 font 产生一个 line-height 来产生一个空白块. 解决方法: <div cl ...
- ffmpeg jpeg图片播放失败之问题排查
播放jpeg时,avformat_find_stream_info出现以下问题,排查: [jpeg_pipe @ 0x146a80] Could not find codec parameters f ...
- Java并发编程之重入锁
重入锁,顾名思义,就是支持重进入的锁,它表示该锁能够支持一个线程对资源的重复加锁.重进入是指任意线程在获取到锁之后能够再次获取该锁而不会被锁阻塞,该特性的实现需要解决以下两个问题. 1.线程再次获取锁 ...
- 特殊的 html 空格
http://www.zhangxinxu.com/wordpress/2015/01/tips-blank-character-chinese-align/
- oracle管道函数的用法
oracle管道函数是一类特殊的函数,oracle管道函数返回值类型必须为集合,下面将介绍oracle管道函数的语法. 在普通的函数中,使用dbms_output输出的信息,需要在服务器执行完整个函数 ...
- 如何看待 Kotlin 成为 Android 官方支持开发语言?
Google IO 2017宣布了 Kotlin 会成为 Android 官方开发语言.一时间朋友圈和Android圈被各种刷屏.当然我也顺势而为发布了一篇的文章<为什么我要改用Kotlin&g ...
- SQL基础四(例子)
------------------------------------------------ --分别创建student/course/score表 Create table student ( ...
- iOS调用第三方API/Framework
前言 老板不止一次地说过:这个世纪靠个人的能力去完成一件事情肯定是不够的.无论什么方面我们都可以找到许许多多的事例表明合作共赢的重要性,例如Linux的发展.建筑事务所的发展.乃至科学技术的发展等等. ...