前言
本篇文章主要介绍的是SpringBoot整合mybatis-plus,实现增删改查。
GitHub源码链接位于文章底部。
建库建表
创建springboot数据库,创建t_user表,字段id主键自增,name,age。
工程结构
添加依赖
新建一个maven项目,在pom文件中添加以下依赖
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
mysql
mysql-connector-java
5.1.39
com.baomidou
mybatisplus-spring-boot-starter
1.0.5
com.baomidou
mybatis-plus
2.3
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1
org.projectlombok
lombok
1.16.20
org.apache.velocity
velocity-engine-core
2.0
src/main/java
**/*.xml
true
yml配置文件
server:
port: 8080
spring:
datasource:
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8
mybatis-plus:
mapper-locations: classpath:com/lxg/springboot/dao/mybatisplus/mapper/xml/*Mapper.xml
typeAliasesPackage: com.lxg.springboot.dao.mybatisplus
global-config:
#主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID,"该类型为未设置主键类型,5, "字符串全局唯一ID"";
id-type: 0
db-column-underline: true
refresh-mapper: true
#configuration:
#这个配置会将执行的sql打印出来,在开发或测试的时候可以用
#log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
主程序入口,启动类:
@SpringBootApplication
//配置mapper文件位置
@MapperScan("com.lxg.springboot.dao.mybatisplus.mapper")
public class MybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusApplication.class, args);
}
}
代码生成器
public class CodeGenerator{
/**
* @param args
* @Title: main
* @Description: 根据数据库生成表格
*/
public static void main(String[] args) {
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
//代码存放地址
gc.setOutputDir( "D://code4" );
gc.setFileOverride( true );
// 不需要ActiveRecord 特性的请改为false
gc.setActiveRecord( false );
// XML 二级缓存
gc.setEnableCache( false );
// XML ResultMap
gc.setBaseResultMap( true );
// XML columList
gc.setBaseColumnList( false );
// 作者