美文网首页计算机微刊
springboot(一)入门篇

springboot(一)入门篇

作者: zhuyuansj | 来源:发表于2017-10-06 18:16 被阅读17次

今天来整理下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(二)进阶篇
关注我的公众号,都是满满的干货!

孙坚.gif

相关文章

  • SpringBoot

    SpringBoot (一) :入门篇 SpringBoot(二) :web综合开发 SpringBoot(三) ...

  • springboot(二)进阶篇

    继之前的springboot(一)入门篇 ,本次主要讲springboot的一些其他特性,springboot内容...

  • 纯洁的微笑的springboot摘要

    入门篇 什么是springboot 使用springboot的好处 快速构建 项目结构 引入 web模块 编写co...

  • 优质资源汇总【于2019年】

    1、SpringBoot 干货 已完成: Spring Boot干货系列:(一)优雅的入门篇 Spring Boo...

  • springboot(一):入门篇

    什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新S...

  • SpringBoot (一) :入门篇

    什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新S...

  • SpringBoot (一) :入门篇

    由于近期项目中在使用springboot,得益于它的编辑,也恰逢此时不太忙了,所以就选择将springboot结合...

  • springboot(一)入门篇

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

  • SpringBoot(一)入门篇

    一、思考 项目上刚好用到Spring boot +Spring Cloud 做微服务,于是开启了Spring bo...

  • springboot入门篇(一)

    一、什么是 Spring Boot? Spring Boot 是 Spring 开源组织下的子项目,是 Sprin...

网友评论

  • 知识学者:东西没有多少,配置看起来有点小麻烦啊。
    zhuyuansj:很快的.springboot思想是约定大于配置,大多是用注解开发.所谓的配置也就一个application.properties.其他基本都是注解.和springmvc比起来简直就是前端的vue.js和react.js的差别了(前端稍微了解点,不一定对).

本文标题:springboot(一)入门篇

本文链接:https://www.haomeiwen.com/subject/nhjryxtx.html