介绍
SpringBoot官网:
https://spring.io/projects/spring-boot
SpringBoot百度百科:
https://baike.baidu.com/item/Spring%20Boot/20249767?fr=aladdin
Spring Boot由Pivotal团队提供,设计目的是简化Spring应用的初始搭建以及开发过程;该框架使用特定方式进行配置,使开发人员不再需要定义样板化的配置;通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
特点:
简化Maven配置;
嵌入的tomcat,无需war包部署;
自动配置Spring;
提供生产就绪型功能,如指标,健康检查和外部配置;
项目
开发环境
JDK8+Eclipse
Mysql+Navicat
Maven(3.3.9)+SpringBoot(1.5.4)+Mybatis(3.4.4)
建表
DROP TABLE IF EXISTS
user
;
CREATE TABLEuser
(
ID
int(11) NOT NULL,
NAME
varchar(30) DEFAULT NULL,
BIRTHDAY
datetime DEFAULT NULL,
ADDRESS
varchar(200) DEFAULT NULL,
PRIMARY KEY (ID
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
增加数据
INSERT INTO USER ( ID, NAME, BIRTHDAY, ADDRESS )
VALUES
( 1, ‘夏言’, ‘1573-01-01’,‘桂州村’ ),
( 2, ‘严嵩’,‘1587-01-01’, ‘分宜县城介桥村’),
(3,‘徐阶’, ‘1580-01-01’,‘明松江府华亭县’ ),
( 4, ‘高拱’,‘1566-01-01’, ‘河南省新郑市高老庄村’),
( 5,‘张居正’, ‘1558-01-01’,‘江陵’ );
新建Maven工程
创建simple project,类型为jar
修改pom.xml文件
引入父工程:控制jar包版本
定义JDK及字符集
添加依赖及插件
<!-- 父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<!-- 定义JDK及字符集 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web服务 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 增加plugin插件,忘记加会报“清单找不到”异常。 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
修改完pom文件,执行Maven的Update Project
新建实体类User
package com.twd.pojo;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
public class User {
private Integer id;
private String name;
private Date birthday;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
新建映射文件UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.twd.mapper.UserMapper">
<select id="find" resultType="User">
select * from user
</select>
</mapper>
新建映射接口UserMapper
package com.twd.mapper;
import java.util.List;
import com.twd.pojo.User;
public interface UserMapper {
public List<User> find();
}
新建服务层接口UserService
package com.twd.service;
import java.util.List;
import com.twd.pojo.User;
public interface UserService {
public List<User> find();
}
新建服务层实现类UserServiceImpl
package com.twd.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.twd.mapper.UserMapper;
import com.twd.pojo.User;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper usermapper;
public List<User> find(){
return usermapper.find();
}
}
新建控制层UserController
package com.twd.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.twd.pojo.User;
import com.twd.service.UserService;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userservice;
@RequestMapping("/find")
public List<User> find(){
return userservice.find();
}
}
新建配置文件application.yml
全局配置文件,yml为新的配置文件方式,注意其中格式为空格,不能有tab;
配置端口,配置数据源,配置mybatis全局配置;
注意:如果端口,启动时日志显示8080,说明此文件未加载。检查原因一般是文件名或者路径不正确。
server:
port: 8070
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mybatisdb
username: root
password: root
mybatis:
typeAliasesPackage: com.twd.pojo
mapperLocations: classpath:mappers/*.xml
新建启动类
package com.twd;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.twd.mapper")
public class RunApplication {
public static void main(String[] args) {
SpringApplication.run(RunApplication.class, args);
}
}
执行启动类,浏览器访问 http://localhost:8070/user/find