1、引入依赖官网地址:代码生成器(新) | MyBatis-Plus (baomidou.com)
在pom.xml中引入
2、快速生成com.baomidou mybatis-plus-generator 3.5.1 org.freemarker freemarker 2.3.31
创建一个测试类,路径为src/test/java/com/mybatisplus_demo/FastAutoGeneratorTest.java
package com.mybatisplus_demo;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.Collections;
public class FastAutoGeneratorTest {
public static void main(String[] args) {
FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/mybatis_plus?characterEncoding=utf-8&userSSL=false", "root", "123456")
.globalConfig(builder -> {
builder.author("aiw") // 设置作者
//.enableSwagger() // 开启 swagger 模式
.fileOverride() // 覆盖已生成文件
.outputDir("E://IdeaProjects//MyBatis-Plus//mybatis_plus"); // 指定输出目录
})
.packageConfig(builder -> {
builder.parent("com.aiw") // 设置父包名
.moduleName("mybatisplus") // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "E://IdeaProjects//MyBatis-Plus//mybatis_plus"));// 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder.addInclude("t_user") // 设置需要生成的表名
.addTablePrefix("t_", "c_"); // 设置过滤表前缀
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}
3、运行结果
4、查看
运行完成后,自动打开输出目录:
将生成的项目使用IDEA打开,项目结构如下:
依次查看文件如下:
package com.aiw.mybatisplus.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
@Controller
@RequestMapping("/mybatisplus/user")
public class UserController {
}
User.java
package com.aiw.mybatisplus.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
@TableName("t_user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
private Integer sex;
private Integer isDeleted;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Integer getIsDeleted() {
return isDeleted;
}
public void setIsDeleted(Integer isDeleted) {
this.isDeleted = isDeleted;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name=" + name +
", age=" + age +
", email=" + email +
", sex=" + sex +
", isDeleted=" + isDeleted +
"}";
}
}
UserMapper.java
package com.aiw.mybatisplus.mapper; import com.aiw.mybatisplus.entity.User; import com.baomidou.mybatisplus.core.mapper.BaseMapper; public interface UserMapper extends BaseMapperUserServiceImpl.java{ }
package com.aiw.mybatisplus.service.impl; import com.aiw.mybatisplus.entity.User; import com.aiw.mybatisplus.mapper.UserMapper; import com.aiw.mybatisplus.service.IUserService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; @Service public class UserServiceImpl extends ServiceImplIUserService.javaimplements IUserService { }
package com.aiw.mybatisplus.service; import com.aiw.mybatisplus.entity.User; import com.baomidou.mybatisplus.extension.service.IService; public interface IUserService extends IServiceUserMapper.xml{ }
在IDEA中打开项目,仔细观察可以发现大部分代码爆红,一般来说将生成的代码拷贝到Maven项目中,然后导入相关依赖即可



