关于mybatisplus的功能与用法在官网里解释的很清楚:https://mp.baomidou.com/guide/
首先我们要引入相关的maven依赖包
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
com.example
train
0.0.1-SNAPSHOT
train
train project for Spring Boot
1.8
3.0.3
1.1.10
1.18.2
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-web-services
org.springframework.boot
spring-boot-starter-test
test
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.1
com.baomidou
mybatis-plus-boot-starter
${mybatis-plus.version}
com.alibaba
druid
${pool.druid.version}
mysql
mysql-connector-java
runtime
org.apache.velocity
velocity
1.7
org.projectlombok
lombok
${lombok.version}
true
provided
org.springframework.boot
spring-boot-maven-plugin
然后创建一个Generator类(里边有一些配置信息,需要根据实际情况修改),运行它就好了
package com.goldencis.train.common;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Yangcq on 2018/08/16.
*/
public class Generator {
//项目路径
private static String canonicalPath = "";
//基本包名
private static String basePackage = "com.goldencis.train";
//要生成的表名
private static String[] tables = {"t_student"};
//table前缀
private static String prefix = "t_";
//数据库类型
private static DbType dbType = DbType.MYSQL;
//数据库配置四要素
private static String driverName = "com.mysql.cj.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/train?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC";
private static String username = "root";
private static String password = "123456789";
public static void main(String[] args) {
AutoGenerator gen = new AutoGenerator();
/**
* 获取项目路径
*/
try {
canonicalPath = new File("").getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
/**
* 数据库配置
*/
gen.setDataSource(new DataSourceConfig()
.setDbType(dbType)
.setDriverName(driverName)
.setUrl(url)
.setUsername(username)
.setPassword(password)
.setTypeConvert(new MySqlTypeConvert() {
// 自定义数据库表字段类型转换【可选】
//@Override
//public DbColumnType processTypeConvert(String fieldType) {
//System.out.println("转换类型:" + fieldType);
// if ( fieldType.toLowerCase().contains( "tinyint" ) ) {
// return DbColumnType.BOOLEAN;
// }
//return super.processTypeConvert(fieldType);
//}
}));
/**
* 全局配置
*/
gen.setGlobalConfig(new GlobalConfig()
.setOutputDir( canonicalPath + "/src/main/java")//输出目录
.setFileOverride(true)// 是否覆盖文件
.setActiveRecord(true)// 开启 activeRecord 模式
.setEnableCache(false)// XML 二级缓存
.setBaseResultMap(true)// XML ResultMap
.setBaseColumnList(true)// XML columList
.setOpen(false)//生成后打开文件夹
// 自定义文件命名,注意 %s 会自动填充表实体属性!
.setMapperName("%sMapper")
.setXmlName("%sMapper")
.setServiceName("I%sService")
.setServiceImplName("%sServiceImpl")
.setControllerName("%sController")
);
/**
* 策略配置
*/
gen.setStrategy(new StrategyConfig()
// .setCapitalMode(true)// 全局大写命名
//.setDbColumnUnderline(true)//全局下划线命名
.setTablePrefix(new String[]{prefix})// 此处可以修改为您的表前缀
.setNaming(NamingStrategy.underline_to_camel)// 表名生成策略
.setInclude(tables) // 需要生成的表
.setRestControllerStyle(true)
//.setExclude(new String[]{"test"}) // 排除生成的表
// 自定义实体父类
// .setSuperEntityClass("com.baomidou.demo.TestEntity")
// 自定义实体,公共字段
//.setSuperEntityColumns(new String[]{"test_id"})
//.setTableFillList(tableFillList)
// 自定义 mapper 父类 默认BaseMapper
//.setSuperMapperClass("com.baomidou.mybatisplus.mapper.BaseMapper")
// 自定义 service 父类 默认IService
// .setSuperServiceClass("com.baomidou.demo.TestService")
// 自定义 service 实现类父类 默认ServiceImpl
// .setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl")
// 自定义 controller 父类
//.setSuperControllerClass("com.kichun."+packageName+".controller.AbstractController")
// 【实体】是否生成字段常量(默认 false)
// public static final String ID = "test_id";
// .setEntityColumnConstant(true)
// 【实体】是否为构建者模型(默认 false)
// public User setName(String name) {this.name = name; return this;}
// .setEntityBuilderModel(true)
// 【实体】是否为lombok模型(默认 false)document
.setEntityLombokModel(true)
// Boolean类型字段是否移除is前缀处理
// .setEntityBooleanColumnRemoveIsPrefix(true)
// .setRestControllerStyle(true)
// .setControllerMappingHyphenStyle(true)
);
/**
* 包配置
*/
gen.setPackageInfo(new PackageConfig()
//.setModuleName("User")
.setParent(basePackage)// 自定义包路径
.setController("controller")// 这里是控制器包名,默认 web
.setEntity("entity") // 设置Entity包名,默认entity
.setMapper("mapper") // 设置Mapper包名,默认mapper
.setService("service") // 设置Service包名,默认service
.setServiceImpl("service.impl") // 设置Service Impl包名,默认service.impl
.setXml("mapper") // 设置Mapper XML包名,默认mapper.xml
);
/**
* 注入自定义配置
*/
// 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
InjectionConfig abc = new InjectionConfig() {
@Override
public void initMap() {
Map map = new HashMap();
map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
this.setMap(map);
}
};
//自定义文件输出位置(非必须)
List fileOutList = new ArrayList();
fileOutList.add(new FileOutConfig("/templates/mapper.xml.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return canonicalPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper.xml";
}
});
abc.setFileOutConfigList(fileOutList);
gen.setCfg(abc);
/**
* 指定模板引擎 默认是VelocityTemplateEngine ,需要引入相关引擎依赖
*/
//gen.setTemplateEngine(new FreemarkerTemplateEngine());
/**
* 模板配置
*/
gen.setTemplate(
// 关闭默认 xml 生成,调整生成 至 根目录
new TemplateConfig().setXml(null)
// 自定义模板配置,模板可以参考源码 /mybatis-plus/src/main/resources/template 使用 copy
// 至您项目 src/main/resources/template 目录下,模板名称也可自定义如下配置:
// .setController("...");
// .setEntity("...");
// .setMapper("...");
// .setXml("...");
// .setService("...");
// .setServiceImpl("...");
);
// 执行生成
gen.execute();
}
}
mybatisplus自带的分页插件只能用于单表
创建配置类
/**
* @Author:
*/
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor(){
PaginationInterceptor page = new PaginationInterceptor();
page.setDialectType("mysql");
return page;
}
}
IStudentService.java
public interface IStudentService extends IService {
/**
*
* 查询所有学生,分页显示
* 注意!!: 如果入参是有多个,需要加注解指定参数名才能在xml中取值
*
*
* @return 分页对象
*/
IPage getAllStudent(int currentPage,int size);
}
StudentServiceImpl.java
@Service
public class StudentServiceImpl extends ServiceImpl implements IStudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public IPage getAllStudent(int currentPage,int size) {
IPage ipage = new Page<>();
ipage.setCurrent(currentPage);
ipage.setSize(size);
studentMapper.selectMapsPage(ipage, new QueryWrapper().eq("class_id","3"));
return ipage;
}
}
StudentController.java
@RestController
@RequestMapping("/student")
@Api(tags="学生控制器")
public class StudentController {
@Autowired
IStudentService iStudentService;
@RequestMapping("/getAllStudent")
@ApiOperation("获取全部学生信息")
public IPage getAllStudent(){
IPage ipage = iStudentService.getAllStudent(2,2);
System.out.println("总条数 ------> " + ipage.getTotal());
System.out.println("当前页数 ------> " + ipage.getCurrent());
System.out.println("当前每页显示数 ------> " + ipage.getSize());
System.out.println("获取的内容-----》"+ipage.getRecords());
System.out.println("总页数-----》"+ipage.getPages());
return ipage;
}
}
调用这个接口就可以在控制台看到测试的内容了。
条件构造器(这篇博客讲解的很好):
https://blog.csdn.net/m0_37034294/article/details/82917234