SpringBoot--多模块开发
目录结构:
实现步骤:
1.创建springboot项目--spring-boot-demo。
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cxb</groupId> <artifactId>spring-boot-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <modules> <module>web</module> <module>persistence</module> <module>model</module> </modules> <!--修改成 pom --> <!-- 模型层:model 持久层:persistence 表示层:web web依赖persistence, persistence依赖于model --> <packaging>pom</packaging> <name>spring-boot-demo</name> <description>Demo project for Spring Boot</description> <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-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
创建模块web:
一直next,填好模块名称即可。
web模块的目录结构:
这里的web.xml文件是为了打成war时候添加的。
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-boot-demo</artifactId> <groupId>com.cxb</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>web</artifactId> <!--将package值(默认jar)调整成war,这里就必须添加一个web.xml文件了 --> <packaging>war</packaging> <!--web依赖于persistence--> <!--web依赖于model--> <!--实现了间接依赖--> <dependencies> <!--添加persistence依赖--> <dependency> <groupId>com.cxb</groupId> <version>0.0.1-SNAPSHOT</version> <artifactId>persistence</artifactId> </dependency> </dependencies> <!--这里需要指明工程启动的主类,父工程里边不需要build这一块了--> <build> <!--加上这个是因为打成war时,编译出错了--> <defaultGoal>compile</defaultGoal> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!--指定mainClass--> <configuration> <mainClass>springbootdemo.SpringBootDemoApplication</mainClass> </configuration> </plugin> </plugins> </build> </project>
package springbootdemo.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.reactive.function.server.RequestPredicates; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerResponse; import reactor.core.publisher.Flux; import springbootdemo.model.User; import springbootdemo.repository.UserRepository; import java.util.Collection; /** * 路由器函数配置 * Created by 81046 on 2018-06-16 */ @Configuration public class RouterFunctionConfiguration { /** * Servlet * 请求接口:ServletRequest或者HttpServletRequest * 响应接口:ServletResponse或者HttpServletResponse * Spring 5.0 重新定义了服务请求和响应接口 * 请求接口:ServletRequest * 响应接口:ServletResponse * 即可支持Servlet 规范,也可以支持自定义,比如 Netty (Web Server) * * 以本例: * 定义GET请求,并且返回所有的用户对象 URI:/user/findAll * Flux 是0-N个对象集合 * Mono 是0-1个对象集合 * Reactive 中的Flux 或者 Mono 它是异步处理 (非阻塞) * 集合对象基本上是同步处理 (阻塞) * Flux 或者 Mono 都是 Publisher */ @Bean public RouterFunction<ServerResponse> userFindAll(UserRepository userRepository){ return RouterFunctions.route(RequestPredicates.GET("/user/list"), request -> { //返回所有的用户 Collection<User> users = userRepository.findAll(); Flux<User> userFlux = Flux.fromIterable(users); return ServerResponse.ok().body(userFlux,User.class); }); } }
package springbootdemo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import springbootdemo.model.User; import springbootdemo.repository.UserRepository; import java.util.Collection; /** * Created by 81046 on 2018-06-16 */ @RestController @RequestMapping("/user") //RestController 等价于controller和responseBody public class UserController { private final UserRepository userRepository; /** * 这里是采用构造器的注入方式注入userRepository * 好处是提前初始化,不能修改 * @Autowired 可写可不写 */ @Autowired public UserController(UserRepository userRepository){ this.userRepository=userRepository; } /** * http://localhost:8080/user/save?name=小石潭记 * @param name * @return */ @PostMapping("/save") public User save(@RequestParam String name){ User user = new User(); user.setName(name); if (userRepository.save(user)){ System.out.println("保存对象成功:" + user); } return user; } @GetMapping("/list") public Collection<User> list(){ return userRepository.findAll(); } }
package springbootdemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringBootDemoApplication.class, args); } }
这里web模块就完成了,注意该模块需要依赖其他模块的文件,依赖在该模块pom.xml文件中。
多模块就是为了职责分明,web模块只做web端的访问,model模块就是用来管理实体类的,persistence模块就是管理数据库打交道的。
接下来介绍model模块和persistence模块。
创建模板的方式和前面的web模块一样。
model模块目录结构:
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-boot-demo</artifactId> <groupId>com.cxb</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>model</artifactId> </project>
package springbootdemo.model; /** * Created by 81046 on 2018-06-16 */ public class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public User() { } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
persistence模块目录结构:
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-boot-demo</artifactId> <groupId>com.cxb</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>persistence</artifactId> <dependencies> <!--添加模型依赖--> <dependency> <groupId>com.cxb</groupId> <version>0.0.1-SNAPSHOT</version> <artifactId>model</artifactId> </dependency> </dependencies> </project>
package springbootdemo.repository; import org.springframework.stereotype.Repository; import springbootdemo.model.User; import java.util.Collection; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicInteger; /** * Created by 81046 on 2018-06-16 */ @Repository public class UserRepository { /** * 采用内存型的存储方式-->Map */ private final ConcurrentMap<Integer,User> repository = new ConcurrentHashMap<>(); /** * id生成器 */ private final static AtomicInteger idGennerator = new AtomicInteger(); /** * 保存用户对象 */ public boolean save(User user){ int id = idGennerator.incrementAndGet(); user.setId(id); return repository.put(id,user) == null ; } /** * 查询所有的用户 * @return */ public Collection<User> findAll(){ return repository.values(); } }
这里就完成了三个模块的实现,注意每个模块的包名保持一致,这里包名全是springbootdemo,然后在下面创建具体的包即可。
再介绍一下打包的方式:
1.jar 在该文件的位置执行cmd 执行mvn -Dmaven.test.skip -U clean package
这里jar的方式介绍完了,war只是将pom文件中的jar改成war