这段时间正在写一个人力资源系统的毕设,想着用spring boot做一个分模块的,因为也用到了dubbo,所以分模块扩展性也比较好,正好做一个记录,记录出现的问题,这一篇可能有一些长,只是简单的分模块,主要是布局能互通,本人测试过了,能行,用的注解的方式
关于怎么创建spring boot怎么创建分模块的项目,这里不多加说明,网上很多
一、整体的项目结构
这里简单介绍一下模块的功能:
- api:往dubbo上挂的服务,也可以认为是公共模块,
- main:里面是server端,也就是dubbo的提供者需要的配置
- support:主要是服务的实现
- webapp:主要是Controller端和前端模板(我这用的freemarker)
父的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>
<version>2.2.4.RELEASE</version>
</parent>
<groupId>com.hr</groupId>
<artifactId>hr-ms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--父模块打包类型必须为pom-->
<packaging>pom</packaging>
<name>hr-ms</name>
<description>人力资源管理系统</description>
<!--子模块继承父模块-->
<modules>
<module>webapp</module>
<module>main</module>
<module>api</module>
<module>support</module>
</modules>
<!--这里进行版本控制-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<dubbo.version>0.2.0</dubbo.version>
</properties>
<dependencyManagement> <!--dependencyManagement用于管理依赖版本号-->
<dependencies>
<!--这个是dubbo zk,springboot整合完后的,要分清-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
二、api模块项目结构与需要注意的地方
1、需要注意的问题
因为是公共模块,所以不需要打包,下面这个在api模块的pom.xml中不需要下面这个,要不会就造成重复打包
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2、api的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>com.hr</groupId>
<artifactId>hr-ms</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.hrms</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>api</name>
<description>微服务的接口模块</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
三、support模块上的结构和需要注意的地方
1、需要注意的地方
- 和上面的api模块一样,不需要打包,pom文件中也就不需要
spring-boot-maven-plugin
- 因为这个是服务的实现包,所以需要能访问api模块的jar包,所以需要在support模块的pom文件上加上下面这个,
<dependency>
<groupId>com.hrms</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
- 注意服务实现的注解不能用错也就是@Service
import com.alibaba.dubbo.config.annotation.Service;
import com.hrms.api.domain.entity.User;
import com.hrms.api.service.UserService;
import com.hrms.support.manager.UserManager;
import javax.annotation.Resource;
/**
* @author 孔超
* @date 2020/4/2 19:07
*/
@Service(interfaceClass = UserService.class) //这个注解必须是dubbo的,
public class UserServiceImpl implements UserService {
@Resource
UserManager userManager;
@Override
public User getById(Long id) {
return userManager.getById(id);
}
}
2、support模块的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>com.hr</groupId>
<artifactId>hr-ms</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.hrms</groupId>
<artifactId>support</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>support</name>
<description>服务的实现</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!--引用api模块-->
<dependency>
<groupId>com.hrms</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
三、main模块结构和需要注意的地方
1、需要注意的地方
- 这里的pom文件需要下面这个,整个server端只需要构建一个,而api,support,main三个只需要构建main这个
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- main的启动类需要注意的地方
/**
* @author 孔超
* @date 2020/4/2 19:07
*/
@SpringBootApplication(scanBasePackages = {"com.hrms.support.manager"})//因为有manger层,而且是在support模块里,就需要指定扫描的位置
@EnableDubbo(scanBasePackages = "com.hrms.support.service.impl") //扫描服务的实现类
@MapperScan(value = "com.hrms.support.dao") //扫描dao的接口,别忘了在dao接口上加上@Mapper
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
- 注意mapper文件的扫描位置,如果不是多模块就没问题,但是如果涉及到多模块,可能扫描不到resource文件夹下的mapper文件,
mybatis.mapper-locations=classpath:mapper/*.xml
如果上面不行的话可以用下面的
mybatis.mapper-locations=classpath*:mapper/*.xml
关于区别可以另行百度
2、main的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>com.hr</groupId>
<artifactId>hr-ms</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.hrms</groupId>
<artifactId>main</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>main</name>
<description>微服务挂起</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!--引用support模块-->
<dependency>
<groupId>com.hrms</groupId>
<artifactId>support</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
四、webapp的结构和需要注意的地方
1、需要注意的地方
- 对于依赖注入不能再用以前的注解了,因为没有可以注入的服务实现,所以要改用
@Reference
/**
* @author 孔超
* @date 2020/4/2 19:07
*/
@Controller
public class UserController {
@Reference//这不再用 @Autowired,因为你没实现类怎么注入
private UserService userService;
@RequestMapping("/")
public String findAll(Model model) throws Exception {
User user = userService.getById(1L);
model.addAttribute("a", user.getAccount());
return "index";
}
}
-
官网上在dubbo调用者配置上用了
@EnableDubbo(scanBasePackages = "服务的接口")
,所以我就在webapp的启动类上加上了
/**
* @author 孔超
* @date 2020/4/2 19:07
*/
@SpringBootApplication
@EnableDubbo(scanBasePackages = "com.hrms.api.service")
public class WebappApplication {
public static void main(String[] args) {
SpringApplication.run(WebappApplication.class, args);
}
}
2、webapp的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>com.hr</groupId>
<artifactId>hr-ms</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.hrms</groupId>
<artifactId>webapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>webapp</name>
<description>人力资源管理系统的web层</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!--需要引用api模块-->
<dependency>
<groupId>com.hrms</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
五、分析分析模块之间的关系
上面就是各个模块之间引用的关系,感觉应该能看图看懂