SpringBoot实现Restful服务,基于HTTP-JSON传输

在这里插入图片描述

一,数据库准备

1)创建数据库

1
CREATE DATABASE springbootdb;

2)创建表

1
2
3
4
5
6
7
8
DROP TABLE IF EXISTS  `city`;
CREATE TABLE `city` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '城市编号',
`province_id` int(10) unsigned NOT NULL COMMENT '省份编号',
`city_name` varchar(25) DEFAULT NULL COMMENT '城市名称',
`description` varchar(25) DEFAULT NULL COMMENT '描述',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

3)插入数据

1
INSERT city VALUES (1001 ,110,'福建','今天很开心');

二,项目结构

项目

项目结构解读

  • controller控制层
  • dao接口层
  • domain实体类
  • service业务逻辑接口类
  • service impl 业务逻辑实现类
  • application.properties 应用配置文件,应用启动会自动读取配置

代码实现

实体层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* 城市实体类
*
* Created by ws on 10/12/2018.
*
*/

public class City {

/**
* 城市编号
*/

private Long id;

/**
* 省份编号
*/

private Long provinceId;

/**
* 城市名称
*/

private String cityName;

/**
* 描述
*/

private String description;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getProvinceId() {
return provinceId;
}

public void setProvinceId(Long provinceId) {
this.provinceId = provinceId;
}

public String getCityName() {
return cityName;
}

public void setCityName(String cityName) {
this.cityName = cityName;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
}

接口层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* 城市 DAO 接口类
*
*Created by ws on 10/12/2018.
*/

public interface CityDao {

/**
* 获取城市信息列表
*
* @return
*/

List<City> findAllCity();

/**
* 根据城市 ID,获取城市信息
*
* @param id
* @return
*/

City findById(@Param("id") Long id);

Long saveCity(City city);

Long updateCity(City city);

Long deleteCity(Long id);
}

接口对应的mapper层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?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="org.spring.springboot.dao.CityDao">
<resultMap id="BaseResultMap" type="org.spring.springboot.domain.City">
<result column="id" property="id" />
<result column="province_id" property="provinceId" />
<result column="city_name" property="cityName" />
<result column="description" property="description" />
</resultMap>

<parameterMap id="City" type="org.spring.springboot.domain.City"/>

<sql id="Base_Column_List">
id, province_id, city_name, description
</sql>

<select id="findById" resultMap="BaseResultMap" parameterType="java.lang.Long">
select
<include refid="Base_Column_List" />
from city
where id = #
{id}
</select>

<select id="findAllCity" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from city
</select>

<insert id="saveCity" parameterMap="City" useGeneratedKeys="true" keyProperty="id">
insert into
city(id,province_id,city_name,description)
values
(#
{id},#{provinceId},#{cityName},#{description})
</insert>

<update id="updateCity" parameterMap="City">
update
city
set
<if test="provinceId!=null">
province_id = #
{provinceId},
</if>
<if test="cityName!=null">
city_name = #
{cityName},
</if>
<if test="description!=null">
description = #
{description}
</if>
where
id = #
{id}
</update>

<delete id="deleteCity" parameterType="java.lang.Long">
delete from
city
where
id = #
{id}
</delete>
</mapper>

业务逻辑接口层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* 城市业务逻辑接口类
*
* Created by ws on 10/12/2018.
*/

public interface CityService {

/**
* 获取城市信息列表
*
* @return
*/

List<City> findAllCity();

/**
* 根据城市 ID,查询城市信息
*
* @param id
* @return
*/

City findCityById(Long id);

/**
* 新增城市信息
*
* @param city
* @return
*/

Long saveCity(City city);

/**
* 更新城市信息
*
* @param city
* @return
*/

Long updateCity(City city);

/**
* 根据城市 ID,删除城市信息
*
* @param id
* @return
*/

Long deleteCity(Long id);
}

业务逻辑接口实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* 城市业务逻辑实现类
*
* Created by ws on 10/12/2018.
*/

@Service
public class CityServiceImpl implements CityService {

@Autowired
private CityDao cityDao;

/**
* 查询所有
* @return
*/

public List<City> findAllCity(){
return cityDao.findAllCity();
}

/**
* 根据id查找相关
* @param id
* @return
*/

public City findCityById(Long id) {
return cityDao.findById(id);
}

/**
* 新增
* @param city
* @return
*/

@Override
public Long saveCity(City city) {
return cityDao.saveCity(city);
}

/**
*修改
* @param city
* @return
*/

@Override
public Long updateCity(City city) {
return cityDao.updateCity(city);
}

/**
* 删除
* @param id
* @return
*/

@Override
public Long deleteCity(Long id) {
return cityDao.deleteCity(id);
}

}

controller层代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
**
* 城市 Controller 实现 Restful HTTP 服务
*
* Created by ws on 10/12/2018.
*/
@RestController
@RequestMapping(value = "/api/city")
public class CityRestController {

@Autowired
private CityService cityService;

/**
* 根据id来查找
* @param id
* @return
*/

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public City findOneCity(@PathVariable("id") Long id) {
return cityService.findCityById(id);
}

/**
* 查询所有
* @return
*/

@RequestMapping(value = "/", method = RequestMethod.GET)
public List<City> findAllCity() {
return cityService.findAllCity();
}

/**
* 新增
* @param city
*/

@RequestMapping(value = "/", method = RequestMethod.POST)
public void createCity(@RequestBody City city) {
cityService.saveCity(city);
}

/**
* 修改
* @param city
*/

@RequestMapping(value = "/", method = RequestMethod.PUT)
public void modifyCity(@RequestBody City city) {
cityService.updateCity(city);
}

/**
* 删除
* @param id
*/

@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public void modifyCity(@PathVariable("id") Long id) {
cityService.deleteCity(id);
}
}

数据库配置文件

1
2
3
4
5
6
7
8
9
## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

## Mybatis 配置
mybatis.typeAliasesPackage=org.spring.springboot.domain
mybatis.mapperLocations=classpath:mapper/*.xml

三,利用postman进行前端请求

根据 ID,获取城市信息 GET http://127.0.0.1:8080/api/city/1

在这里插入图片描述

获取城市列表 GET http://127.0.0.1:8080/api/city

在这里插入图片描述

新增城市信息 POST http://127.0.0.1:8080/api/city

在这里插入图片描述

更新城市信息 PUT http://127.0.0.1:8080/api/city

在这里插入图片描述

删除城市信息
DELETE http://127.0.0.1:8080/api/city/2

在这里插入图片描述

四,更多信息

springforall社区

-------------本文结束感谢您的阅读-------------
wusha wechat
欢迎您扫一扫上面的微信二维码,加我的微信!
坚持原创技术分享,您的支持将鼓励我继续创作!