一,数据库准备 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 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 public interface CityDao { List <City> findAllCity(); 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 public interface CityService { List <City> findAllCity(); City findCityById(Long id); Long saveCity(City city); Long updateCity(City city); 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 @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) ; } }
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; @RequestMapping(value = "/{id}" , method = RequestMethod.GET) public City findOneCity(@PathVariable("id" ) Long id) { return cityService.findCityById(id); } @RequestMapping(value = "/" , method = RequestMethod.GET) public List<City> findAllCity() { return cityService.findAllCity(); } @RequestMapping(value = "/" , method = RequestMethod.POST) public void createCity(@RequestBody City city) { cityService.saveCity(city); } @RequestMapping(value = "/" , method = RequestMethod.PUT) public void modifyCity(@RequestBody City city) { cityService.updateCity(city); } @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.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社区