IDEA搭建springboot项目
最终项目结构展示
1.pom.xml配置文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.7.RELEASE
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
org.apache.tomcat.embed
tomcat-embed-jasper
8.5.31
org.projectlombok
lombok
org.springframework.boot
spring-boot-devtools
2.0.4.RELEASE
com.baomidou
mybatis-plus-boot-starter
3.0.1
org.apache.velocity
velocity-engine-core
2.0
org.freemarker
freemarker
2.3.23
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
false
${basedir}/src/main/java
**/*.xml
true
2.application.properties配置文件
#访问相关配置
server.port=8099
server.tomcat.uri-encoding=utf-8
#项目访问名称,如果不配置直接访问bean就可用
server.servlet.context-path=/demo
#数据库连接
spring.datasource.url = jdbc:mysql://localhost:3306/demo
spring.datasource.username = root
spring.datasource.password = 123
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#spring boot视图配置
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
logging.level.cn.piesat.mapper=debug
#设定ftl文件路径
spring.freemarker.template-loader-path=classpath:/templates
#设定静态文件路径,js,css等
spring.mvc.static-path-pattern=/static/**
#设置mapper文件的位置
mybatis-plus.mapper-locations: classpath*:com/example/demo/mapper/*.xml
mybatis-plus.type-aliases-package=cmo.example.demo.bean;cmo.example.demo.entity;
#mybatis.mapper-locations=classpath*:com/example/demo/mapper/*.xml
#mybatis.mapperLocations=classpath*:com/example/demo/mapper/*.xml
#mybatis.type-aliases-package=com.example.demo.bean
3.创建创建mybatis生成器类
包路径com.example.demo.generator
创建类----》参考官方文档mybatis-plus:https://mp.baomidou.com/guide
package com.example.demo.generator;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
*/
public class CodeGenerator {
/**
*
* 读取控制台内容
*
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("jobob");
gc.setOpen(false);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/myblog?useUnicode=true&useSSL=false&characterEncoding=utf8");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("myblog");
dsc.setPassword("myblog");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner("模块名"));
pc.setParent("com.example"); //com.baomidou.ant
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
// templateConfig.setEntity();
// templateConfig.setService();
// templateConfig.setController();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
strategy.setInclude(scanner("表名"));
strategy.setSuperEntityColumns("id");
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
main函数启动,输入项目名+输入表名---》自动创建entity / mapper / srvice / controller
4.编写controller 测试方法,运行springboot
package com.example.demo.controller;
import com.example.demo.entity.Tuser;
import com.example.demo.service.ITuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
//import com.baomidou.ant.common.BaseController;
import java.util.List;
/**
*
* 用户 前端控制器
*
*
* @author jobob
* @since 2019-01-08
*/
@RestController
@RequestMapping("/demo/tuser")
public class TuserController /*extends BaseController*/ {
@Autowired
ITuserService service;
@RequestMapping("")
@ResponseBody
public String test(){
List list = service.list(null);
System.out.println(list.size());
return "123";
}
}
访问最终效果:http://localhost:8099/demo/demo/tuser