目录
一、创建项目
1、先创建一个空项目
2、设置项目SDK等
二、创建父模块 选择springboot
1、创建父模块parent
2、删除多余文件,只保留pom.xml
3、修改pom.xml
4、将部分公共依赖加入到pom
三、创建实体类子模块entity
1、创建实体类子模块entity
2、删除无关文件
3、修改pom
4、复制实体类文件entity到目录下(没有就创建)
四、创建工具类子模块utils
1、创建工具类子模块utils
2、删除多余文件,只保留pom.xml
3、修改pom.xml
4、复制实体类文件utils到目录下(没有就创建)
五、创建web项目子模块web-management
1、创建web项目子模块web-management
2、修改pom.xml
3、复制web项目文件
4、新建resources
5、resources新建目录com/bocai/mapper
6、复制相关文件
7、 复制启动程序
六、启动服务
1、启动后端AppApplication
2、启动ngnix
3、验证数据
前言:接《[JavaWeb]【十四】web后端开发-MAVEN高级》开始从头开始分模块设计java项目
一、创建项目
1、先创建一个空项目
2、设置项目SDK等
SDK选择1.8,Language Level 选择8。
二、创建父模块 选择springboot
1、创建父模块parent
2、删除多余文件,只保留pom.xml
3、修改pom.xml
<?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>
<!-- 1、修改为2.7.5 -->
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bocai</groupId>
<artifactId>bocai-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 2、新增设置打包模式为pom -->
<packaging>pom</packaging>
<!-- 3、注释 name与description -->
<!--<name>bocai-parent</name>
<description>bocai-parent</description>-->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 4、注释以下依赖 -->
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
-->
</dependencies>
<!-- 5、注释 build -->
<!--
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
-->
</project>
4、将部分公共依赖加入到pom
<?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>
<!-- 1、修改为2.7.5 -->
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bocai</groupId>
<artifactId>bocai-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 2、新增设置打包模式为pom -->
<packaging>pom</packaging>
<!-- 3、注释 name与description -->
<!--<name>bocai-parent</name>
<description>bocai-parent</description>-->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 6、 加入的公共依赖 -->
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- 4、注释以下依赖 -->
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
-->
</dependencies>
<!-- 5、注释 build -->
<!--
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
-->
</project>
三、创建实体类子模块entity
1、创建实体类子模块entity
2、删除无关文件
3、修改pom
<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>
<parent>
<groupId>com.bocai</groupId>
<artifactId>bocai-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../bocai-parent/pom.xml</relativePath>
</parent>
<artifactId>bocai-entity</artifactId>
<!-- 1、注释 打包方式(不确定)-->
<!--
<packaging>jar</packaging> -->
<name>bocai-entity</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 2、 注释 junit -->
<!--
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
-->
</dependencies>
</project>
4、复制实体类文件entity到目录下(没有就创建)
四、创建工具类子模块utils
1、创建工具类子模块utils
2、删除多余文件,只保留pom.xml
略
3、修改pom.xml
<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>
<parent>
<groupId>com.bocai</groupId>
<artifactId>bocai-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../bocai-parent/pom.xml</relativePath>
</parent>
<artifactId>bocai-utils</artifactId>
<!-- 1、注释 打包方式(不确定)-->
<!--
<packaging>jar</packaging> -->
<name>bocai-utils</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 2、 注释 junit -->
<!--
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
-->
<!-- 3、加入依赖 -->
<!-- web起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 阿里云OSS依赖-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- no more than 2.3.3-->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.3</version>
</dependency>
<!--JWT令牌-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
</dependencies>
</project>
4、复制实体类文件utils到目录下(没有就创建)
五、创建web项目子模块web-management
1、创建web项目子模块web-management
2、修改pom.xml
核心是引入本文的子模块entity和utils
<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>
<parent>
<groupId>com.bocai</groupId>
<artifactId>bocai-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../bocai-parent/pom.xml</relativePath>
</parent>
<artifactId>bocai-web-management</artifactId>
<!-- 1、注释 打包方式(不确定)-->
<!--
<packaging>jar</packaging> -->
<name>bocai-web-management</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 2、 注释 junit -->
<!--
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
-->
<!-- 3、加入 子模块依赖 -->
<dependency>
<groupId>com.bocai</groupId>
<artifactId>bocai-entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.bocai</groupId>
<artifactId>bocai-utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 4、加入依赖 -->
<!-- web起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis起步依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.1</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!-- springboot单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- PageHelper分页插件https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.6</version>
</dependency>
<!--fastJSON-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<!--AOP-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
</project>
3、复制web项目文件
部分文件可能会报错,主要体现在 web起步依赖 ,到每个文件单独处理一下
4、新建resources
5、resources新建目录com/bocai/mapper
6、复制相关文件
7、 复制启动程序
package com.bocai;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@Slf4j
@ServletComponentScan //开启了对servlet组件支持 Filter
@SpringBootApplication
public class AppApplication {
public static void main(String[] args) {
SpringApplication.run(AppApplication.class, args);
log.info("============ 服务启动完成!=====================");
}
}
AppApplicationTests 注释了一个测试代码,打包的时候报错
package com.bocai;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
class AppApplicationTests {
@Test
void contextLoads() {
}
/**
* 生成Jwt令牌
*/
@Test
public void testGenJwt(){
Map<String, Object> claims = new HashMap<>();
claims.put("id",1);
claims.put("name","bocai");
String jwt = Jwts.builder()
.signWith(SignatureAlgorithm.HS256, "bocai") // 签名算法 算法有哪些上官网
.setClaims(claims) //自定义的内容(载荷)
.setExpiration(new Date(System.currentTimeMillis() + 3600* 1000)) // 设置有效期为1h
.compact();
System.out.println(jwt);
}
/**
* 解析Jwt, parseClaimsJws(换成上面生成的jwt)
*/
// @Test
// public void testPareJwt(){
// Claims claims = Jwts.parser()
// .setSigningKey("bocai") //runa要与前面生成一致
// .parseClaimsJws("eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiYm9jYWkiLCJpZCI6MSwiZXhwIjoxNjk3NTM5MTQzfQ.kh_8USqITQHQvnOpWuF8Z7BB0x_BVhfZvOgF1JMcj8o")
// .getBody();
// System.out.println(claims);
//
// }
}