注意: jdk必须1.8java
Finish 完成建立mysql
因为springboot先天性集成了spring/springmvc,因此咱们不须要编写相关配置文件,直接开发便可。git
Springboot的核心配置文件主要是来修改一些基本配置(服务器端口号、项目访问名字)以及集成配置(数据源、mybatis、redis等等)程序员
Springboot的核心配置文件分为两种形式:(注意:名字都必须为application)github
Springboot的application.properties/application.yml 文件还能够配置一些自定义属性,用来给对象属性赋值,方便代码维护,以及解耦合web
应用场景:springMvc文件上传,定义全局文件夹路径。redis
方式一:经过@Value(“${upload.imagePath}”)spring
方式二:定义通用映射解析sql
com.sam.name=sam
com.sam.age=11
com.sam.desc=magical sam
Sam类须要添加@Component注解,让spring在启动的时候扫描到该类,并添加到spring容器中。后端
package com.sam.demo.conf; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * @author sam * @since 2017/7/15 */ @Component public class Sam { //获取application.properties的属性 @Value("${com.sam.name}") private String name; @Value("${com.sam.age}") private int age; @Value("${com.sam.desc}") private String desc; //getter & setter }
package com.sam.demo.conf; import org.springframework.stereotype.Component; /** * @author sam * @since 2017/7/15 */ @Component @ConfigurationProperties(prefix = "com.sam") public class Sam { private String name; private int age; private String desc; //getter & setter }
package com.sam.demo.controller; import com.sam.demo.conf.Sam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author sam * @since 2017/7/14 */ @RestController public class IndexController { @Autowired private Sam sam; @RequestMapping("/index") public String index() { System.out.println(sam.getName() + " " + sam.getAge() + " " + sam.getDesc()); return "index"; } }
在实际开发过程当中,不一样的开发环境,咱们使用的配置信息也是不同的,这关系到链接池配置、工厂配置等等
例如:
生产环境(线上环境)
开发环境(线下环境)
测试环境(单元测试)
不一样的环境咱们要指定不一样的配置文件,相对springboot给咱们提供了这样的便利。
注意:也能够在application.properties总配置文件中引入application-dev.properties(开发)或application-prod.properties(生产)或application-test.properties(测试) 达到动态切换环境的目的
application.properties:
<!--mybatis 依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.10</version> </dependency> <!-- 分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> <!--链接池配置--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.31</version> </dependency> <!--log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
3.Demo测试
在启动时 在启动类上经过@ImportResource(“classpath:shiro-config.xml”),初始化shiro配置
4.启动测试
注意: 若是在访问jsp的过程当中 发生下载现象
SpringBoot不推荐使用JSP做为View,而是推荐咱们使用模板(如:thymeleaf、freemarker等模板引擎),缘由以下:
1. JSP性能较差
2. 绝对的先后端分离思想,JSP并不利于页面调试(运行依赖于web容器)
3. SpringBoot对内嵌web容器的支持默认也是用tomcat。但tomcat对web资源的处理上写死了要使用文件目录,对于打包成jar包的SpringBoot
应用来讲,显然不行,也有的人打包成war,而后仍是部署到tomcat上,这样违反了SpringBoot的初衷,这样一来,等于否认了嵌入式容
器,并且程序员还要处理嵌入式环境和外部tomcat环境的不一样带来的问题。