forked from JeffLi1993/springboot-learning-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Springboot 实现 Restful 服务,基于 HTTP / JSON 传输
- Loading branch information
1 parent
8d837b8
commit 736be2a
Showing
12 changed files
with
398 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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>springboot</groupId> | ||
<artifactId>springboot-restful</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>springboot-restful :: Spsringboot 实现 Restful 服务,基于 HTTP / JSON 传输 Demo</name> | ||
|
||
<!-- Spring Boot 启动父依赖 --> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.5.1.RELEASE</version> | ||
</parent> | ||
|
||
<properties> | ||
<mybatis-spring-boot>1.2.0</mybatis-spring-boot> | ||
<mysql-connector>5.1.39</mysql-connector> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
<!-- Spring Boot Web 依赖 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- Spring Boot Test 依赖 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- Spring Boot Mybatis 依赖 --> | ||
<dependency> | ||
<groupId>org.mybatis.spring.boot</groupId> | ||
<artifactId>mybatis-spring-boot-starter</artifactId> | ||
<version>${mybatis-spring-boot}</version> | ||
</dependency> | ||
|
||
<!-- MySQL 连接驱动依赖 --> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>${mysql-connector}</version> | ||
</dependency> | ||
|
||
<!-- Junit --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
26 changes: 26 additions & 0 deletions
26
springboot-restful/src/main/java/org/spring/springboot/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.spring.springboot; | ||
|
||
import org.mybatis.spring.annotation.MapperScan; | ||
import org.spring.springboot.dao.CityDao; | ||
import org.spring.springboot.domain.City; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* Spring Boot 应用启动类 | ||
* | ||
* Created by bysocket on 16/4/26. | ||
*/ | ||
// Spring Boot 应用的标识 | ||
@SpringBootApplication | ||
// mapper 接口类扫描包配置 | ||
@MapperScan("org.spring.springboot.dao") | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
// 程序启动入口 | ||
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件 | ||
SpringApplication.run(Application.class,args); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
springboot-restful/src/main/java/org/spring/springboot/controller/CityRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.spring.springboot.controller; | ||
|
||
import org.spring.springboot.domain.City; | ||
import org.spring.springboot.service.CityService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 城市 Controller 实现 Restful HTTP 服务 | ||
* | ||
* Created by bysocket on 07/02/2017. | ||
*/ | ||
@RestController | ||
public class CityRestController { | ||
|
||
@Autowired | ||
private CityService cityService; | ||
|
||
@RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET) | ||
public City findOneCity(@PathVariable("id") Long id) { | ||
return cityService.findCityById(id); | ||
} | ||
|
||
@RequestMapping(value = "/api/city", method = RequestMethod.GET) | ||
public List<City> findAllCity() { | ||
return cityService.findAllCity(); | ||
} | ||
|
||
@RequestMapping(value = "/api/city", method = RequestMethod.POST) | ||
public void createCity(@RequestBody City city) { | ||
cityService.saveCity(city); | ||
} | ||
|
||
@RequestMapping(value = "/api/city", method = RequestMethod.PUT) | ||
public void modifyCity(@RequestBody City city) { | ||
cityService.updateCity(city); | ||
} | ||
|
||
@RequestMapping(value = "/api/city/{id}", method = RequestMethod.DELETE) | ||
public void modifyCity(@PathVariable("id") Long id) { | ||
cityService.deleteCity(id); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
springboot-restful/src/main/java/org/spring/springboot/dao/CityDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.spring.springboot.dao; | ||
|
||
import org.apache.ibatis.annotations.Param; | ||
import org.spring.springboot.domain.City; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 城市 DAO 接口类 | ||
* | ||
* Created by bysocket on 07/02/2017. | ||
*/ | ||
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); | ||
} |
61 changes: 61 additions & 0 deletions
61
springboot-restful/src/main/java/org/spring/springboot/domain/City.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.spring.springboot.domain; | ||
|
||
/** | ||
* 城市实体类 | ||
* | ||
* Created by bysocket on 07/02/2017. | ||
*/ | ||
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; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
springboot-restful/src/main/java/org/spring/springboot/service/CityService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.spring.springboot.service; | ||
|
||
import org.spring.springboot.domain.City; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 城市业务逻辑接口类 | ||
* | ||
* Created by bysocket on 07/02/2017. | ||
*/ | ||
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); | ||
} |
45 changes: 45 additions & 0 deletions
45
springboot-restful/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.spring.springboot.service.impl; | ||
|
||
import org.spring.springboot.dao.CityDao; | ||
import org.spring.springboot.domain.City; | ||
import org.spring.springboot.service.CityService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 城市业务逻辑实现类 | ||
* | ||
* Created by bysocket on 07/02/2017. | ||
*/ | ||
@Service | ||
public class CityServiceImpl implements CityService { | ||
|
||
@Autowired | ||
private CityDao cityDao; | ||
|
||
public List<City> findAllCity(){ | ||
return cityDao.findAllCity(); | ||
} | ||
|
||
public City findCityById(Long id) { | ||
return cityDao.findById(id); | ||
} | ||
|
||
@Override | ||
public Long saveCity(City city) { | ||
return cityDao.saveCity(city); | ||
} | ||
|
||
@Override | ||
public Long updateCity(City city) { | ||
return cityDao.updateCity(city); | ||
} | ||
|
||
@Override | ||
public Long deleteCity(Long id) { | ||
return cityDao.deleteCity(id); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
## 数据源配置 | ||
spring.datasource.url=jdbc:mysql://139.224.14.39:3306/springbootdb?useUnicode=true&characterEncoding=utf8 | ||
spring.datasource.username=root | ||
spring.datasource.password=Hello123!@ | ||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver | ||
|
||
## Mybatis 配置 | ||
mybatis.typeAliasesPackage=org.spring.springboot.domain | ||
mybatis.mapperLocations=classpath:mapper/*.xml |
Oops, something went wrong.