1.Maven模块
1.1步骤
- maven分模块开发:方便项目后期维护等操作。
- 拆分原则:按照分层拆分。
- 操作:
- 步骤1:创建父项目:day0307-parent
- 版本1:拷贝坐标
- 版本2:锁定坐标
- 步骤2:创建子项目:
- 通用项目: day0307-common (工具类等)
- domain项目:day0307-domain (JavaBean)
- dao项目: day0307-dao (数据访问层项目)
- service项目:day0307-service (业务逻辑层层项目)
- web项目: day0307-web (表示层项目)
- 版本1:直接使用,可以继承父项目的坐标
- 版本2:按需引入,必须要再次声明坐标
- 步骤3:拷贝代码(班级管理)
1.2版本1
1.2.1父项目
创建项目
编辑
拷贝坐标
<!-- 1 确定spring boot的版本--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> </parent> <!--2 确定版本--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <spring-cloud-release.version>Hoxton.SR3</spring-cloud-release.version> <nacos.version>1.1.0</nacos.version> <alibaba.cloud.version>2.2.1.RELEASE</alibaba.cloud.version> <mybatis.starter.version>1.3.2</mybatis.starter.version> <mapper.starter.version>2.0.2</mapper.starter.version> <pageHelper.starter.version>1.2.5</pageHelper.starter.version> <mysql.version>5.1.32</mysql.version> <durid.starter.version>1.1.10</durid.starter.version> <mybatis.plus.version>3.4.0</mybatis.plus.version> <swagger.version>2.7.0</swagger.version> <jwt.jjwt.version>0.9.0</jwt.jjwt.version> <jwt.joda.version>2.9.7</jwt.joda.version> <beanutils.version>1.9.3</beanutils.version> </properties> <!-- 3 添加依赖 --> <dependencies> <!--web开发启动器--> <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> <!-- mybatis启动器 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.starter.version}</version> </dependency> <!-- 通用Mapper启动器 --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>${mapper.starter.version}</version> </dependency> <!-- 分页助手启动器 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>${pageHelper.starter.version}</version> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- Druid连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${durid.starter.version}</version> </dependency> <!--swagger2--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--common工具--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.11</version> </dependency> </dependencies>
1.2.2子项目:通用项目
1.2.3子项目:domain
创建项目
编辑
修改坐标:添加common项目的坐标
编辑
<dependencies> <dependency> <groupId>com.czxy</groupId> <artifactId>day0307-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
1.2.4maven添加依赖
步骤1:在pom.xml文件中右键,点击“Generate”
编辑
步骤2:在新的弹出框中,点击Dependency
编辑
步骤3:搜索 包名:项目名
编辑
1.2.5子项目:dao
创建项目
编辑
添加坐标
编辑
<dependencies> <dependency> <groupId>com.czxy</groupId> <artifactId>day0307-domain</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
1.2.6子项目: service
创建项目:
编辑
添加坐标
编辑
<dependencies> <dependency> <groupId>com.czxy</groupId> <artifactId>day0307-dao</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
1.2.7子项目:web
创建项目:
编辑
坐标
编辑
1.2.8验证
编辑
1.2.9拷贝代码
编辑
编辑
编辑
编辑
1.2.10总结
- maven核心:pom.xml 、 src
- 父项目:打包方式pom、模块配置
- 编辑
子项目: - 编辑
1.3版本2:按需导入
1.3.1父项目
父项目:依赖锁定,进行版本的确定。
编辑
<!-- 3 依赖锁定 --> <!-- 3 依赖锁定 --> <dependencyManagement> <dependencies> <!--web开发启动器 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> --> <!-- mybatis启动器 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.starter.version}</version> </dependency> <!-- 通用Mapper启动器 --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>${mapper.starter.version}</version> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency> <!-- 分页助手启动器 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>${pageHelper.starter.version}</version> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- Druid连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${durid.starter.version}</version> </dependency> <!--swagger2--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency> <!--common工具--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.11</version> </dependency> <!-- 自定义项目 --> <dependency> <groupId>com.czxy</groupId> <artifactId>day0307-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.czxy</groupId> <artifactId>day0307-domain2</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.czxy</groupId> <artifactId>day0307-dao</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.czxy</groupId> <artifactId>day0307-service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </dependencyManagement>
1.3.2domain 项目
domain子项目:根据需要导入对应的坐标
domain的pom.xml文件中添加
<dependencies> <!-- lombok:@Data --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- jpa: @Table、@Id等 --> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> </dependency> <!-- json格式化:@JsonFormat --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </dependency> <!-- 自定义项目 --> <dependency> <groupId>com.czxy</groupId> <artifactId>day0307-common</artifactId> </dependency> </dependencies>
1.3.3dao项目
- 修改dao的pom文件添加坐标
<dependencies> <!-- 数据库相关的所有 --> <!-- mybatis 启动器 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- 通用Mapper启动器 --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> </dependency> <!-- 分页助手启动器 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- Druid连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> <!-- 自定义项目 --> <dependency> <groupId>com.czxy</groupId> <artifactId>day0307-domain2</artifactId> </dependency> </dependencies>
1.3.4service项目
<dependencies> <!--common工具--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <!-- 自定义项目 --> <dependency> <groupId>com.czxy</groupId> <artifactId>day0307-dao</artifactId> </dependency> </dependencies>
1.3.5web项目
<dependencies> <!--web开发启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--swagger2--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency> <!-- 自定义项目 --> <dependency> <groupId>com.czxy</groupId> <artifactId>day0307-service</artifactId> </dependency> </dependencies>
1.4总结
- maven多模块,就是一个环境搭建。
- 核心:配置pom
- 建议:多搭建几遍