一、使用spring initializr创建java工程
- 1、启动IDEA,新建java工程,使用向导创建一个springboot框架的工程
- 2、设置项目信息,java版本选择8
- 3、勾选项目需要用到的依赖
- 4、设置项目名称,点击完成
- 5、等待maven将项目所需要的依赖都下载完毕,展开项目结构,如下图所示,这就创建完一个springboot框架的简单工程
二、修改工程,添加web模块
- 1、修改appdemo工程的pom文件,修改工程打包方式为pom,这样项目就变成了一个父工程
<packaging>pom</packaging>
- 2、打开文件-新建-模块,打开新模块创建向导,选择maven模式,不需要选择模板,点击下一步
- 3、设置模块名称为web,可以看到父工程为appdemo,点击完成
- 4、等待maven导入模块完毕,展开项目结构,如下图,appdemo工程中增加了web模块
- 5、在appdemo的pom文件中,会自动添加模块信息
<modules>
<module>web</module>
</modules>
- 6、修改web模块中的pom文件,增加打包方式
<packaging>jar</packaging>
- 7、展开工程框架,将父工程中的包:com.example.appdemo,以及启动文件,都移动到web模块的java文件夹下
多模块项目中,项目启动由web模块进行管理,所以需要将启动文件以及包结构,移动到web模块下
移动完毕,项目架构如下
- 8、删除没用的文件夹及文件,删除红框中的内容
在多模块工程中,开发各种代码,分别在模块中进行,不在父工程中开发,所以父工程appdemo中的src文件夹,就没用了
三、添加entity、service、serviceImpl、dao模块
- 1、按照添加web模块的方式,添加entity、service、serviceImpl、dao模块
- 2、修改各模块的pom文件,都增加打包方式:
<packaging>jar</packaging>
- 3、父工程中的pom文件,会自动增加模块信息
<modules>
<module>web</module>
<module>entity</module>
<module>service</module>
<module>serviceImpl</module>
<module>dao</module>
</modules>
- 4、模块全部添加完毕后,项目文件结构如下:
四、修改项目依赖信息
1、修改父项目依赖
- 1、在第一步创建springboot框架项目后,pom文件中自动添加了工程需要的依赖,这个暂时不需要修改
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<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>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>
</dependencies>
- 2、将各子模块,做为依赖,引入到父项目中(没有引入web模块,因为web模块会依赖其他模块,但是其他模块不会依赖web模块)
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>serviceImpl</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
使用dependencyManagement对依赖进行管理,可以使子模块在引用管理中的依赖时,不用再设置版本号。如果不明白这个做法的原理的小伙伴,请戳dependencies与dependencyManagement的区别
2、修改web模块pom文件,增加如下依赖
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>entity</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>service</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>serviceImpl</artifactId>
</dependency>
</dependencies>
3、修改service模块pom文件,增加如下依赖
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>entity</artifactId>
</dependency>
</dependencies>
4、修改serviceImpl模块pom文件,增加如下依赖
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>entity</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>dao</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>service</artifactId>
</dependency>
</dependencies>
5、修改entity模块pom文件,增加如下依赖
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
6、修改dao模块pom文件,增加如下依赖
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
7、修改父项目appdemo的pom文件,删除数据库相关的依赖
<!--<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>-->
因为在父项目中设置的依赖,子模块中会自动继承,无需重复引用,但是并不是每个子模块都会需要连接、操作数据的这些依赖,所以在父项目的pom文件中,将这些依赖删除,在涉及到连接数据库,操作数据库的dao模块,以及涉及到使用实体类创建表的entity模块,单独引入这些必要的依赖即可。
五、修改启动配置
因为启动类AppdemoApplication已经移动到web模块,并且要求项目是从web模块启动,所以需要删除父项目中的启动配置,并在web的pom文件中增加启动配置
- 1、注释掉父项目pom文件中build部分
<!--<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>
</plugins>
</build>-->
- 2、在web模块的pom文件中增加build配置
<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>
</plugins>
</build>
比较简单的方法就是把父项目中的这个build配置项,复制到web模块的pom中。
如果不做启动项的修改,在运行启动类时,会提示找不到main方法,导致项目无法启动。
六、在各模块中编写代码
1、在entity模块中增加实体类文件Response和StuInfo
- 1、Response类用于在controllor中给前端返回结果使用,代码如下(为了减少代码量,使用了lombok,如果不使用lombok,需要自己手动编写setter/getter方法)
package com.example.entity.common;
import lombok.Data;
@Data
public class Response<T> {
private int code;
private String message;
private T data;
public Response(int code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
}
- 2、StuInfo类作为数据库建表、前后端数据传输、dao层操作数据的数据模板使用,代码如下
package com.example.entity.stuEntity;
import lombok.Data;
import javax.persistence.*;
import java.sql.Timestamp;
@Data
@Entity
@Table(name = "stuinfo")
public class StuInfo{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;//id,键值,自增
private int stuid;//学生id
private String name;//姓名
private int gender;//性别
private int age;//年龄
private int grade_num;//年级
private int class_num;//班级
private int status;//状态,1-数据有效,2-数据删除
private Timestamp createtime;//创建时间
private Timestamp updatetime;//更新时间
}
@Table(name = “stuinfo”)会报红线,如果不想显示红线,需要在项目中配置数据库信息,然后做下关联就行,不做处理也无影响
2、在dao模块中,增加数据库操作接口
- 1、因为操作数据库,使用的是jpa,jap提供了很多封装,此处只是用于做demo,所以不写过于复杂,只需要继承JpaRepository接口即可,代码如下
package com.example.dao;
import com.example.entity.stuEntity.StuInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StuInfoDao extends JpaRepository<StuInfo, Long> {
}
- 2、增加数据库连接以及jpa配置文件
因为存在开发环境,测试环境,预发环境,线上环境等多种环境,为了方便切换不同环境的,所以可以设置多个配置文件,配置文件名称格式为:application-XXX.yml,如开发环境的,可以写成:application-dev.yml
如果引用dev的这个配置文件,只需要在配置文件application.yml中,激活该配置文件即可,格式如下
spring:
profiles:
active: dev
application-dev.yml中配置了数据库连接和jpa等信息,内容如下(该部分需要根据自己的数据库信息,还有数据库的版本进行调整)
spring:
datasource:
url: jdbc:mysql://localhost:3306/demo_db?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&serverTimezone=UTC
# url: jdbc:mysql://localhost:3306/demo_db?serverTimezone=UTC&useSSL=true&allowPublicKeyRetrieval=true&useUnicode=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
database: mysql
show-sql: true
hibernate:
ddl-auto: create
naming:
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
以上配置文件中,
url:连接mysql数据库的url,网上很多介绍,不做赘述
username、password:在选择的时候,不要写成data-username、data-password,否则启动项目的时候,会报连接数据库账号无效或无权限的情况,我是经常会写错,导致连接数据库的经常出现问题。
driver-class-name:这个驱动配置,不同版本的要求不同,不过现在高版本的驱动应该都是这个
3、在service模块中定义接口StuService
接口代码如下
package com.example.service;
import com.example.entity.stuEntity.StuInfo;
public interface StuService {
Boolean save(StuInfo stuInfo);
}
4、在serviceImpl模块中编写接口实现类
代码如下
package com.example.serviceImpl;
import com.example.dao.StuInfoDao;
import com.example.entity.stuEntity.StuInfo;
import com.example.service.StuService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Timestamp;
@Service
@Log4j2
public class StuServiceImpl implements StuService {
@Autowired
private StuInfoDao stuInfoDao;
@Override
public Boolean save(StuInfo stuInfo) {
stuInfo.setStatus(1);
stuInfo.setCreatetime(new Timestamp(System.currentTimeMillis()));
stuInfo.setUpdatetime(new Timestamp(System.currentTimeMillis()));
log.error("测试日志");
return Boolean.TRUE;
}
}
@Service,标注该类为接口实现类,可供spring扫描注入
@Log4j2,日志工具
@Autowired,将StuInfoDao进行注入
5、在web模块中编写controllor类,供前端请求调用
代码如下
package com.example.appdemo.controllor;
import com.example.entity.common.Response;
import com.example.entity.stuEntity.StuInfo;
import com.example.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/stu")
public class StuInfoControllor {
@Autowired
private StuService stuService;
@PostMapping("/addStu")
public Response<Boolean> addStu(@RequestBody StuInfo stuInfo) {
if (stuService.save(stuInfo)) {
return new Response<>(200, "保存成功", Boolean.TRUE);
} else {
return new Response<>(400, "保存失败", Boolean.FALSE);
}
}
}
6、修改启动类AppdemoApplication,增加扫描注解
工程在启动过程中,spring会对工程内的类进行扫描,自动生成对象供程序调用,但是有时候工程自动扫描时会忽略某些类,这就需要明确指定需要扫描的包,启动类代码如下
package com.example.appdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication(scanBasePackages = "com.example")
@ComponentScan({"com.example"})
@EnableJpaRepositories("com.example")
@EntityScan("com.example.entity")
public class AppdemoApplication {
public static void main(String[] args) {
SpringApplication.run(AppdemoApplication.class, args);
}
}
七、清理、安装、运行、测试
- 1、使用maven工具【clean】上次打包的文件
- 2、清理完毕后,【install】程序,将各模块进行编译,方便模块之间的依赖和调用
- 3、安装完成后,右键运行启动类,成功运行
- 4、使用postman调用接口:http://localhost:8080/stu/addStu,content-type设置为【application/json;charset=utf-8】
八、搭架子时碰到的问题
- 1、数据库连接的配置文件错误,导致连接数据库时报用户名不能连接,这个注意配置的关键字不要写错
- 2、各模块的依赖,有些依赖会和父项目冲突,这个需要调整,重复引用的的依赖,需要做依赖的清理
- 3、spring的某些自动注入不能实现,很多是由于spring扫描忽略导致的,需要在启动类中添加扫描的包,参考:https://blog.youkuaiyun.com/machuang30508/article/details/78616501,https://blog.youkuaiyun.com/jakemanse/article/details/83271442
- 4、项目的改来改去,碰到了
Error:java:JDK isn’t specified for module
错误,这个只要把工程父项目的【.idea】文件夹删除,重新刷新生成即可解决,参考:https://blog.youkuaiyun.com/javaboyweng/article/details/103265754 - 5、需要注意的地方,每个模块的包名结构,应该是一样的,比如我的,就都是com.example的
九、好玩的配置
- 1、在线生成一个banner文件样式:https://www.bootschool.net/ascii
- 2、将生成的字符复制到文件 banner.txt 中
- 3、将 banner.txt 文件放到web模块的resource中,启动项目会看到你设置的字符