Java项目学习(一)
idea环境搭建
Spring Initializr
通过spring Initializr创建spring项目,可以大大简化创建项目流程。
观察pom文件可以发现,项目的父项目是:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
点入其中,可以发现我们的项目继承了其中很多配置。
<properties>
<java.version>1.8</java.version>
<resource.delimiter>@</resource.delimiter>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>**/application*.yml</exclude>
<exclude>**/application*.yaml</exclude>
<exclude>**/application*.properties</exclude>
</excludes>
</resource>
</resources>
还有一些插件的版本管理。
而其也有父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
其中包含非常多版本管理。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
等等......
不过打包时发现一点标签貌似子项目中定义会直接覆盖父项目中定义的,而不是合并。
mybatis-plus使用
maven依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
需要注意的是第一次在maven酷站上查mybatisplus依赖,得到的是mybatisplus-…然鹅这并不是mybatis-plus的依赖。
mapper接口和Service接口
//继承通用mapper接口
public interface HospSetMapper extends BaseMapper<HospitalSet>{
}
//继承Iservice接口
public interface HospSetService extends IService<HospitalSet>{
}
//继承实现类
@Service
public class HospSetServiceImpl extends ServiceImpl<HospSetMapper, HospitalSet> implements HospSetService {
}
这样就不用写xml文件获得了基础增删改查操作和服务,具体有什么发现可以点入类中查看或查看文档。
相关注解
Swagger2使用
用于生成接口文档.
maven依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
swagger2 2.xx版本中需要使用guava.
springfox-swagger-ui用于生成web界面。
//通用配置类
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
//只显示api路径下的页面
.paths(Predicates.and(PathSelectors.regex("/api/.*")))
.build();
}
@Bean
public Docket adminApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("adminApi")
.apiInfo(adminApiInfo())
.select()
//只显示admin路径下的页面
.paths(Predicates.and(PathSelectors.regex("/admin/.*")))
.build();
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("网站-API文档")
.description("本文档描述了网站微服务接口定义")
.version("1.0")
.contact(new Contact("flyzz", "http://f", "1353530978@qq.com"))
.build();
}
private ApiInfo adminApiInfo(){
return new ApiInfoBuilder()
.title("后台管理系统-API文档")
.description("本文档描述了后台管理系统微服务接口定义")
.version("1.0")
.contact(new Contact("flyzz", "http://flyzz.xyz", "1353530978@qq.com"))
.build();
}
}
另外注意如果该配置类是通过utils包引入其他项目中,记得配置basePackages,启动项目访问xxx:端口/swagger-ui.html即可访问接口管理页面。
杂项
todo注释
//TODO 发送短信
这个注释在idea中会高亮显示,b
idea修改模块名重启后无效
找到.idea文件夹,修改其中models.xml中对应model。