springboot使用maven分模块搭建环境(idea)
程序员文章站
2024-02-03 20:50:28
...
一、搭建基本架构
创建聚合父工程
删除多余文件,只留一个pom文件
在父工程下创建子模块
对着父工程右键 - New - Module - > 输入 web
对着父工程右键 - New - Module - > 输入 service
..等
修改pom文件,导入依赖模块jar(不是springboot依赖)
repo依赖entity,service依赖dao,controller依赖service
修改各个模块的pom.xml文件,例如repo层:(加入依赖的entity的坐标)
其他模块类似(注意,依赖version一定要写,否则报错,与父工程version一致)
<?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>vote</artifactId>
<groupId>com.mlj</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mlj.repo</groupId>
<artifactId>repo</artifactId>
<dependencies>
<dependency>
<groupId>com.mlj.entity</groupId>
<artifactId>entity</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
clean install各个模块
注意修改web模块的打包方式为war
看是否报错,没有报错才能执行下一步
二、加入jar包依赖(springboot依赖),配置
我是为了方便全放在父模块的pom里边了,继承springboot父工程
<?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>com.mlj</groupId>
<artifactId>vote</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>common</module>
<module>entity</module>
<module>service</module>
<module>web</module>
<module>repo</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--spring boot Configuration Annotation Proessor not found in classpath异常-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.mlj.vote.controller.GirlController</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
web模块下resource下创建 application.yml
spring:
#开发环境配置
# profiles:
# active: dev
#数据库配置
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/dbgirl
username: root
password: root
#jpa配置
jpa:
hibernate:
ddl-auto: update
show-sql: true
创建启动类application
package com.mlj.vote;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class VoteApplication {
public static void main(String[] args) {
SpringApplication.run(VoteApplication.class, args);
}
}
三、测试是否搭建成功
新建数据库dbvote
实体类
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.Min;
@Entity
public class Girl {
@Id
@GeneratedValue
private Integer id;
private String cupSize;
@Min(value = 18,message = "禁止未成年入内!")
private Integer age;
public Girl() {
}
// 必须有无参构造
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
repo
package com.mlj.vote.repo;
import com.mlj.vote.entity.Girl;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface GirlRepository extends JpaRepository<Girl,Integer> {
}
controller
package com.mlj.vote.controller;
import com.mlj.vote.entity.Girl;
import com.mlj.vote.repo.GirlRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class GirlController {
@Autowired
private GirlRepository girlRepository;
/**
* 获取列表
*
* @return
*/
@GetMapping(value = "/girls")
public List<Girl> girlList() {
return girlRepository.findAll();
}
}
启动运行:
四、问题与解决
Unable to find main class
Spring Boot Maven Plugin打包异常及三种解决方法:Unable to find main class
IDEA_maven依赖错误 包下面红色波浪线
开始搭建环境时候先把springboot依赖写上了,有些依赖导入不进去,修改pom 配置文件,讲标红的依赖先删除,并点击reimport, 之后重新加上出错的依赖,再reimport
推荐先把多模块跑起来再加入依赖和配置
Error:java:JDK isn't specified for module
忘记部署了。。web打包方式应该改为war,部署webwar包