springboot+mybatis-plus 根据表自动生成crud
时间: 2023-08-09 15:02:25 浏览: 225
springboot+mybatisplus自动建表案例
5星 · 资源好评率100%
Spring Boot整合MyBatis-Plus可以通过使用代码生成器来自动生成CRUD代码。下面是使用步骤:
1. 首先,在pom.xml文件中添加MyBatis-Plus和相关依赖:
```xml
<!-- MyBatis-Plus依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>最新版本号</version>
</dependency>
<!-- MyBatis-Plus代码生成器依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>最新版本号</version>
</dependency>
```
2. 创建一个配置类来配置代码生成器:
```java
@Configuration
public class MyBatisPlusConfig {
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Value("${mybatis-plus.package}")
private String packageName;
@Value("${mybatis-plus.author}")
private String author;
@Value("${mybatis-plus.table-prefix}")
private String tablePrefix;
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClassName);
return dataSource;
}
@Bean
public GlobalConfig globalConfig() {
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setAuthor(author);
globalConfig.setOpen(false);
return globalConfig;
}
@Bean
public DataSourceConfig dataSourceConfig() {
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL);
dataSourceConfig.setDriverName(driverClassName);
dataSourceConfig.setUsername(username);
dataSourceConfig.setPassword(password);
dataSourceConfig.setUrl(url);
return dataSourceConfig;
}
@Bean
public PackageConfig packageConfig() {
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent(packageName);
packageConfig.setEntity("entity");
packageConfig.setMapper("mapper");
packageConfig.setService("service");
packageConfig.setServiceImpl("service.impl");
packageConfig.setController("controller");
return packageConfig;
}
@Bean
public StrategyConfig strategyConfig() {
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setEntityColumnConstant(true);
strategyConfig.setEntityLombokModel(true);
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setInclude("表名1", "表名2"); // 生成指定的表
strategyConfig.setRestControllerStyle(true);
strategyConfig.setTablePrefix(tablePrefix); // 设置表前缀
return strategyConfig;
}
@Bean
public TemplateConfig templateConfig() {
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setController(null); // 不生成controller层
return templateConfig;
}
@Bean
public InjectionConfig injectionConfig() {
Map<String, Object> map = new HashMap<>();
return new InjectionConfig() {
@Override
public void initMap() {
this.setMap(map);
}
};
}
@Bean
public AutoGenerator autoGenerator() {
AutoGenerator autoGenerator = new AutoGenerator();
autoGenerator.setGlobalConfig(globalConfig());
autoGenerator.setDataSource(dataSourceConfig());
autoGenerator.setPackageInfo(packageConfig());
autoGenerator.setStrategy(strategyConfig());
autoGenerator.setTemplate(templateConfig());
autoGenerator.setCfg(injectionConfig());
return autoGenerator;
}
}
```
3. 在application.properties或application.yml文件中配置相关信息:
```properties
# 数据源配置
spring.datasource.url=数据库URL
spring.datasource.username=数据库用户名
spring.datasource.password=数据库密码
spring.datasource.driver-class-name=数据库驱动类名
# MyBatis-Plus配置
mybatis-plus.package=你的包路径
mybatis-plus.author=代码作者名
mybatis-plus.table-prefix=表前缀
```
4. 创建一个启动类,添加@SpringBootApplication和@EnableAutoConfiguration注解,并在main方法中调用代码生成器生成代码:
```java
@SpringBootApplication
@EnableAutoConfiguration
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
// 调用代码生成器生成代码
ApplicationContext context = SpringApplication.run(MyApplication.class, args);
AutoGenerator autoGenerator = context.getBean(AutoGenerator.class);
autoGenerator.execute();
}
}
```
5. 运行启动类,执行代码生成器。生成的代码将包含实体类、Mapper接口、Service接口、Service实现类和Controller类。
这样就能根据数据库表自动生成CRUD代码了。如果需要生成其他的代码,可以根据需要配置生成器相应的参数和模板。
阅读全文