今天來整理下springboot,開發(fā)工具使用eclipse(后期會再做個idea的).主要實現(xiàn)的功能是
1.通過數(shù)據(jù)庫的查詢返回前端數(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ù)源,請見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已經(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);
}
}
效果圖:
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)錯誤,請稍后重試....");
return result;
}
}
效果圖
3.springboot如何返回jsp(但是springboot是不推薦使用jsp的,一般會用ftl)
首先在webapp下面新建WEB-INF,然后按照下圖新建文件
在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
然后測試侨把,見圖
springboot-jsp下載地址 密碼 f3pw