參考文章:https://blog.csdn.net/qq_34410726/article/details/98214992html
1、maven分佈式工程的基本架構java
demo #父工程模塊,主要用來定義整個分佈式工程的依賴版本mysql
---- common #公共模塊,主要用來定義一些公共的組件,好比實體類等web
---- function-one #功能模塊1,引入common模塊redis
---- function-two #功能模塊2,引入common模塊spring
---- pom.xmlsql
2、maven父工程demo的搭建數據庫
1.打開IDEA,File->New->New Project,而後選擇Empty Project,以下:apache
接下來會彈出窗口讓咱們新建modules,點擊+號,新建一個父工程,也就是一個父module。而後咱們選擇maven工程,選擇jd 版本json
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.0</modelVersion> <groupId>com.sc</groupId> <artifactId>demo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>demo</name> <description>This is parent project</description> <!-- 子模塊清單(建立新子模塊後,會自動填入)--> <modules> <module>common</module> <module>function-one</module> <module>function-two</module> </modules> <!-- 本項目的父模塊使用spring-boot框架 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <!-- 項目自定義屬性 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <!-- 依賴的版本號:在定義版本時,要注意的是不一樣的依賴版本之間會有影響,有些最新的版本不支持其餘依賴的低版本一塊兒使用--> <mysql.driver.version>5.1.29</mysql.driver.version> <org.mybatis.spring.boot.version>1.3.1</org.mybatis.spring.boot.version> <alibaba.druid.version>1.1.9</alibaba.druid.version> <spring.cloud.version>Edgware.SR1</spring.cloud.version> </properties> <!-- 項目依賴項 --> <dependencyManagement> <!-- dependencyManagement只是聲明依賴,並不實現引入 --> <dependencies> <!-- Spring Cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 添加junit4依賴 單元測試--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- 切面 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.8</version> </dependency> <!-- 簡化Entity代碼(實體類添加@Data,編譯時爲全部字段添加@ToString/@EqualsAndHashCode/@Getter/爲非final字段添加@Setter) --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.20</version> </dependency> <!-- 添加mybatis依賴--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${org.mybatis.spring.boot.version}</version> </dependency> <!-- 添加mysql驅動依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>${mysql.driver.version}</version> </dependency> <!-- 添加數據庫鏈接池依賴 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${alibaba.druid.version}</version> </dependency> </dependencies> <!-- <scope>主要管理依賴的部署 * compile,缺省值,適用於全部階段,會隨着項目一塊兒發佈。 * provided,相似compile,指望JDK、容器或使用者會提供這個依賴。如servlet.jar。 * runtime,只在運行時使用,如JDBC驅動,適用運行和測試階段。 * test,只在測試時使用,用於編譯和運行測試代碼。不會隨項目發佈。 * system,相似provided,須要顯式提供包含依賴的jar,Maven不會在Repository中查找它。 --> <build> <plugins> <!--打包跳過測試--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> </project>
3、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.sc</groupId> <artifactId>demo</artifactId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>common</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <!-- 依賴的版本號 --> <joda.time.version>2.9.9</joda.time.version> <commons.lang.version>2.6</commons.lang.version> <commons.io.version>2.5</commons.io.version> <fastjson.version>1.2.29.sec06</fastjson.version> <pinyin4j.version>2.5.1</pinyin4j.version> <commons.codec.version>1.9</commons.codec.version> </properties> <dependencies> <!-- 在Java中處理日期和時間是很常見的需求,基礎的工具類就是咱們熟悉的Date和Calendar,然而這些工具類的api使用並非很方便和強大,因而就誕生了Joda-Time這個專門處理日期時間的庫。--> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>${joda.time.version}</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>${commons.lang.version}</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${commons.io.version}</version> </dependency> <!-- fastJson用於對JSON格式的數據進行解析和打包 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <!-- 用於轉換拼音,調用PinyinHelper類的靜態工具方法 --> <dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>${pinyin4j.version}</version> </dependency> <!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- 用於摘要運算、編碼解碼 --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>${commons.codec.version}</version> </dependency> <!-- JPA的宗旨是爲POJO提供持久化標準規範,好比@Table註解 --> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency> <!-- thymeleaf:先後端數據交互模板引擎 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </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") List<UserEntity> getList(); 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>
4、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.sc</groupId> <artifactId>demo</artifactId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>function-one</artifactId> <packaging>jar</packaging> <dependencies> <!-- 引入本身定義的common通用包 --> <dependency> <groupId>com.sc</groupId> <artifactId>common</artifactId> <version>1.0-SNAPSHOT</version> </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); } }
5、測試
1、直接後臺測試
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 List<UserEntity> getList(){ 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{ List<UserEntity> getList(); 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 List<UserEntity> getList() { return userMapper.getList(); } @Override public UserEntity getById(Integer id) { return userMapper.getById(id); } }
4.測試
http://localhost:8666/demo/user/getList
2、先後端交互數據測試
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>hello</title> </head> <body> <h1>this is the hello.html in templates</h1> <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