springboot

一、了解

1.敏捷开发(整合框架)

2.无须tomcat(java应用程序运行,实际jar包),内置tomcat

3.减少xml配置文件,配置properties

4.springcloud+springboot微服务开发,基于http协议,restful规范

二、创建工程

创建maven工程,引入依赖,通过maven继承实现找到所有jar包

 <!-- spring boot 的核心依赖 -->
 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <dependencies>
         <!-- spring boot web组件 -->
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 </dependencies>

三、测试实例

1.新建测试类,并使用java应用程序运行。

/*@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
1) 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
2) 如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
    如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。*/
@EnableAutoConfiguration
@RestController
public class ApplicationStartTest {

    @RequestMapping("/index")
    public String index(){
        return "success";
    }
    
    @RequestMapping("/getMap")
    public Map getMap(){
        Map map = new HashMap<String,Object>();
        map.put("result", "success");
        return map;
    }
    
    public static void main(String[] args) {
        //springboot项目入口
        SpringApplication.run(ApplicationStartTest.class, args);
    }
}

2.当程序入口在当前类时,只能扫描到当前类的注解。可以新建一个app类,专门用来启动项目,但是当扫描包内有多个main方法时,项目是启动失败的。

@ComponentScan("com.shuaibiao")
@EnableAutoConfiguration
public class App {

    public static void main(String[] args) {
        //springboot项目入口
        SpringApplication.run(App.class, args);
    }
}

四、springboot的全局捕获异常

/**
 * 全局捕获异常类
 * @author yin.jinbiao
 *
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * RuntimeException表示拦截运行时异常,基于aop五大通知中的异常通知
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Map<String,Object> resultError(){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("errorCode", "500");
        map.put("errorMsg", "系统错误");
        return map;
    }
    
}

五、引入freemarker

首先在pom文件中引入依赖。

<!-- freemarker的依赖 -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
      </dependency>

然后在resources文件夹下创建templates,freemarker默认找这个文件夹,创建一个模板文件index.ftl

this is freemarker test.
${result}

书写测试类,FreemarkerTest

@Controller
public class FreemarkerTest {

    @RequestMapping("/freemarkerTest")
    public String index(Map<String,Object> map){
        map.put("result", "Hello,Freemarker!");
        return "index";
    }
    
}

 六、springboot整合jsp

现在springboot一般使用freemarker来进行页面渲染,一般不使用jsp。了解一下,jsp的整合。

首先创建工程的时候,需要将打包方式设置为war类型。在pom文件中加入依赖。

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>
    <dependencies>
        <!-- SpringBoot 核心组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
   </dependencies>

然后配置springmvc视图解析器的配置,在resources文件夹下新建application.properties

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

return "index";就会找到webapp/WEB-INF/jsp/index.jsp。

七、整合jdbcTemplate

加入数据库驱动依赖和jdbcTemplate依赖

<!-- mysql驱动 -->
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!-- jdbc模版 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
在application.properties中加入,连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

然后直接使用自动注入jdbcTemplate增删改査。

 
posted @ 2019-03-22 16:42  三笠丶阿克曼  阅读(209)  评论(0编辑  收藏  举报