springboot(一)入門(mén)篇

今天來(lái)整理下springboot,開(kāi)發(fā)工具使用eclipse(后期會(huì)再做個(gè)idea的).主要實(shí)現(xiàn)的功能是

1.通過(guò)數(shù)據(jù)庫(kù)的查詢(xún)返回前端數(shù)據(jù).

 第一步:新建marven工程,在pom文件中引入
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sunjian</groupId>
    <artifactId>springboot-jsp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <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>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

    </dependencies>

</project>

第二步:新建entity


package com.sunjian.entity;

public class UserEntity {

    private Long id;
    private String name;
    private Integer age;

    public Long getId() {

        return id;
    }

    public void setId(Long id) {

        this.id = id;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {

        this.name = name;
    }

    public Integer getAge() {

        return age;
    }

    public void setAge(Integer age) {

        this.age = age;
    }

}

第三步:新建UserMapper(需要在application.properties下面配置mysql數(shù)據(jù)源,請(qǐng)見(jiàn)3)


package com.sunjian.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.sunjian.entity.UserEntity;

public interface UserMapper {

    @Select("select * from users where id=#{id}")
    UserEntity findName(@Param("id") long id);

}

第四部:新建IndexController


package com.sunjian.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sunjian.entity.UserEntity;
import com.sunjian.mapper.UserMapper;

@Controller
public class IndexController {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/index")
    public String index() {
        int i=1/0;
        return "index";
    }

    @ResponseBody
    @RequestMapping("/getUserName")
    public UserEntity getUserName(long id) {
        return userMapper.findName(id);
    }

}

第五步:新建app啟動(dòng)類(lèi)運(yùn)行試試(注:sql已經(jīng)在下面springboot-jsp下載那里提供了)


package com.sunjian.app;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = "com.sunjian.controller")
@MapperScan(basePackages = "com.sunjian.mapper")
@EnableAutoConfiguration
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}

效果圖:

image.png

2.全局異常的捕獲

在controller中新建GlobalExceptionHandler


package com.sunjian.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @classDesc: 功能描述:(攔截異常)
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody // 攔截返回是 json返回結(jié)果
    public Map<String, Object> exceptionHandler() {
        Map<String, Object> result=new HashMap<String, Object>();
        result.put("code","500");
        result.put("msg","親,系統(tǒng)錯(cuò)誤,請(qǐng)稍后重試....");
        return result;
    }

}

效果圖

image.png

3.springboot如何返回jsp(但是springboot是不推薦使用jsp的,一般會(huì)用ftl)

首先在webapp下面新建WEB-INF,然后按照下圖新建文件


image.png

在application.properties中加入

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


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

然后測(cè)試,見(jiàn)圖

Image.png
image.png

springboot-jsp下載地址 密碼 f3pw

springboot(二)進(jìn)階篇

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 由于近期項(xiàng)目中在使用springboot,得益于它的編輯,也恰逢此時(shí)不太忙了,所以就選擇將springboot結(jié)合...
    阿亮私語(yǔ)閱讀 525評(píng)論 0 0
  • 什么是spring boot Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新S...
    余平的余_余平的平閱讀 291評(píng)論 0 0
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評(píng)論 6 342
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評(píng)論 19 139
  • 午后的陽(yáng)光,傾斜著穿透婆娑的樹(shù)葉,照在她的臉上,讓一切顯得這么清晰而又夢(mèng)幻。 她笑著,眼睛瞇了起來(lái),嘴唇微微翹起,...
    未九而久閱讀 214評(píng)論 1 2

友情鏈接更多精彩內(nèi)容