maven介绍
- Maven的核心功能便是合理叙述项目间的依赖关系,通俗点讲,就是通过pom.xml文件的配置获取jar包,而不用手动去添加jar包
使用idea构建聚合工程
1. 创建父工程
image.png
image.png
image.png
image.png
2. 创建子工程
image.png
image.png
image.png
- idea 会自动再付pom文件中生成module标签,并且添加子工程
image.png
image.png
- 如上步骤.分别创建子工程(pojo,mapper,service,api)
image.png
3. 添加项目依赖
<dependencies>
<!--
pojo -> common
pojo是可以使用common中相应的方法的
-->
<dependency>
<groupId>org.example</groupId>
<artifactId>springboot-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
image.png
- 以此类推,mapper 依赖 pojo; service 依赖 mapper; api 依赖 service
image.png
image.png
image.png
4. 添加maven依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springboot-module</artifactId>
<version>1.0-SNAPSHOT</version>
<!--子依赖-->
<modules>
<module>springboot-common</module>
<module>springboot-pojo</module>
<module>springboot-mapper</module>
<module>springboot-service</module>
<module>springboot-api</module>
</modules>
<!--父工程的packaging方式必须是pom-->
<packaging>pom</packaging>
<!-- 将springboot作为父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath />
</parent>
<!--自定义环境变量-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- 打包时跳过test测试 -->
<skipTests>true</skipTests>
<hutool.version>5.0.5</hutool.version>
</properties>
<!--依赖,子模块会继承这些依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<!--dependencyManagement这里是作版本控制,相当于只是对依赖的定义,并不是真的依赖这些模块,
而是在子模块依赖相对应的依赖时,能够不用写版本号
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-module</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot-service</artifactId>
<!--自定义环境变量-->
<properties>
<hutool.version>4.6.1</hutool.version>
</properties>
<dependencies>
<!--
service -> mapper -> pojo -> common
service可以使用pojo与common中相应的方法的
-->
<dependency>
<groupId>org.example</groupId>
<artifactId>springboot-mapper</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
</dependencies>
</project>
image.png
5. 编写controller
image.png
http://localhost:8080/hello
image.png
6.配置数据库
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
public interface UsersMapper {
Users queryById(String id);
}
# sql
<!--查询单个-->
<select id="queryById" resultMap="UsersMap">
select
id, username, password, nickname, realname, face, mobile, email, sex, birthday, created_time, updated_time
from users
where id = #{id}
</select>
image.png
############################################################
#
# web访问端口号 约定:8088
#
############################################################
server:
# port: 8088
tomcat:
uri-encoding: UTF-8
max-http-header-size: 80KB
############################################################
#
# 配置数据源信息
#
############################################################
spring:
profiles:
active: dev
datasource: # 数据源的相关配置
type: com.zaxxer.hikari.HikariDataSource # 数据源类型:HikariCP
driver-class-name: com.mysql.jdbc.Driver # mysql驱动
# url: jdbc:mysql://localhost:3306/foodie-shop-dev?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
username: root
# password: root
hikari:
connection-timeout: 30000 # 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 默认:30秒
minimum-idle: 5 # 最小连接数
maximum-pool-size: 20 # 最大连接数
auto-commit: true # 自动提交
idle-timeout: 600000 # 连接超时的最大时长(毫秒),超时则被释放(retired),默认:10分钟
pool-name: DateSourceHikariCP # 连接池名字
max-lifetime: 1800000 # 连接的生命时长(毫秒),超时而且没被使用则被释放(retired),默认:30分钟 1800000ms
connection-test-query: SELECT 1
servlet:
multipart:
max-file-size: 512000 # 文件上传大小限制为500kb
max-request-size: 512000 # 请求大小限制为500kb
############################################################
#
# mybatis 配置
#
############################################################
mybatis:
type-aliases-package: org.example.pojo # 所有POJO类所在包路径
mapper-locations: classpath:mapper/*.xml # mapper映射文件
# configuration:
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
image.png
package org.example.pojo;
import com.alibaba.fastjson.JSON;
import org.example.mapper.UsersMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author Wgs
* @version 1.0
* @create:2020/06/11
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class UsersServiceTest {
@Autowired
private UsersMapper usersMapper ;
@Test
public void getUserBuId(){
Users users = usersMapper.queryById("159063669295256592");
System.out.println(JSON.toJSONString(users,true));
}
}
image.png
7. 配置swagger2
<!-- swagger2 配置 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.6</version>
</dependency>
package org.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2 {
// http://localhost:8088/swagger-ui.html 原路径
// http://localhost:8088/doc.html 原路径
// 配置swagger2核心配置 docket
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2) // 指定api类型为swagger2
.apiInfo(apiInfo()) // 用于定义api文档汇总信息
.select()
.apis(RequestHandlerSelectors
.basePackage("org.example.controller")) // 指定controller包
.paths(PathSelectors.any()) // 所有controller
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("测试api") // 文档页标题
.contact(new Contact("admin",
"https://www.jd.com",
"abc@imooc.com")) // 联系人信息
.description("专为测试提供的api文档") // 详细信息
.version("1.0.1") // 文档版本号
.termsOfServiceUrl("https://www.jdcom") // 网站地址
.build();
}
}
@Api(value = "测试swagger", tags = {"测试的相关接口"})
@RestController
public class HelloController {
@ApiOperation(value = "获取测试接口", notes = "获取测试结果", httpMethod = "GET")
@GetMapping("/hello")
public JSONObject hello() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", 200);
return jsonObject;
}
}
image.png
8. 配置log4j日志
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--引入日志依赖 抽象层 与 实现层-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
- 创建 log4j.properties 并且放到资源文件目录 src/main/resources
log4j.rootLogger=DEBUG,stdout,file
log4j.additivity.org.apache=true
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.threshold=INFO
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %c{1}:%L - %m%n
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file.Threshold=INFO
log4j.appender.file.append=true
log4j.appender.file.File=/workspaces/logs/foodie-api/imooc.log
网友评论