1.创建父级模块:file-new-project-maven-next,打包方式pom
2.创建子模块,右键父模块new-Module,(web模块与其他模块有区别),先web模块创建new-Module-spring initializr-next
3.其他子模块的创建,比较简单不用勾选选项,一路next即可
最后结构如下:
可删掉多余文件。
各个模块的pom.xml,依赖关系,
父级只是管理整个模块,统一版本号,提供公共jar,打包方式pom
web控制层依赖service,连接前端和后端主要模块,数据库配置我也放这里,打包方式jar,也可以war
base实体类模块,不依赖谁,可继承父模块,打包方式jar
service服务层依赖dao(mapper),utils,打包方式jar
dao(mapper)依赖base,打包方式jar
utils工具类模块,暂不依赖谁,可继承父模块,打包方式jar
pom文件:
boot-parent的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.bingo</groupId>
<artifactId>boot-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<!--打包方式pom-->
<packaging>pom</packaging>
<!--管理的所有模块-->
<modules>
<module>boot-base</module>
<module>boot-dao</module>
<module>boot-service</module>
<module>boot-utils</module>
<module>boot-web</module>
</modules>
<!--指定项目的spring boot的版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--统一配置所需jar的版本,方便管理-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!--默认关掉单元测试 -->
<skipTests>true</skipTests>
<pagehelper.version>1.2.5</pagehelper.version>
<druid.version>1.1.9</druid.version>
<mysql-version>5.1.46</mysql-version>
<commons-lang3-version>3.4</commons-lang3-version>
<commons-pool2-version>2.5.0</commons-pool2-version>
<fastjson-version>1.2.47</fastjson-version>
</properties>
<!--只是声明依赖方便统一版本号,并不实现引入,因此子项目需要显示的声明需要用的依赖,子类不用加version除非插件-->
<dependencyManagement>
<dependencies>
<!--管理模块版本,这样其他模块引入就不用加版本号-->
<dependency>
<groupId>com.bingo</groupId>
<artifactId>boot-utils</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.bingo</groupId>
<artifactId>boot-base</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.bingo</groupId>
<artifactId>boot-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.bingo</groupId>
<artifactId>boot-dao</artifactId>
<version>${project.version}</version>
</dependency>
<!-- 分页插件pagehelper自动依赖mybatis -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3-version}</version>
</dependency>
<!--spring2.0集成redis所需common-pool2-->
<!-- 必须加上,jedis依赖此 -->
<!-- spring boot 2.0 的操作手册有标注 大家可以去看看 地址是:https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>${commons-pool2-version}</version>
</dependency>
<!-- 将作为Redis对象序列化器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!--指定项目中公有的依赖,所有的模块不管是否调用都自动添加-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
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>
<groupId>com.bingo</groupId>
<artifactId>boot-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-web</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<!--继承父模块,需要注意的是,这里要指定父模块pom.xml的相对路径-->
<parent>
<groupId>com.bingo</groupId>
<artifactId>boot-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!--其他模块的依赖-->
<dependency>
<groupId>com.bingo</groupId>
<artifactId>boot-service</artifactId>
</dependency>
<!--其他jar-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<!--<build>-->
<!--<plugins>-->
<!--<plugin>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-maven-plugin</artifactId>-->
<!--<configuration>-->
<!--<mainClass>com.bingo.BootWebApplication</mainClass>-->
<!--<includeSystemScope>true</includeSystemScope>-->
<!--</configuration>-->
<!--<executions>-->
<!--<execution>-->
<!--<goals>-->
<!--<goal>repackage</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
<!--</plugin>-->
<!--</plugins>-->
<!--</build>-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
service的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>
<groupId>com.bingo</groupId>
<artifactId>boot-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-service</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<!--继承父模块,需要注意的是,这里要指定父模块pom.xml的相对路径-->
<parent>
<groupId>com.bingo</groupId>
<artifactId>boot-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!--其他模块的依赖-->
<dependency>
<groupId>com.bingo</groupId>
<artifactId>boot-dao</artifactId>
</dependency>
<dependency>
<groupId>com.bingo</groupId>
<artifactId>boot-utils</artifactId>
</dependency>
<!-- 分页插件pagehelper自动依赖mybatis -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<!--<build>-->
<!--<plugins>-->
<!--<plugin>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-maven-plugin</artifactId>-->
<!--<configuration>-->
<!--<mainClass>com.bingo.BootWebApplication</mainClass>-->
<!--<includeSystemScope>true</includeSystemScope>-->
<!--</configuration>-->
<!--<executions>-->
<!--<execution>-->
<!--<goals>-->
<!--<goal>repackage</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
<!--</plugin>-->
<!--</plugins>-->
<!--</build>-->
</project>
web项目启动入口:
package com.bingo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.bingo.dao")//将项目中对应的mapper类的路径加进来就可以了
@SpringBootApplication
public class BootWebApplication {
public static void main(String[] args) {
SpringApplication.run(BootWebApplication.class, args);
}
}
web项目属性文件:application.yml
server:
port: 8083
spring:
datasource:
name: demo_test
type: com.alibaba.druid.pool.DruidDataSource
#druid相关配置
druid:
#监控统计拦截的filters
filters: stat
driver-class-name: com.mysql.jdbc.Driver
#基本属性
url: jdbc:mysql://192.168.1.108:3306/demo_test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=true
username: root
password: root
#配置初始化大小/最小/最大
initial-size: 1
min-idle: 1
max-active: 20
#获取连接等待超时时间
max-wait: 60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
time-between-eviction-runs-millis: 60000
#一个连接在池中最小生存的时间
min-evictable-idle-time-millis: 300000
validation-query: SELECT 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
#打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: 20
mybatis:
mapper-locations: classpath:mapping/*.xml
type-aliases-package: com.bingo.base
#pagehelper
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
dao:
UserServiceImpl:
package com.bingo.service.impl;
import com.bingo.dao.UserMapper;
import com.bingo.domain.User;
import com.bingo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
public PageInfo findAllUser(int pageNum, int pageSize) {
//将参数传给这个方法就可以实现物理分页了,非常简单。
PageHelper.startPage(pageNum, pageSize);
List<User> users = userMapper.listInfo();
PageInfo result = new PageInfo(users);
return result;
}
}