<1> 学习SpringBoot,得了解它的优势所在,这也是为啥用它的原因;
(1).SpringBoot对Spring框架做了封装
(2).SpringBoot内置Tomcat服务器,启动时会自动将程序发布到Tomcat服务器
(3).SpringBoot具有自动配置功能,去除了XML配置,完全采用Java配置(注解)
(4).SpringBoot内置自动创建很多对象,例如连接池\JdbcTemplate\DispatcherServlet等,需要使用可以直接注入应用
(5).SpringBoot提供了一系列的工具包集合,便于开发引入
基础包:spring-boot-starter-parent IOC包:spring-boot-starter MVC:spring-boot-starter-web DAO:spring-boot-starter-jdbc AOP:spring-boot-starter-aop mybatis: mybatis-spring-boot-starter
<2>SpringBoot基本套路
例:用SpringBoot实现hello案例
创建maven project,在pom.xml添加boot包
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.7.RELEASE</version> </parent> <!--添加mvc包--根据层级原因,其中包含IOC包 --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
编写HelloController
@RestController //等价于@Controller+@ResponseBody public class HelloController { @RequestMapping("/hello.do") public Map<String,Object> execute(){ Map<String,Object> data = new HashMap<String, Object>(); data.put("msg", "Hello Boot"); return data; } }
编写启动Boot应用的类
@SpringBootApplication public class BootApplication { public static void main(String[] args) { SpringApplication.run(BootApplication.class, args); } }
修改tomcat端口号和请求项目名(application.properties)——<防止出现与tomcat的端口号一致而导致项目无法运行>
server.port=8888 server.context-path=/boot
5.编写JSP页面
<h1>#{msg}<h1> |
以上即为springboot的基本套路应用;