idea创建项目
菜单 -> New -> Project -> Spring Initializr
项目参数 group artifact com.xx.xxx
选择web>springweb
建好的项目自带一个SpringbootApplication, 其被@SpringBootApplication 所标记,表示这个是一个Springboot 应用
建包 类
@RestController 是spring4里的新注解,是@ResponseBody和@Controller的缩写。 映射@RequestMapping("/xxx")
导入
idea导入项目时选中 pom.xml
部署
jar方式
打包jar
cd E:\xxx\springboot
mvn install
运行jar
java -jar target/springboot-0.0.1-SNAPSHOT.jar
就启动这个jar了通过这种方式,把此jar上传到服务器并运行,就可达到部署的效果了
-----
war方式
Application 新加@ServletComponentScan注解,并且继承SpringBootServletInitializer 。
pom.xml 修改为如下代码,主要两个改动新加打包成war的声明:
<packaging>war</packaging>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
spring-boot-starter-tomcat 修改为 provided方式,以避免和独立 tomcat 容器的冲突. 表示provided 只在编译和测试的时候使用,打包的时候就没它了。
cd C:\Users\X7TI\Downloads\springboot
mvn clean package
这样就在 target 目录下 生成了一个 springboot-0.0.1-SNAPSHOT.war 文件 重命名为 ROOT.war然后把它放进tomcat 的webapps目录下。
注意重命名时必须是ROOT.war,如果是小写root.war,那么访问路径加 /root
页面
Springboot的默认视图支持是Thymeleaf
jsp方式:
增加对jsp支持
<!-- servlet依赖. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- tomcat的支持.-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
在src/main/resources 目录下增加 application.properties文件,用于视图重定向jsp文件的位置
修改application.properties,可修改访问的端口号和上下文路径
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
server.port=8888
server.servlet.context-path=/test
#spring.mvc.servlet.path = /test
# 使用spring.mvc.servlet.path也可以
server.servlet.context-path 和 spring.mvc.servlet.path
The main difference is that setting the dispatcher servlet path allows you to register additional servlets on other paths while with context path set, spring boot's tomcat can only serve content below that context path.
设置dispatcher servle path允许您在其他路径上注册额外的servlet,当使用上下文路径context path,spring boot的tomcat只能提供上下文路径下面的内容。
如果是springboot2.0以下,用配置server.context-path=/demo
如果是springboot2.0以上,用配置server.servlet.context-path=/demo
本来的@RestController 改为@Controller。这时返回"hello"就不再是字符串,而是根据application.properties 中的视图重定向,到/WEB-INF/jsp目录下去寻找hello.jsp文件
热部署
在pom.xml中新增加一个依赖和一个插件就行了。
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
</dependency>
插件:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
idea配置好后热部署需要"Build **"或者"Rebuild"
解决方法:
idea热部署无效 spring-boot-devtools
多配置 部署
3个配置文件:核心配置文件:application.properties开发环境用的配置文件:application-dev.properties生产环境用的配置文件:application-pro.properties这样就可以通过application.properties里的 spring.profiles.active 灵活地来切换使用哪个环境
spring.profiles.active=pro
部署
java -jar target/springboot-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
在springboot里配置文件。 除了使用.properties外,springboot还支持 yml格式。YAML是一种层级格式,更易读
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
server:
port: 8888
context-path: /test
----------------
异常处理
新增加一个类GlobalExceptionHandler,用于捕捉Exception异常以及其子类。捕捉到之后,把异常信息,发出异常的地址放进ModelAndView里,然后跳转到 errorPage.jsp
----------------
- DO(Data Object):与数据库表结构一一对应,通过 DAO 层向上传输数据源对象
- DTO(Data Transfer Object):数据传输对象,Service 和 Manager 向外传输的对象
- BO(Business Object):业务对象,可以由 Service 层输出的封装业务逻辑的对象
- VO(View Object):显示层对象,通常是 Web 向模板渲染引擎层传输的对象
- PO :persistent object持久对象。
如有错误,恳请指出。