最近刚学spring boot 对于spring boot 我只有一个句话 那就是 “太便捷了” 以前对于学习新知识过于认真的我 在平时的编程中 繁琐的步骤给与我太多的阻力 如果能从一开始项目的环境搭建中解放出来那么就可以用更多的时间思考去整理所需技术 真的感谢spring boot 的开发者 也希望spring 系列能永久的辉煌下去
步骤先总结:
创建 父项目 parent(spring boot jar 记得删除 除 .idea和 pom 以外的文件) + common(maven jar项目)+dao(maven jar项目)+domain(maven jar项目)+
service(maven jar项目)+web(spring boot jar)
注意先说明:
(一切的要写的文件最好 位于 web 的webApplication 启动类 的平级或同包名下 包结构示意图)
好了 现在就创建一个spring boot ssm整合 多模块项目的父工程·
1.file >> new >> Project 注意不是Model
2. 选择 Spring Initializr 点击next
3 在page中删除springboot-parent group Artifact 参考maven 自己编写 其他都是默认的无需修改 点击next
4.这里选择版本 默认是2.0.4 其他有关依赖暂时不选 直接 点击next
5 点击next
6.点击new window 新窗口打开
7.这个时候注意了一定要选择 Enable Auto-Import 不然的话会给后面造成很大的影响 运行时各种依赖错误
8 由于是父工程删除src 右键delete
8 之后就是 common dao service web 等的创建 前三者选择Maven jar形式 而web 则需要Spring Initializr
由于前三者创建一致 这里只介绍 springboot-common的创建
common:在父工程上 右键 New 》Model
选择 Maven 点击next 注意Create from achetype 是没有勾选的
点击next
注意 Module name:框 需要于之前的Artifactld 保持一致 应为idea默认是驼峰式命名的 点击finish
这里特殊介绍下怎么去除与父工成的关联 在你选择的项目上右键找到Remove Model 点击
之后就是dao domain service 相同的步骤如法炮制
重点创建springboot-web 其实和第一个父项目基本一致 :父工程下右键New > Model
选择spring Initalizr 点击next 其他不要改动
package 写入com.czxy 点击next
版本选择 点击next
点击 finish
右下角出现 不去管它 后面步骤正确无伤大雅
在父项目的pom 中你会发现 其实 springboot-web 没有与父工程关联
所以我们进行如下步骤:
在springboot-web 下的pom 下注解掉当前<parent></parent>
从其他项目的pom下复制 <parent></parent> 到 springboot-web 下的pom
之后我们在修改父项目下的pom 引入springboot-web module
应为我们的项目是ssm项目 父项目下的pom 导入相关的启动器
<!-- spring springmvc 启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jdbc启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</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>
我们这里 是用了Maven 的版本控制 事先 覆盖掉了原先的<properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatis.starter.version>1.3.2</mybatis.starter.version>
<mapper.starter.version>2.0.2</mapper.starter.version>
<mysql.version>5.1.32</mysql.version>
<pageHelper.starter.version>1.2.5</pageHelper.starter.version>
<durid.starter.version>1.1.10</durid.starter.version>
</properties>
由于是多模块项目 相关之间也必须有依赖 这里
springboot-dao 下的pom
<dependency>
<groupId>com.czxy</groupId>
<artifactId>springboot-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.czxy</groupId>
<artifactId>springboot-domain</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
springboot-service:
<dependency>
<groupId>com.czxy</groupId>
<artifactId>springboot-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
springboot-web:
<dependency>
<groupId>com.czxy</groupId>
<artifactId>springboot-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
相关快捷键:Alt+insert
由于父工程 和 springboot-web 都是选择spring Initalizr 创建 我们运行一直是选择 springboot-web 下的SpringbootWebApplication运行
我们选择SpringbootWebApplication
运行前我们在springboot-web项目下的 application.properties 下配置 端口映射 数据库连接 druid连接池
并且在其resources 创建一个static 文件夹用来存储我们的页面
在static 下创建一个index.html
我们运行项目 倒三角处点击
正常运行结果
在Google 火狐等浏览器输入
跳转结果:
最后提出几个相关的运行错误:
这是由于找不到‘test’ 表 检查一下 :application.properties 应为大多数问题都是出自这里没有设置成功
最后是:运行时 依赖错误Settings > maven 设置一下仓库
相关的maven 版本 百度网盘地址: 链接:https://pan.baidu.com/s/1YbyPAr6JTOjebqEoTZFQlg 密码:rcrl
repository 仓库 百度网盘地址:链接:https://pan.baidu.com/s/1bAlCju4AXY0Kb2oVhLdYUw 密码:goet