(1)CustomerRestfulController
package com.cao.springbootdemo1.controller;
import com.cao.springbootdemo1.entity.Customer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* restful 风格 客户信息
* @author Caogy
* @date 2020.06.22
* @RestController替代Controller 方法中不在使用@ResponseBody来指定返回内容为json或xml格式数据
* */
@RestController
@RequestMapping(value = "/customer/rest/api/soap-server")
public class CustomerRestfulController {
@Resource
private JdbcTemplate jdbcTemplate ;
/**
* full url: http://127.0.0.1:18020/customer/rest/api/soap-server/findifo/1
* 场景 如果需要根据客户id展示不同客户信息
* Spring会对@PathVariable注解的变量进行自动赋值,也可以指定@PathVariable使用哪一个URL中的变量
* */
@RequestMapping(value = "/findifo/{id}",method = RequestMethod.GET)
public Customer findCustIfo(@PathVariable(value = "id") String id){
String sql = "select customer_id,store_id,first_name,last_name,email,address_id,create_date from customer where customer_id=?" ;
List<Customer> customerList = jdbcTemplate.query(sql, new Object[]{id}, new RowMapper<Customer>() {
Customer custifo ;
@Override
public Customer mapRow(ResultSet resultSet, int i) throws SQLException {
custifo = new Customer() ;
custifo.setAddress_id(resultSet.getString("address_id"));
custifo.setCreate_date(resultSet.getString("create_date"));
custifo.setCustomer_id(resultSet.getInt("customer_id"));
custifo.setEmail(resultSet.getString("email"));
custifo.setFirst_name(resultSet.getString("first_name"));
custifo.setLast_name(resultSet.getString("last_name"));
custifo.setStore_id(resultSet.getInt("store_id"));
return custifo;
}
});
return customerList.get(0);
}
}
(2)Customer
package com.cao.springbootdemo1.entity;
/**
* 客户信息实体
* @author Caogy
* @date 2020.06.22
* */
public class Customer {
private Integer customer_id ;
private Integer store_id;
private String first_name;
private String last_name;
private String email;
private String address_id;
private String create_date;
public Integer getCustomer_id() {
return customer_id;
}
public void setCustomer_id(Integer customer_id) {
this.customer_id = customer_id;
}
public Integer getStore_id() {
return store_id;
}
public void setStore_id(Integer store_id) {
this.store_id = store_id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress_id() {
return address_id;
}
public void setAddress_id(String address_id) {
this.address_id = address_id;
}
public String getCreate_date() {
return create_date;
}
public void setCreate_date(String create_date) {
this.create_date = create_date;
}
}
(3)application.properties
#指定服务端口
server.port=18020
############################################################
#
# mysql
#
############################################################
spring.datasource.url=jdbc:mysql://192.168.1.102:3306/testdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
(4)数据库表结构
CREATE TABLE `customer` (
`customer_id` smallint(5) unsigned NOT NULL,
`store_id` tinyint(3) unsigned NOT NULL,
`first_name` varchar(45) NOT NULL,
`last_name` varchar(45) NOT NULL,
`email` varchar(50) DEFAULT NULL,
`address_id` smallint(5) unsigned NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`create_date` datetime NOT NULL,
`last_update` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
(5)表数据
INSERT INTO `customer` VALUES (1, 1, 'MARY', 'SMITH', 'MARY.SMITH@sakilacustomer.org', 5, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (2, 1, 'PATRICIA', 'JOHNSON', 'PATRICIA.JOHNSON@sakilacustomer.org', 6, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (3, 1, 'LINDA', 'WILLIAMS', 'LINDA.WILLIAMS@sakilacustomer.org', 7, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (4, 2, 'BARBARA', 'JONES', 'BARBARA.JONES@sakilacustomer.org', 8, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (5, 1, 'ELIZABETH', 'BROWN', 'ELIZABETH.BROWN@sakilacustomer.org', 9, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (6, 2, 'JENNIFER', 'DAVIS', 'JENNIFER.DAVIS@sakilacustomer.org', 10, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (7, 1, 'MARIA', 'MILLER', 'MARIA.MILLER@sakilacustomer.org', 11, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (8, 2, 'SUSAN', 'WILSON', 'SUSAN.WILSON@sakilacustomer.org', 12, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (9, 2, 'MARGARET', 'MOORE', 'MARGARET.MOORE@sakilacustomer.org', 13, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (10, 1, 'DOROTHY', 'TAYLOR', 'DOROTHY.TAYLOR@sakilacustomer.org', 14, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (11, 2, 'LISA', 'ANDERSON', 'LISA.ANDERSON@sakilacustomer.org', 15, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (12, 1, 'NANCY', 'THOMAS', 'NANCY.THOMAS@sakilacustomer.org', 16, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (13, 2, 'KAREN', 'JACKSON', 'KAREN.JACKSON@sakilacustomer.org', 17, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (14, 2, 'BETTY', 'WHITE', 'BETTY.WHITE@sakilacustomer.org', 18, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (15, 1, 'HELEN', 'HARRIS', 'HELEN.HARRIS@sakilacustomer.org', 19, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (16, 2, 'SANDRA', 'MARTIN', 'SANDRA.MARTIN@sakilacustomer.org', 20, 0, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (17, 1, 'DONNA', 'THOMPSON', 'DONNA.THOMPSON@sakilacustomer.org', 21, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (18, 2, 'CAROL', 'GARCIA', 'CAROL.GARCIA@sakilacustomer.org', 22, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (19, 1, 'RUTH', 'MARTINEZ', 'RUTH.MARTINEZ@sakilacustomer.org', 23, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
INSERT INTO `customer` VALUES (20, 2, 'SHARON', 'ROBINSON', 'SHARON.ROBINSON@sakilacustomer.org', 24, 1, '2006-2-14 22:04:36', '2006-2-15 04:57:20');
(6)启动springboot服务 , 用postman模拟执行查询请求,在
路径传入变量值即可查看该客户的相关信息
http://127.0.0.1:18020/customer/rest/api/soap-server/findifo/{id}
http://127.0.0.1:18020/customer/rest/api/soap-server/findifo/2
http://127.0.0.1:18020/customer/rest/api/soap-server/findifo/15
(7) 同理QQ空间 重点看路径,修改一个QQ号码,就可以实现查看不同QQ号的信息
https://user.qzone.qq.com/QQ号码