导入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
添加CodeGenerator 工具类
public class CodeGenerator {
public static void main(String[] args) {
generate();
}
private static void generate() {
FastAutoGenerator.create("url", "root", "password")//数据库地址、账号、密码
.globalConfig(builder -> {
builder.author("Pzzit") // 设置作者
.enableSwagger() // 开启 swagger 模式
.fileOverride() // 覆盖已生成文件
.outputDir("D:\\design\\car-java\\car-java\\src\\main\\java\\"); // 指定输出目录
})
.packageConfig(builder -> {
builder.parent("com.car") // 设置父包名
.moduleName(null) // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D:\\design\\car-java\\car-java\\src\\main\\resources\\mapper\\")); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder.entityBuilder().enableLombok();
// builder.mapperBuilder().enableMapperAnnotation().build();
builder.controllerBuilder().enableHyphenStyle() // 开启驼峰转连字符
.enableRestStyle(); // 开启生成@RestController 控制器
builder.addInclude("user"); // 设置需要生成的表名
// .addTablePrefix("t_", "sys_"); // 设置过滤表前缀,若数据库无xx_前缀可不要
})
.execute();
}
}
自定义controller,service,serviceImpl,实现增删查改
在resources文件夹下创建templates文件夹
然后可以在外部库里面找到如下目录,复制controller.java.vm,service.java.vm,serviceImpl.java.vm这几个文件,也可以直接在templates文件夹下创建这几个文件。
controller.java.vm
package ${package.Controller};
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.metadata.IPage;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
#if(${restControllerStyle})
import org.springframework.web.bind.annotation.RestController;
#else
import org.springframework.stereotype.Controller;
#end
#if(${superControllerClassPackage})
import ${superControllerClassPackage};
#end
/**
* <p>
* $!{table.comment} 前端控制器
* </p>
*
* @author ${author}
* @since ${date}
*/
#if(${restControllerStyle})
@RestController
#else
@Controller
#end
@RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${table.entityPath}#end")
#if(${kotlin})
class ${table.controllerName}#if(${superControllerClass}) : ${superControllerClass}()#end
#else
#if(${superControllerClass})
public class ${table.controllerName} extends ${superControllerClass} {
#else
public class ${table.controllerName} {
#end
@Resource
private ${table.serviceName} ${table.entityPath}Service;
// 新增或者更新
@PostMapping("/save")
public boolean save(@RequestBody ${entity} ${table.entityPath}) {
return ${table.entityPath}Service.saveOrUpdate(${table.entityPath});
}
@DeleteMapping("/delOne/{id}")
public boolean delete(@PathVariable Integer id) {
return ${table.entityPath}Service.removeById(id);
}
@PostMapping("/del/batch")
public boolean deleteBatch(@RequestBody List<Integer> ids) {
return ${table.entityPath}Service.removeByIds(ids);
}
@GetMapping("findAll")
public List<${entity}> findAll() {
return ${table.entityPath}Service.list();
}
@GetMapping("findOne/{id}")
public ${entity} findOne(@PathVariable Integer id) {
return ${table.entityPath}Service.getById(id);
}
@GetMapping("/findAllByPage")
public IPage<${entity}> findAllByPage(@RequestParam Integer pageNum,
@RequestParam Integer pageSize) {
return ${table.entityPath}Service.findAllByPage(pageNum, pageSize);
}
}
#end
ervice.java.vm
package ${package.Service};
import ${package.Entity}.${entity};
import ${superServiceClassPackage};
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RequestParam;
/**
* <p>
* $!{table.comment} 服务类
* </p>
*
* @author ${author}
* @since ${date}
*/
#if(${kotlin})
interface ${table.serviceName} : ${superServiceClass}<${entity}>
#else
public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {
public IPage<${entity}> findAllByPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize);
}
#end
serviceImpl.java.vm
package ${package.ServiceImpl};
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
/**
* <p>
* $!{table.comment} 服务实现类
* </p>
*
* @author ${author}
* @since ${date}
*/
@Service
#if(${kotlin})
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {
}
#else
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {
@Autowired
private ${table.mapperName} ${table.entityPath}Mapper;
@Override
public IPage<${entity}> findAllByPage(Integer pageNum, Integer pageSize) {
IPage<${entity}> page = new Page<>(pageNum,pageSize);
QueryWrapper<${entity}> userQueryWrapper = new QueryWrapper();
IPage<User> page1 = userMapper.selectPage(page, userQueryWrapper);
return page1;
}
}
#end
回到 CodeGenerator运行。
在自定义的分页查询中可以添加实体类的属性作为参数然后进行查询,如下:
userController
@GetMapping("/findAllByPage")
public IPage<User> findAllByPage(@RequestParam Integer pageNum,
@RequestParam Integer pageSize, @RequestParam(defaultValue = "") String userName,@RequestParam(defaultValue = "") String userRealname,
@RequestParam(defaultValue = "") String userPhone,@RequestParam(defaultValue = "") String userEmail,
@RequestParam(defaultValue = "") String userAddress) {
return userService.findAllByPage(pageNum, pageSize, userName,userRealname,userPhone,userEmail,userAddress);
}
IUserService
IPage<User> findAllByPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, String userName,String userRealname,String userPhone, String userEmail,String userAddress);
IUserServiceImpl
@Override
public IPage<User> findAllByPage(Integer pageNum, Integer pageSize,String userName,String userRealname,String userPhone, String userEmail,String userAddress) {
IPage<User> page = new Page<>(pageNum,pageSize);
QueryWrapper<User> userQueryWrapper = new QueryWrapper();
userQueryWrapper.like(!userName.isEmpty(), "userName", userName);
userQueryWrapper.like(!userRealname.isEmpty(), "userRealname", userRealname);
userQueryWrapper.like(!userPhone.isEmpty(), "userPhone", userPhone);
userQueryWrapper.like(!userEmail.isEmpty(), "userEmail", userEmail);
userQueryWrapper.like(!userAddress.isEmpty(), "userAddress", userAddress);
userQueryWrapper.orderByDesc("userId");
IPage<User> page1 = userMapper.selectPage(page, userQueryWrapper);
return page1;
}
最后就可以在前端显示啦。