MyBatis-Plus 自动生成
根据表自动生成实体类,mapper,Service,ServiceImpl,Controller
1 pom.xml导入如下配置
//这个依赖我们已经导过了如果你只是导入了开启mysql-plus的依赖这个也加上
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
2.我们需要在main方法下进行创建。我们创建一个Main类代码如下
package com.bdh.myatispulstest;
import com.baomidou.mybatisplus.annotation.DbType;
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;
public class Main {
public static void main(String[] args) {
//创建gennerator对象
AutoGenerator autoGenerator = new AutoGenerator();
//数据源
DataSourceConfig dataSourceConfig = new DataSourceConfig();
//是什么数据库就写对应的数据库
dataSourceConfig.setDbType(DbType.MYSQL);
dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/news?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC");
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("123456");
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
autoGenerator.setDataSource(dataSourceConfig);
//全局配置
GlobalConfig globalConfig = new GlobalConfig();
//设置自动生成的路径
globalConfig.setOutputDir(System.getProperty("user.dir")+"/src/main/java");
//关闭默认打开
globalConfig.setOpen(false);
//作者名
globalConfig.setAuthor("baodahai");
autoGenerator.setGlobalConfig(globalConfig);
//包信息
PackageConfig packageConfig = new PackageConfig();
//包的路径
packageConfig.setParent("com.bdh.myatispulstest");
//生成侧包
packageConfig.setModuleName("generator");
packageConfig.setController("controller");
packageConfig.setService("service");
packageConfig.setServiceImpl("service.impl");
packageConfig.setMapper("mapper");
autoGenerator.setPackageInfo(packageConfig);
//配置策略
StrategyConfig strategyConfig = new StrategyConfig();
//自动添加Lombok
strategyConfig.setEntityLombokModel(true);
//斜杠改为驼峰命名
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
//驼峰命名
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
autoGenerator.setStrategy(strategyConfig);
autoGenerator.execute();
}
}
如果我的这个你运行错误那就看其他大佬的一般这东西一大堆。如果你找到可以运行的最好把他收藏在自己的文件夹上
这个就是最简单的使用了
package com.bdh.myatispulstest.generator.controller;
import com.bdh.myatispulstest.generator.service.IUsersService;
import com.bdh.myatispulstest.generator.service.impl.UsersServiceImpl;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
/**
* <p>
* 前端控制器
* </p>
*
* @author baodahai
* @since 2021-10-06
*/
@Controller
@RequestMapping("/generator/users")
public class UsersController {
private IUsersService usersService;
@PostMapping("/index")
public ModelAndView index(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
modelAndView.addObject("list",usersService.list());
return modelAndView;
}
}