SpringBoot多模块项目搭建 - 优化版
基础条件
请确保以下软件已安装并能正常使用
JDK8
Maven3.5
Mysql数据库
Idea
创建父模块
新建项目
保证是Maven工程,且为JDK8
不选任何依赖
清理多余文件
修改pom.xml
父pom必须显示声明打包方式为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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--父POM-->
<!--父模块的打包类型必须显示声明为pom,这样就不会将父模块打成jar包-->
<groupId>com.pxl</groupId>
<artifactId>tmall</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>tmall</name>
<description>父模块</description>
<packaging>pom</packaging>
<!--声明子模块-->
<modules>
</modules>
<!--公共属性-->
<properties>
<java.version>1.8</java.version>
</properties>
<!--依赖包-->
<dependencies>
</dependencies>
<build>
<plugins>
<!--maven-surefire-plugin是执行测试用例的插件,这里声明【跳过执行测试用例】-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
创建核心子模块 (mall-core)
新建项目
清理多余文件
删除多余文件后的效果如下
父模块和mall-core建立关联
在mall-core的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.pxl</groupId>
<artifactId>tmall</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--寻找parent的pom文件,隐式声明(不写此参数),则默认是../pom.xml; 显式声明,则需要指定-->
<!--<relativePath/>-->
</parent>
<!--声明当前模块-->
<groupId>com.pxl</groupId>
<artifactId>mall-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mall-core</name>
<packaging>jar</packaging>
<description>核心依赖代码,包括service/dao等</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>
</dependency>
</dependencies>
</project>
在父模块的pom中声明mall-core
<!--声明子模块-->
<modules>
<module>mall-core</module>
</modules>
整合Mybatis
引入jar包
<?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.pxl</groupId>
<artifactId>tmall</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--寻找parent的pom文件,隐式声明(不写此参数),则默认是../pom.xml; 显式声明,则需要指定-->
<!--<relativePath/>-->
</parent>
<!--声明当前模块-->
<groupId>com.pxl</groupId>
<artifactId>mall-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mall-core</name>
<packaging>jar</packaging>
<description>核心依赖代码,包括service/dao等</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>
</dependency>
<!--Mybatis & Mysql-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
编写代码
建立表 ums_resource
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for ums_resource
-- ----------------------------
DROP TABLE IF EXISTS `ums_resource`;
CREATE TABLE `ums_resource` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
`name` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '资源名称',
`url` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '资源URL',
`description` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '描述',
`category_id` bigint(20) NULL DEFAULT NULL COMMENT '资源分类ID',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 30 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '后台资源表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of ums_resource
-- ----------------------------
INSERT INTO `ums_resource` VALUES (1, '2020-02-04 17:04:55', '商品品牌管理', '/brand/**', NULL, 1);
INSERT INTO `ums_resource` VALUES (2, '2020-02-04 17:05:35', '商品属性分类管理', '/productAttribute/**', NULL, 1);
INSERT INTO `ums_resource` VALUES (3, '2020-02-04 17:06:13', '商品属性管理', '/productAttribute/**', NULL, 1);
SET FOREIGN_KEY_CHECKS = 1;
实体类 com.pxl.mall.entity.UmsMenu
package com.pxl.tmall.entity;
import lombok.Data;
import java.util.Date;
/**
* @author computer
*/
@Data
public class UmsMenu {
private int id;
private String title;
private int level;
private Date createTime;
private int parentId;
}
Mapper类 com.pxl.tmall.mapper.UmsMenuMapper
package com.pxl.tmall.mapper;
import com.pxl.tmall.entity.UmsMenu;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.Date;
import java.util.List;
/**
* @author computer
*/
public interface UmsMenuMapper {
/**
* 根据ID查找
* @param id
* @return
*/
@Select("select * from ums_menu where id = #{id}")
@Results({
@Result(property = "createTime",column = "create_time",javaType = Date.class),
@Result(property = "parentId",column = "parent_id",javaType = int.class)
})
UmsMenu findById(@Param("id") int id);
/**
* 查找所有
* @return
*/
@Select("select * from ums_menu")
@Results({
@Result(property = "createTime",column = "create_time",javaType = Date.class),
@Result(property = "parentId",column = "parent_id",javaType = int.class)
})
List<UmsMenu> findAll();
}
编写配置文件和配置类
配置文件 application.properties
以下配置是创建SqlSessionFactory所必须的
#DataSource
db.common.str=useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/tmall?${db.common.str}
spring.datasource.username=root
spring.datasource.password=root
#Mybatis
mybatis.type-aliases-package=com.pxl.tmall.entity
mybatis.mapper-locations=classpath:/mapper/*.xml
配置类 com.pxl.tmall.config.MybatisConfig
package com.pxl.tmall.config;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
/**
* @author computer
*/
@Configuration
@MapperScan("com.pxl.tmall.mapper")
public class MybatisConfig {
}
整合原理说明
SqlSessionFactory依赖于:
1. 数据源 dataSource
2. 实体所对应的包路径 typeAliasesPackage
3. xml文件所在位置 mapper-locations
以上形成如下配置:
spring.datasource.xxxxx
mybatis.mapper-locations=
mybatis.type-aliases-package
session.getMapper(xxxDao.class):
需要定位到dao接口所在位置 -- MapperScan扫描指定的dao接口所在包
以上形成如下配置:
@MapperScan("com.pxl.tmall.mapper")
验证
com.pxl.tmall.MallCoreApplication
package com.pxl.tmall;
import com.pxl.tmall.mapper.UmsMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author computer
*/
@SpringBootApplication
public class MallCoreApplication implements ApplicationRunner {
@Autowired
private UmsMenuMapper umsMenuMapper;
public static void main(String[] args) {
SpringApplication.run(MallCoreApplication.class, args);
}
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(umsMenuMapper.findAll());
}
}
成功
创建web子模块 (mall-web)
core模块,是公共的核心模块,所有不对外暴露的内容都放入core中; Web模块仅仅作为外界访问的门户,暴露接口供外部使用
新建项目
清理多余文件
父模块和mall-web建立关联
mall-web的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.pxl</groupId>
<artifactId>tmall</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--寻找parent的pom文件,隐式声明(不写此参数),则默认是../pom.xml; 显式声明,则需要指定-->
<!--<relativePath/>-->
</parent>
<!--声明当前模块-->
<groupId>com.pxl</groupId>
<artifactId>mall-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mall-web</name>
<description>Controller层,专门对外暴露接口</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--Web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--依赖核心模块-->
<dependency>
<groupId>com.pxl</groupId>
<artifactId>mall-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--编译打包相关-->
<!--多模块项目中,只有对外暴露接口的项目才保留build,其他一律删除-->
<build>
<plugins>
<!--SpringBoot项目必须加入这个插件,才会将普通JAR打成SpringBoot的JAR-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--指定Main Class为全局的唯一入口-->
<mainClass>com.pxl.tmall.MallWebApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
父模块的pom修改
<!--声明子模块-->
<modules>
<module>mall-core</module>
<module>mall-web</module>
</modules>
编写配置文件
#引入core模块的配置
#spring profiles setting
#springboot2.4及之前版本,使用spring.profiles.include=[core-dev]
#官方文档:https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4
spring.config.import=classpath:application-core-dev.properties
编写控制器类
package com.pxl.tmall.controller;
import com.pxl.tmall.mapper.UmsMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author computer
*/
@RestController
public class WebTest {
@Autowired
private UmsMenuMapper umsMenuMapper;
/**
* 获取所有Menu信息
* @return
*/
@RequestMapping(value = "/getAllMenu",produces = "application/json")
public String getAllMenu(){
return umsMenuMapper.findAll().toString();
}
}
页面效果
源代码
https://gitee.com/pengxiaoliang/tmall
注意事项
1.父pom.xml 打包方式,jar要更改为pom,build 需要更改
2.不需要打包的模块pom.xml文件中不要写<build>,全删掉,例如有些工程中的common模块,utils模块,entity模块,service模 块都不需要打包
3.声明父工程时,填写父工程位置<relativePath>../pom.xml</relativePath>
4.关于applicatin.properties配置文件,只需要在启动的模块中配置就可以了;在core模块中将properties文件名改为application-core-dev.properties,存放公共模块所使用的配置,在启动模块(web模块)中,application.properties 可以使用 如下方式引入:
#spring profiles setting
#springboot2.4及之前版本,使用spring.profiles.include=[core-dev]
#官方文档:https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4
spring.config.import=classpath:application-core-dev.properties
5.关于打包为什么打包jar包,不打war包,打war包目的是war包可以运行在tomcat下,但是SpringBoot是内置tomcat,如果你打war包,前提是干掉内置的tomcat,然后才能打包,各种麻烦,直接打包可执行jar包,使用java -jar 命令就可以完美的运行起来很方便!
————————————————
版权声明:本文为CSDN博主「凌云冷海」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/baidu_41885330/article/details/81875395
本文来自博客园,作者:江南西道,转载请注明原文链接:https://www.cnblogs.com/jiangnanxidao/p/15782850.html