文章目录
序
首先附上mybatis-plus官方文档
本篇参考官方文档记录spring mvc项目接入mybatis plus的全流程及一些问题的解决方案,建议优先参考官方文档
开始之前,假设数据库已建好并已能正常访问
依赖配置
此处使用maven
核心依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.1.0</version>
</dependency>
如之前已引入mybatis相关依赖,请去除,mybatis plus会自行管理<./font>
代码生成器相关依赖
AutoGenerator
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.1.0</version>
</dependency>
freemarker模版引擎
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
lombok
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
生成的entity包中若报错,须在idea中安装Lombok plugin插件
代码生成
代码生成器
根据官方代码改了个较为简洁方便适合懒人直接使用的生成器, 如需更多配置可查看官方生成器模版或参考文末附的全配置项自行配置
package com.hpe.utils;
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;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
/**
* @description: 代码生成器
* @author: zxj
* @date: 2019/3/3
**/
public class CodeGenerator {
public static void main(String[] args) {
// ================= 必须修改的配置 start =================
// 数据源配置
String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/hadoop?useSSL=true";
String jdbcDriver = "com.mysql.jdbc.Driver";
String jdbcUsername = "root";
String jdbcPassword = "root";
// 父级包名配置
String parentPackage = "com.hpe";
// 生成代码的 @author 值
String author = "zxj";
// 要生成代码的表名配置
String[] tables = {
"dir",
"file",
"recode",
"rela",
"resources"
};
// ================= 必须修改的配置 end =================
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor(author);
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
// 生成完毕后是否打开输出目录
gc.setOpen(false);
// 为true时生成entity将继承Model类,单类即可完成基于单表的业务逻辑操作,按需开启
gc.setActiveRecord(false);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(jdbcUrl);
dsc.setDriverName(jdbcDriver);
dsc.setUsername(jdbcUsername);
dsc.setPassword(jdbcPassword);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
// 父级包名,按需修改
pc.setParent(parentPackage);
// 设置模块名, 会在parent包下生成一个指定的模块包
pc.setModuleName(null);
mpg.setPackageInfo(pc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setRestControllerStyle(false);
strategy.setInclude(tables);
strategy.setSuperEntityColumns("id");
// Controller驼峰连字符,如开启,则requestMapping由 helloWorld 变为 hello-world 默认false
strategy.setControllerMappingHyphenStyle(false);
strategy.setTablePrefix(pc.getModuleName() + "_");
// 开启后将使用lombok注解代替set-get方法,false则生成set-get方法
strategy.setEntityLombokModel(true);
// 在实体类中移除is前缀
strategy.setEntityBooleanColumnRemoveIsPrefix(true);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
执行后会生成这些文件
可将生成的xml放到resources目录或者配置xml目录为resource目录方便扫描
但是,建议把生成的xml目录放入resource中。对于Maven项目,IntelliJ IDEA默认是不处理src/main/java中的非java文件的,不专门在pom.xml中配置是会报错的,参考。
配置
扫描mapper的接口
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="你的mapper目录"/>
</bean>
扫描mapper的xml文件
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="mapper xml的资源路径"/>
</bean>
mapper CRUD接口
如何使用?
mapper CRUD的原理
生成的mapper接口会继承BaseMapper接口,查看mapper CRUD接口支持的操作
service CRUD接口
如何使用?
生成的service接口会继承 IService接口,查看service CRUD接口支持的操作