参考文章:https://blog.csdn.net/qq_34410726/article/details/98214992
一、maven分布式工程的基本架构
demo #父工程模块,主要用来定义整个分布式工程的依赖版本
---- common #公共模块,主要用来定义一些公共的组件,比如实体类等
---- function-one #功能模块1,引入common模块
---- function-two #功能模块2,引入common模块
---- pom.xml
二、maven父工程demo的搭建
1.打开IDEA,File->New->New Project,然后选择Empty Project,如下:
接下来会弹出窗口让我们新建modules,点击+号,新建一个父工程,也就是一个父module。然后我们选择maven工程,选择jdk版本
2.pom.xml
创建好之后,该父工程demo是个空的maven工程,只有src目录和pom.xml文件,删除src目录,修改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.0modelVersion> <groupId>com.scgroupId> <artifactId>demoartifactId> <version>1.0-SNAPSHOTversion> <packaging>pompackaging> <name>demoname> <description>This is parent projectdescription> <modules> <module>commonmodule> <module>function-onemodule> <module>function-twomodule> modules> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.0.2.RELEASEversion> parent> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding> <java.version>1.8java.version> <mysql.driver.version>5.1.29mysql.driver.version> <org.mybatis.spring.boot.version>1.3.1org.mybatis.spring.boot.version> <alibaba.druid.version>1.1.9alibaba.druid.version> <spring.cloud.version>Edgware.SR1spring.cloud.version> properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-dependenciesartifactId> <version>${spring.cloud.version}version> <type>pomtype> <scope>importscope> dependency> dependencies> dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starterartifactId> <version>2.0.2.RELEASEversion> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> <version>2.0.2.RELEASEversion> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.12version> <scope>testscope> dependency> <dependency> <groupId>org.aspectjgroupId> <artifactId>aspectjweaverartifactId> <version>1.8.8version> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <version>1.16.20version> dependency> <dependency> <groupId>org.mybatis.spring.bootgroupId> <artifactId>mybatis-spring-boot-starterartifactId> <version>${org.mybatis.spring.boot.version}version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <scope>runtimescope> <version>${mysql.driver.version}version> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>druidartifactId> <version>${alibaba.druid.version}version> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-surefire-pluginartifactId> <configuration> <skip>trueskip> configuration> plugin> plugins> build> project>
三、maven子工程common模块的搭建
1.创建
2.创建之后,只有src空目录和pom.xml,我们可以把图中这些文件放在common公共模块中
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <groupId>com.scgroupId> <artifactId>demoartifactId> <version>1.0-SNAPSHOTversion> parent> <modelVersion>4.0.0modelVersion> <artifactId>commonartifactId> <version>1.0-SNAPSHOTversion> <packaging>jarpackaging> <properties> <joda.time.version>2.9.9joda.time.version> <commons.lang.version>2.6commons.lang.version> <commons.io.version>2.5commons.io.version> <fastjson.version>1.2.29.sec06fastjson.version> <pinyin4j.version>2.5.1pinyin4j.version> <commons.codec.version>1.9commons.codec.version> properties> <dependencies> <dependency> <groupId>joda-timegroupId> <artifactId>joda-timeartifactId> <version>${joda.time.version}version> dependency> <dependency> <groupId>commons-langgroupId> <artifactId>commons-langartifactId> <version>${commons.lang.version}version> dependency> <dependency> <groupId>commons-iogroupId> <artifactId>commons-ioartifactId> <version>${commons.io.version}version> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>fastjsonartifactId> <version>${fastjson.version}version> dependency> <dependency> <groupId>com.belerwebgroupId> <artifactId>pinyin4jartifactId> <version>${pinyin4j.version}version> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-data-redisartifactId> dependency> <dependency> <groupId>commons-codecgroupId> <artifactId>commons-codecartifactId> <version>${commons.codec.version}version> dependency> <dependency> <groupId>javax.persistencegroupId> <artifactId>persistence-apiartifactId> <version>1.0version> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-thymeleafartifactId> dependency> dependencies> project>
4.安装lombok插件,创建一个实体类UserEntity.java
lombok插件:简化Entity实体类,@Data,可以免写set/get方法
UserEntity.java
package entity.one; import lombok.Data; import javax.persistence.Column; import javax.persistence.Table; @Data @Table(name = "user") public class UserEntity { @Column(name = "id") private Integer id; @Column(name = "name") private String name; @Column(name = "age") private Integer age; }
5.新建Mapper层类
UserMapper.java
package mapper.one; import entity.one.UserEntity; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Component; import java.util.List; @Component public interface UserMapper{ @Select("SELECT * FROM user") ListgetList(); UserEntity getById(@Param("id") Integer id); }
UserMapper.xml
xml version="1.0" encoding="UTF-8"?> DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="mapper.one.UserMapper"> <resultMap id="BaseResultMap" type="entity.one.UserEntity"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="name" jdbcType="VARCHAR" property="name" /> <result column="age" jdbcType="INTEGER" property="age" /> resultMap> <select id="getById" resultType="entity.one.UserEntity"> SELECT * FROM user WHERE id = #{id} select> mapper>
四、maven子工程function-one模块的搭建
1.创建之后,添加一些目录,function-two类似
2.pom.xml,function-two也引入common包
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> <groupId>com.scgroupId> <artifactId>demoartifactId> <version>1.0-SNAPSHOTversion> parent> <modelVersion>4.0.0modelVersion> <artifactId>function-oneartifactId> <packaging>jarpackaging> <dependencies> <dependency> <groupId>com.scgroupId> <artifactId>commonartifactId> <version>1.0-SNAPSHOTversion> dependency> dependencies> project>
3.application.yml
server:
port: 8666
servlet:
context-path: /demo
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/demo_one?useSSL=false&useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
#thymeleaf
thymeleaf:
prefix: classpath:/templates/
suffix: .html
cache: false
servlet:
content-type: text/html
enabled: true
encoding: UTF-8
mode: HTML5
mybatis:
mapper-locations: classpath:mapper/*.xml #配置映射文件
type-aliases-package: entity #配置实体类
function-two,端口号8888,连接的数据库demo_two
4.OneApplication.java
package com.demo; import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan({"mapper"}) //项目启动时会扫描mapper包及子包的接口,否则会报错找不到mapper文件
public class OneApplication {
public static void main(String[] args) { SpringApplication.run(OneApplication.class, args); } }
五、测试
一、直接后台测试
common模块的UserEntity.java、UserMapper.java、UserMapper.xml
function-one模块:
1.UserController.java
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.demo.service.UserService; import java.util.List; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/getList") public ListgetList(){ return userService.getList(); } @GetMapping("/getById/{id}") public UserEntity getById(@PathVariable("id") Integer id){ return userService.getById(id); } }
2.UserService.java
package com.demo.service; import entity.one.UserEntity; import java.util.List; public interface UserService{ ListgetList(); UserEntity getById(Integer id); }
3.UserServiceImpl.java
package com.demo.service.impl; import entity.one.UserEntity; import mapper.one.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.demo.service.UserService; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public ListgetList() { return userMapper.getList(); } @Override public UserEntity getById(Integer id) { return userMapper.getById(id); } }
4.测试
http://localhost:8666/demo/user/getList
二、前后端交互数据测试
1.利用前后端数据交互模板引擎:thymeleaf
引入包,然后在.yml中添加配置
resources/templates目录下hello.html
DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>hellotitle>
head>
<body>
<h1>this is the hello.html in templatesh1>
<span th:text="${key}">span>
body>
html>
2.HelloController.java
package com.demo.controller; import com.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller //需跳转到页面,不能用@RestController public class HelloController { @Autowired private UserService userService; @RequestMapping("/hello") public ModelAndView sayHello(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("hello"); String name = userService.getById(1).getName(); modelAndView.addObject("key", "您好!"+ name); return modelAndView; } }
3.测试
http://localhost:8666/demo/hello
补充:
不管是自己搭建一个项目,还是导入一个项目打开,需注意2点:
1.JDK是不是1.8的
2.maven配置