springboot(一)入门篇

今天来整理下springboot,开发工具使用eclipse(后期会再做个idea的).主要实现的功能是

1.通过数据库的查询返回前端数据.

 第一步:新建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数据源,请见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启动类运行试试(注:sql已经在下面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返回结果
    public Map<String, Object> exceptionHandler() {
        Map<String, Object> result=new HashMap<String, Object>();
        result.put("code","500");
        result.put("msg","亲,系统错误,请稍后重试....");
        return result;
    }

}

效果图

image.png

3.springboot如何返回jsp(但是springboot是不推荐使用jsp的,一般会用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

然后测试,见图

Image.png
image.png

springboot-jsp下载地址 密码 f3pw

springboot(二)进阶篇

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 由于近期项目中在使用springboot,得益于它的编辑,也恰逢此时不太忙了,所以就选择将springboot结合...
    阿亮私语阅读 3,318评论 0 0
  • 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新S...
    余平的余_余平的平阅读 1,807评论 0 0
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,040评论 6 342
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,323评论 19 139
  • 午后的阳光,倾斜着穿透婆娑的树叶,照在她的脸上,让一切显得这么清晰而又梦幻。 她笑着,眼睛眯了起来,嘴唇微微翘起,...
    未九而久阅读 1,300评论 1 2