SpringBoot项目配置(后面会更新)
项目创建:1.Maven. 2.SpringBoot Initalizer:点击
pom.xml:
SpringBoot版本尽量2.x,JDK1.8及1.8以上;若用3.x,需要修改JDK版本到17以上。
MyBatisPlus版本尽量高一些。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>hospital</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hospital</name>
<description>医院挂号预约</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
<version>8.0.32</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.26</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**.*</include>
</includes>
</resource>
</resources>
</build>
</project>
MyBatisPlus配置类:阻止数据恶意删除
Swagger配置类:
@SpringBootApplication//springBoot核心注解,启动类里。必写。
/*
请求接口注解
*/
@RequestMapping("/hello",method=RequestMethod.GET) == @GetMapping("/hello")
@RequestMapping("/hello",method=RequestMethod.POST) == @PostMapping("/hello")
@ResponseBoby//请求数据接口(JSON),需要拿数据
@RestController == @Controller+@ResponseBoby
配置文件一:
mybatis-plus:
mapper-locations: classpath:mapper/*.xml #dao层下每一个mapper都对应一个XML,在XML里面写SQL语句放在Mapper文件夹下
#配置SQL日志
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
spring:
profiles:
active: dev
配置文件二:开发环境
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/hospital?useUnicode=true&useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
password: 123456
username: root
科室列表页面
大概效果:
数据库:
创建数据库hospital;
建立科室表department:
插入数据:
后端:代码生成器:
配置类:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.example.hospital.controller.BaseController;
import com.example.hospital.entity.BaseEntity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class AutoGenerator {
private static final String URL="jdbc:mysql://localhost:3306/hospital?useUnicode=true&useSSL=false&characterEncoding=utf-8&serverTimezone=GMT%2B8" ;
private static final String USERNAME="root" ;
private static final String USERPASSWORD="123456";
public static void main(String[] args){
List<String> tables=new ArrayList<>();
tables.add("department");
FastAutoGenerator.create(URL,USERNAME,USERPASSWORD)
.globalConfig(builder -> {
builder.author("hb")//作者
.outputDir(System.getProperty("department.dir")+"\\src\\main\\java")//输出路径(写到Java目录)、
.enableSwagger()//开启Swagger
.commentDate("yyyy-MM-dd")
.fileOverride();//开启覆盖之前生成的文件
})
.packageConfig(builder -> {
builder.parent("com.example.hospital")
//留空请求路径中就会少一个层级 原层级/hospital//user 现层级
.moduleName("")
.entity("entity")
.service("service")
.serviceImpl("serviceImpl")
.controller("controller")
.mapper("dao")
.xml("mapper")
.pathInfo(Collections.singletonMap(OutputFile.mapper,System.getProperty("user.dir")+"\\src\\main\\resourses\\mapper"));
})
.strategyConfig(builder -> {
builder.addInclude(tables)
//.addTablePrefix("p_") //生成是否有前缀
.serviceBuilder()
.formatServiceFileName("%sService")
.formatServiceImplFileName("%sServiceImpl")
.entityBuilder()
.superClass(BaseEntity.class)
//开启Lombok,默认生成@Get,@Set,可以手动换成@Data
// .addTableFills(new Column("create_time", FieldFill.INSERT)) //自动填充
// .addTableFills(new Column("update_time", FieldFill.UPDATE))
.enableLombok()
.logicDeleteColumnName("deleted")
.enableTableFieldAnnotation()
.controllerBuilder()
//如果没有2父类请注释掉
.superClass(BaseController.class)
.formatFileName("%sController")
.enableRestStyle()
.mapperBuilder()
.enableBaseResultMap()//生成通用的resulMNap
.superClass(BaseMapper.class)
.formatMapperFileName("%sMapper")
.enableMapperAnnotation()
.formatMapperFileName("%sMapper");
})
.templateEngine(new FreemarkerTemplateEngine()).execute();//使用FreeMarke为引擎模板。
}
}
BaseController:
@RestController
public class BaseController {
}
BaseEneity:
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.sql.Date;
@Data
public class BaseEntity implements Serializable {
@ApiModelProperty("id")
@TableId(value = "id",type= IdType.AUTO)
private Integer id;
@ApiModelProperty("创建时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@TableField(value="create_time",fill= FieldFill.INSERT)
private Date createTime;
@ApiModelProperty("更新时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@TableField(value="create_time",fill= FieldFill.UPDATE)
private Date updateTime;
@ApiModelProperty("状态 已封禁/已删除/正常")
@TableField("status")
private Integer status;
}
点击运行后结果: