SpringBoot-Maven多模組開發測試
靜下心來,寫下人生的第一篇部落格。
(小序:文筆不好,基礎Demo,大神請略過哈~~)
之前習慣用了eclipse,最近轉到IDEA後,給我最大的差別就是:
eclipse有workspace的概念,而IDEA最頂層是project;這就導致了eclipse可以在同一視窗開啟多個project,而IDEA只能開啟一個project。後來學習找到可以通過新建一個空project的maven專案,然後新增多個Modules來實現同時開啟多個project。閒話不多說,寫個小Demo記錄一下:
一、開發環境:
- IDEA;
- JDK1.8
- Maven-3.3.9
- SpringBoot-1.4.1
二、專案目錄截圖:
三、專案建立步驟:
(1)建立一個maven的project專案,File -> New -> Project,選擇建立maven;名稱命名為:SpringBootMutil,因為該專案只是作為聚合專案的父專案,所以刪除src檔案,保留pom.xml即可,並將打包方式改成pom。
(2)右鍵父專案名,New -> Module,依次建立各個子Module,方便識別,名稱命名為SpringBootMutil-*的形式,打包方式為jar包。
(3)在父專案中新增依賴的版本資訊,以及公共依賴等,最後的父pom檔案:
父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.lzz</groupId> <artifactId>SpringBootMutil</artifactId> <version>1.0-SNAPSHOT</version> <!--打包方式為pom --> <packaging>pom</packaging> <modules> <module>SpringBootMutil-Service</module> <module>SpringBootMutil-Mapper</module> <module>SpringBootMutil-Domain</module> <module>SpringBootMutil-Common</module> <module>SpringBootMutil-Controller</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.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> <druid.version>1.0.9</druid.version> <mybatis.boot.starter.version>1.1.1</mybatis.boot.starter.version> <mysql.connector.java.version>5.1.32</mysql.connector.java.version> </properties> <!-- 依賴版本控制 --> <dependencyManagement> <dependencies> <!-- mybatis的依賴 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.boot.starter.version}</version> </dependency> <!-- 新增MySQL資料庫驅動依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.connector.java.version}</version> </dependency> <!-- 資料庫連線池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> </dependencies> </dependencyManagement> <!-- 公共依賴 --> <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> </dependency> </dependencies> <!--指定使用maven打包--> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <configuration> <skipTests>true</skipTests><!--預設關掉單元測試 --> </configuration> </plugin> </plugins> </build> </project>
(4)按照專案命名規則,子Module之間的依賴關係:
- Controller依賴Common、Domain、Service;
- Service依賴Common、Domain、Mapper;
- Mapper依賴Common、Domain;
- Common為公共工具類專案,因此不依賴其他Module。
[1] Common的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> <artifactId>SpringBootMutil-Common</artifactId> <packaging>jar</packaging> <parent> <artifactId>SpringBootMutil</artifactId> <groupId>com.lzz</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> </project>
[2] Domain的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> <artifactId>SpringBootMutil-Domain</artifactId> <packaging>jar</packaging> <parent> <artifactId>SpringBootMutil</artifactId> <groupId>com.lzz</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> </project>
[3] Mapper的pom.xml
注意:在Mapper專案中,要引入mybatis和資料庫的依賴
<?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> <artifactId>SpringBootMutil-Mapper</artifactId> <packaging>jar</packaging> <parent> <artifactId>SpringBootMutil</artifactId> <groupId>com.lzz</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <dependencies> <!-- 內部依賴 --> <dependency> <groupId>com.lzz</groupId> <artifactId>SpringBootMutil-Common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.lzz</groupId> <artifactId>SpringBootMutil-Domain</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- 外部依賴 --> <!-- mybatis的依賴 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- 新增MySQL資料庫驅動依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 資料庫連線池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> </dependencies> </project>
[4] Service的pom.xml
注意:在service專案中引入事務控制的依賴
<?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> <artifactId>SpringBootMutil-Service</artifactId> <packaging>jar</packaging> <parent> <artifactId>SpringBootMutil</artifactId> <groupId>com.lzz</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <dependencies> <!-- 內部依賴 --> <dependency> <groupId>com.lzz</groupId> <artifactId>SpringBootMutil-Common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.lzz</groupId> <artifactId>SpringBootMutil-Domain</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.lzz</groupId> <artifactId>SpringBootMutil-Mapper</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- 外部依賴 --> <!-- 事務管理需要引入該jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> </dependencies> </project>
[5] Controller的pom.xml
注意:配置maven外掛,指定唯一的main方法的入口
<?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> <artifactId>SpringBootMutil-Controller</artifactId> <packaging>jar</packaging> <parent> <artifactId>SpringBootMutil</artifactId> <groupId>com.lzz</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <dependencies> <!-- 內部依賴 --> <dependency> <groupId>com.lzz</groupId> <artifactId>SpringBootMutil-Common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.lzz</groupId> <artifactId>SpringBootMutil-Domain</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.lzz</groupId> <artifactId>SpringBootMutil-Service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <!--spring boot打包的話需要指定一個唯一的入門--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- 指定該Main Class為全域性的唯一入口 --> <mainClass>com.lzz.SpringBootMutilApp</mainClass> <layout>ZIP</layout> </configuration> <executions> <execution> <!--可以把依賴的包都打包到生成的Jar包中--> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
最後,將各個專案打包,(這個地方容易出現package失敗的情況),最好是install到本地倉庫
四、編寫演示Demo
(1)在Common專案中編寫一個工具類:
package com.lzz.utils; import java.util.UUID; /** * Author lzz * Date 2018/5/23 */ public class IDUtils { public static String uuid() { return UUID.randomUUID().toString().replace("-", "").toUpperCase(); } }(2)在Domain專案中編寫一個pojo:
package com.lzz.pojo; /** * Author lzz * Date 2018/5/23 */ public class User { private String id; private String username; private String password; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
(3)在Mapper專案中編寫相應的mapper介面和對映xml
package com.lzz.mapper; import com.lzz.pojo.User; import java.util.List; /** * Author lzz * Date 2018/5/23 */ public interface UserMapper { void insert(User user); List<User> findAll(); }
<?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="com.lzz.mapper.UserMapper"> <select id="findAll" resultType="com.lzz.pojo.User"> select * from user </select> <insert id="insert" parameterType="com.lzz.pojo.User"> insert into user(username, password, id) values(#{username},#{password},#{id}) </insert> </mapper>
(4)在Service專案中編寫介面以及實現類(加入事務控制)
package com.lzz.service; import com.lzz.pojo.User; import java.util.List; /** * Author lzz * Date 2018/5/23 */ public interface UserService { void insert(User user); List<User> findAll(); }
package com.lzz.service.impl; import com.lzz.mapper.UserMapper; import com.lzz.pojo.User; import com.lzz.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; /** * Author lzz * Date 2018/5/23 */ @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override @Transactional(rollbackFor = Exception.class) public void insert(User user) { userMapper.insert(user); } @Override public List<User> findAll() { return userMapper.findAll(); } }
(5)在Controller專案中編寫呼叫介面(演示方法,使用靜態資料模擬)
package com.lzz.controller; import com.lzz.pojo.User; import com.lzz.service.UserService; import com.lzz.utils.IDUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * Author lzz * Date 2018/5/23 */ @RestController public class UserController { @Autowired private UserService userService; @RequestMapping(value = "findAll", method = RequestMethod.GET) public List<User> findAll() { return userService.findAll(); } @RequestMapping(value = "/insert", method = RequestMethod.GET) public String insert() { User user = new User(); user.setId(IDUtils.uuid()); user.setUsername("張三"); user.setPassword("123"); userService.insert(user); return "insert success"; } }
五、啟動類編寫(Controller專案中)
本專案基於SpringBoot,使用application.yml檔案管理
(1)application.yml配置:
####################################################################### server: port: 8080 context-path: / logging: level: debug ####################################################################### spring: datasource: url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver password: 123 username: root dbcp2: min-idle: 5 # 資料庫連線池的最小維持連線數 initial-size: 10 # 初始化提供的連線數 max-total: 10 # 最大的連線數 max-wait-millis: 200 # 等待連接獲取的最大超時時間 ####################################################################### mybatis: config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mapper/*.xml type-aliases-package: com.lzz.pojo
(2)app啟動類:
package com.lzz; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; /** * Author lzz * Date 2018/5/23 */ @RestController @MapperScan("com.lzz.mapper") @SpringBootApplication public class SpringBootMutilApp { public static void main(String[] args) { SpringApplication.run(SpringBootMutilApp.class, args); } @RequestMapping(value = "/", method=RequestMethod.GET) public Map<String, Object> showIndex() { Map<String, Object> map = new HashMap<>(); map.put("Project", "SpringBootMutil"); map.put("Author", "lzz"); map.put("Date", "2018-05-23"); return map; } }
(3)編寫了一個測試類,測試下資料庫連通性
package com.lzz.test; import com.lzz.SpringBootMutilApp; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import javax.annotation.Resource; import javax.sql.DataSource; /** * Author lzz * Date 2018/5/24 */ @SpringBootTest(classes = SpringBootMutilApp.class) @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration public class DataSourceTest { @Resource private DataSource dataSource; @Test public void testConnection() { System.out.println(this.dataSource); } }
測試結果:成功
----------------------------------------------------------------------------------------------------------------------------------
2018-05-24 16:53:09.775 INFO 7588 --- [ main] com.lzz.test.DataSourceTest : Started DataSourceTest in 3.778 seconds (JVM running for 4.593)
{
CreateTime:"2018-05-24 16:53:08",
ActiveCount:0,
PoolingCount:0,
CreateCount:0,
DestroyCount:0,
CloseCount:0,
ConnectCount:0,
Connections:[
]
}
2018-05-24 16:53:09.819 INFO 7588 --- [ Thread-1] o.s.w.c.s.GenericWebApplicationContext : Closing org.s[email protected]624ea235: startup date [Thu May 24 16:53:06 CST 2018]; root of context hierarchy
Process finished with exit code 0
----------------------------------------------------------------------------------------------------------------------------------
然後啟動main函式,進入首頁
測試成功!
然後測試下Controller,測試成功!
到資料庫檢視,一切OK!
六、總結
IDEA建立多專案:
主要是使用maven的module模組進行管理,但專案之間沒有必然聯絡的時候不適宜用module,
比如說像eclipse中的workspace顯式方式:
IDEA目前還在學習中,有了解這塊的小夥伴可以留言哈~~