目录
一,主要流程
二,编码过程
4.2 在ExpressDao实现类中编写SQL语句
完整项目在这里实时更新<( ̄︶ ̄)↗[GO!]:https://github.com/GoodbyeFirefly/ExpressManagementSystem
一,主要流程
二,编码过程
1,创建数据库表格
CREATE TABLE express(
id INT PRIMARY KEY auto_increment,
number VARCHAR(64) UNIQUE,
username VARCHAR(32),
userphone VARCHAR(32),
company VARCHAR(32),
code VARCHAR(32) UNIQUE,
intime TIMESTAMP,
outtime TIMESTAMP,
status int,
sysPhone VARCHAR(32)
);
若通过navicat可视化工具创建时,UNIQUE属性需要在索引栏进行相关配置
生成表格对应的实体类
package com.xxy.bean;
import java.sql.Timestamp;
import java.util.Objects;
public class Express {
private int id;
private String number;
private String username;
private String userphone;
private String company;
private String code;
private Timestamp intime;
private Timestamp outtime;
private int status;
private String sysPhone;
public Express(String number, String username, String userphone, String company, String code, String sysPhone) {
this.number = number;
this.username = username;
this.userphone = userphone;
this.company = company;
this.code = code;
this.sysPhone = sysPhone;
}
public Express(int id, String number, String username, String userphone, String company, Timestamp intime, Timestamp outtime, int status, String sysPhone) {
this.id = id;
this.number = number;
this.username = username;
this.userphone = userphone;
this.company = company;
this.intime = intime;
this.outtime = outtime;
this.status = status;
this.sysPhone = sysPhone;
}
public Express() {
}
@Override
public String toString() {
return "Express{" +
"id=" + id +
", number='" + number + '\'' +
", username='" + username + '\'' +
", userphone='" + userphone + '\'' +
", company='" + company + '\'' +
", code='" + code + '\'' +
", intime=" + intime +
", outtime=" + outtime +
", status=" + status +
", sysPhone='" + sysPhone + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Express express = (Express) o;
return id == express.id &&
status == express.status &&
Objects.equals(number, express.number) &&
Objects.equals(username, express.username) &&
Objects.equals(userphone, express.userphone) &&
Objects.equals(company, express.company) &&
Objects.equals(code, express.code) &&
Objects.equals(intime, express.intime) &&
Objects.equals(outtime, express.outtime) &&
Objects.equals(sysPhone, express.sysPhone);
}
@Override
public int hashCode() {
return Objects.hash(id, number, username, userphone, company, code, intime, outtime, status, sysPhone);
}
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserphone() {
return userphone;
}
public void setUserphone(String userphone) {
this.userphone = userphone;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public java.sql.Timestamp getIntime() {
return intime;
}
public void setIntime(java.sql.Timestamp intime) {
this.intime = intime;
}
public java.sql.Timestamp getOuttime() {
return outtime;
}
public void setOuttime(java.sql.Timestamp outtime) {
this.outtime = outtime;
}
public long getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getSysPhone() {
return sysPhone;
}
public void setSysPhone(String sysPhone) {
this.sysPhone = sysPhone;
}
}
2, API设计
2.1 用于获取控制台所需的快递数据
请求地址:express/console.do
参数列表:无
返回的格式示例:
{
status:0,
reuslt:"获取成功",
data:[
{//全部的快递
size:1000,//快递总数
day:100//今日新增
},{//待取件快递
size:500,//待取件数
day:100//今日新增
}
]
}
2.2 快件列表(分页)
请求地址:express/findAll.do
参数列表:
1. limit:
值:0,表示开启分页(默认)
值:1,表示查询所有
2. offset:
值:数字,表示SQL语句起始索引
3. pageNumber:
值:数字,表示获取的快递数量
返回的格式示例:
2.3 根据单号查询快递信息
请求地址:express/findByNumber.do
参数列表:
1. number:快递单号
返回的格式示例:
2.4 根据取件码查询快递信息
请求地址:express/findByCode.do
参数列表:
1. code:取件码
返回的格式示例:
2.5 根据用户的手机号,查询快递信息
请求地址:express/findByUserPhone.do
参数列表:
1. phoneNumber:手机号码
2. status:
值:0表示查询待取件的快递(默认)
值:1表示查询已取件的快递
值:2表示查询用户的所有快递
返回的格式示例:
2.6 根据录入人的手机号,查询快递信息(快递员/柜子的历史记录)
请求地址:express/findBySysPhone.do
参数列表:
1. sysPhone:手机号码
返回的格式示例:
2.7 进行快递数量的排序查询(用户表)
请求地址:express/lazyboard.do
参数列表:
1. type:
值:0,表示查询总排名
值:1,表示查询年排名
值:2,表示查询月排名
返回的格式示例:
2.8 快件录入
请求地址:express/insert.do
参数列表:
1. number:快递单号
2. company:快递公司
3. username:收件人姓名
4. userPhone:收件人手机号码
录入成功返回的格式示例:
录入失败返回的格式示例:
2.9 修改快递信息
请求地址:express/update.do
参数列表:
1. id:要修改的快递id
2. number:新的快递单号
3. company:新的快递公司
4. username:新的收货人姓名
5. userPhone:新的收件人手机号码,(手机号码更新,重新生成取件码,并发送短信)
6. status:新的快递的状态
返回的格式示例:
2.10 根据id删除快递信息
请求地址:express/delete.do
参数列表:
1. id: 要删除的快递的id
返回的格式示例:
2.11 确认取件
请求地址:express/updateStatus.do
参数列表:
number:要更改状态为已取件的快递单号
返回的格式示例:
3,DAO接口编写
package com.xxy.dao;
import com.xxy.bean.Express;
import java.util.List;
import java.util.Map;
public interface BaseExpressDao {
/**
* 用于查询数据库中的全部快递(总数+新增),待取件快递(总数+新增)
* @return [{size:总数,day:新增},{size:总数,day:新增}]
*
*/
List<Map<String,Integer>> console();
/**
* 用于查询所有快递
* @param limit 是否分页的标记,true表示分页。false表示查询所有快递
* @param offset SQL语句的起始索引
* @param pageNumber 页查询的数量
* @return 快递的集合
*/
List<Express> findAll(boolean limit,int offset,int pageNumber);
/**
* 根据快递单号,查询快递信息
* @param number 单号
* @return 查询的快递信息,单号不存在时返回null
*/
Express findByNumber(String number);
/**
* 根据快递取件码,查询快递信息
* @param code 取件码
* @return 查询的快递信息,取件码不存在时返回null
*/
Express findByCode(String code);
/**
* 根据用户手机号码,查询他所有的快递信息
* @param userPhone 手机号码
* @return 查询的快递信息列表
*/
List<Express> findByUserPhone(String userPhone);
/**
* 根据录入人手机号码,查询录入的所有记录
* @param sysPhone 手机号码
* @return 查询的快递信息列表
*/
List<Express> findBySysPhone(String sysPhone);
/**
* 快递的录入
* @param e 要录入的快递对象
* @return 录入的结果,true表示成功,false表示失败
*/
boolean insert(Express e);
/**
* 快递的修改
* @param id 要修改的快递id
* @param newExpress 新的快递对象(number,company,username,userPhone)
* @return 修改的结果,true表示成功,false表示失败
*/
boolean update(int id,Express newExpress);
/**
* 更改快递的状态为1,表示取件完成
* @param code 要修改的快递取件码
* @return 修改的结果,true表示成功,false表示失败
*/
boolean updateStatus(String code);
/**
* 根据id,删除单个快递信息
* @param id 要删除的快递id
* @return 删除的结果,true表示成功,false表示失败
*/
boolean delete(int id);
}
4,SQL语句编写
4.1 向Express表添加记录
4.2 在ExpressDao实现类中编写SQL语句
// 这里统一使用大写 如果不是大写的话后面会自动转换为大写
// 用于查询数据库中的全部快递(总数+新增),待取件快递(总数+新增)
public static final String SQL_CONSOLE = "SELECT COUNT(ID) data1_size, COUNT(TO_DAYS(INTIME)=TO_DAYS(NOW()) OR NULL) data1_day, COUNT(STATUS=0 OR NULL) data2_size, COUNT(TO_DAYS(INTIME)=TO_DAYS(NOW()) AND STATUS=0 OR NULL) data2_day FROM EXPRESS";
// 用于查询数据库中的所有快递信息
public static final String SQL_FIND_ALL = "SELECT * FROM EXPRESS";
// 用于分页查询数据库中的快递信息
public static final String SQL_FIND_LIMIT = "SELECT * FROM EXPRESS LIMIT ?,?";
// 通过取件码查询快递信息
public static final String SQL_FIND_BY_CODE = "SELECT * FROM EXPRESS WHERE CODE=?";
// 通过快递单号查询快递信息
public static final String SQL_FIND_BY_NUMBER = "SELECT * FROM EXPRESS WHERE NUMBER=?";
// 通过录入人手机号查询快递信息
public static final String SQL_FIND_BY_SYSPHONE = "SELECT * FROM EXPRESS WHERE SYSPHONE=?";
// 通过用户手机号查询用户所有快递
public static final String SQL_FIND_BY_USERPHONE = "SELECT * FROM EXPRESS WHERE USERPHONE=?";
// 录入快递
public static final String SQL_INSERT = "INSERT INTO EXPRESS (NUMBER,USERNAME,USERPHONE,COMPANY,CODE,INTIME,STATUS,SYSPHONE) VALUES(?,?,?,?,?,NOW(),0,?)";
// 快递修改
public static final String SQL_UPDATE = "UPDATE EXPRESS SET NUMBER=?,USERNAME=?,COMPANY=?,STATUS=? WHERE ID=?";
// 快递的状态码改变(取件)
public static final String SQL_UPDATE_STATUS = "UPDATE EXPRESS SET STATUS=1,OUTTIME=NOW(),CODE=NULL WHERE CODE=?";
// 快递的删除
public static final String SQL_DELETE = "DELETE FROM EXPRESS WHERE ID=?";
5,Dao编写+Dao测试
5.1 ExpressDao实现类编写
package com.xxy.dao.impl;
import com.xxy.bean.Express;
import com.xxy.dao.BaseExpressDao;
import com.xxy.util.DruidUtil;
import com.xxy.exception.DuplicateCodeException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExpressDaoMysql implements BaseExpressDao {
// 这里统一使用大写 如果不是大写的话后面会自动转换为大写
// 用于查询数据库中的全部快递(总数+新增),待取件快递(总数+新增)
public static final String SQL_CONSOLE = "SELECT COUNT(ID) data1_size, COUNT(TO_DAYS(INTIME)=TO_DAYS(NOW()) OR NULL) data1_day, COUNT(STATUS=0 OR NULL) data2_size, COUNT(TO_DAYS(INTIME)=TO_DAYS(NOW()) AND STATUS=0 OR NULL) data2_day FROM EXPRESS";
// 用于查询数据库中的所有快递信息
public static final String SQL_FIND_ALL = "SELECT * FROM EXPRESS";
// 用于分页查询数据库中的快递信息
public static final String SQL_FIND_LIMIT = "SELECT * FROM EXPRESS LIMIT ?,?";
// 通过取件码查询快递信息
public static final String SQL_FIND_BY_CODE = "SELECT * FROM EXPRESS WHERE CODE=?";
// 通过快递单号查询快递信息
public static final String SQL_FIND_BY_NUMBER = "SELECT * FROM EXPRESS WHERE NUMBER=?";
// 通过录入人手机号查询快递信息
public static final String SQL_FIND_BY_SYSPHONE = "SELECT * FROM EXPRESS WHERE SYSPHONE=?";
// 通过用户手机号查询用户所有快递
public static final String SQL_FIND_BY_USERPHONE = "SELECT * FROM EXPRESS WHERE USERPHONE=?";
// 录入快递
public static final String SQL_INSERT = "INSERT INTO EXPRESS (NUMBER,USERNAME,USERPHONE,COMPANY,CODE,INTIME,STATUS,SYSPHONE) VALUES(?,?,?,?,?,NOW(),0,?)";
// 快递修改
public static final String SQL_UPDATE = "UPDATE EXPRESS SET NUMBER=?,USERNAME=?,COMPANY=?,STATUS=? WHERE ID=?";
// 快递的状态码改变(取件)
public static final String SQL_UPDATE_STATUS = "UPDATE EXPRESS SET STATUS=1,OUTTIME=NOW(),CODE=NULL WHERE CODE=?";
// 快递的删除
public static final String SQL_DELETE = "DELETE FROM EXPRESS WHERE ID=?";
/**
* 用于查询数据库中的全部快递(总数+新增),待取件快递(总数+新增)
*
* @return [{size:总数,day:新增},{size:总数,day:新增}]
*/
@Override
public List<Map<String, Integer>> console() {
List<Map<String, Integer>> data = new ArrayList<>();
// 1 获取数据库连接
Connection connection = DruidUtil.getConnection();
PreparedStatement statement = null;// 变量向上抽取,便于释放资源
ResultSet result = null;
try {
// 2 预编译SQL语句
statement = connection.prepareStatement(SQL_CONSOLE);
// 3 填充参数(可选)
// 4 执行SQL语句
result = statement.executeQuery();
// 5 获得执行结果
if (result.next()) {
int data1_size = result.getInt("data1_size");
int data1_day = result.getInt("data1_day");
int data2_size = result.getInt("data2_size");
int data2_day = result.getInt("data2_day");
Map data1 = new HashMap();
data1.put("data1_size", data1_size);
data1.put("data1_day", data1_day);
Map data2 = new HashMap();
data2.put("data2_size", data2_size);
data2.put("data2_day", data2_day);
data.add(data1);
data.add(data2);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 6 释放资源
DruidUtil.close(connection, statement, result);
}
return data;
}
/**
* 用于查询所有快递
*
* @param limit 是否分页的标记,true表示分页。false表示查询所有快递
* @param offset SQL语句的起始索引
* @param pageNumber 页查询的数量
* @return 快递的集合
*/
@Override
public List<Express> findAll(boolean limit, int offset, int pageNumber) {
ArrayList<Express> data = new ArrayList<>();
//1. 获取数据库的连接
Connection conn = DruidUtil.getConnection();
PreparedStatement state = null;
ResultSet result = null;
//2. 预编译SQL语句
try {
if(limit) {
state = conn.prepareStatement(SQL_FIND_LIMIT);
//3. 填充参数(可选)
state.setInt(1,offset);
state.setInt(2,pageNumber);
} else {
state = conn.prepareStatement(SQL_FIND_ALL);
}
//4. 执行SQL语句
result = state.executeQuery();
//5. 获取执行的结果
while(result.next()) {
int id = result.getInt("id");
String number = result.getString("number");
String username = result.getString("username");
String userPhone = result.getString("userPhone");
String company = result.getString("company");
String code = result.getString("code");
Timestamp inTime = result.getTimestamp("inTime");
Timestamp outTime = result.getTimestamp("outTime");
int status = result.getInt("status");
String sysPhone = result.getString("sysPhone");
Express e = new Express(id,number,username,userPhone,company,code,inTime,outTime,status,sysPhone);
data.add(e);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//6. 资源的释放
DruidUtil.close(conn,state,result);
}
return data;
}
/**
* 根据快递单号,查询快递信息
*
* @param number 单号
* @return 查询的快递信息,单号不存在时返回null
*/
@Override
public Express findByNumber(String number) {
// 1 获取数据库连接
Connection connection = DruidUtil.getConnection();
PreparedStatement statement = null;
ResultSet result = null;
try {
// 2 预编译SQL语句
statement = connection.prepareStatement(SQL_FIND_BY_NUMBER);
// 3 填充参数(可选)
statement.setString(1, number);
// 4 执行SQL语句
result = statement.executeQuery();
// 5 获得执行结果
if (result.next()) {
int id = result.getInt("id");
String username = result.getString("username");
String userPhone = result.getString("userPhone");
String company = result.getString("company");
String code = result.getString("code");
Timestamp inTime = result.getTimestamp("inTime");
Timestamp outTime = result.getTimestamp("outTime");
int status = result.getInt("status");
String sysPhone = result.getString("sysPhone");
Express e = new Express(id,number,username,userPhone,company,code,inTime,outTime,status,sysPhone);
return e;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 6 释放资源
DruidUtil.close(connection, statement, result);
}
return null;
}
/**
* 根据快递取件码,查询快递信息
*
* @param code 取件码
* @return 查询的快递信息,取件码不存在时返回null
*/
@Override
public Express findByCode(String code) {
// 1 获取数据库连接
Connection connection = DruidUtil.getConnection();
PreparedStatement statement = null;
ResultSet result = null;
try {
// 2 预编译SQL语句
statement = connection.prepareStatement(SQL_FIND_BY_CODE);
// 3 填充参数(可选)
statement.setString(1, code);
// 4 执行SQL语句
result = statement.executeQuery();
// 5 获得执行结果
if (result.next()) {
int id = result.getInt("id");
String number = result.getString("number");
String username = result.getString("username");
String userPhone = result.getString("userPhone");
String company = result.getString("company");
Timestamp inTime = result.getTimestamp("inTime");
Timestamp outTime = result.getTimestamp("outTime");
int status = result.getInt("status");
String sysPhone = result.getString("sysPhone");
Express e = new Express(id,number,username,userPhone,company,code,inTime,outTime,status,sysPhone);
return e;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 6 释放资源
DruidUtil.close(connection, statement, result);
}
return null;
}
/**
* 根据用户手机号码,查询他所有的快递信息
*
* @param userPhone 手机号码
* @return 查询的快递信息列表
*/
@Override
public List<Express> findByUserPhone(String userPhone) {
List<Express> data = new ArrayList<>();
// 1 获取数据库连接
Connection connection = DruidUtil.getConnection();
PreparedStatement statement = null;
ResultSet result = null;
try {
// 2 预编译SQL语句
statement = connection.prepareStatement(SQL_FIND_BY_USERPHONE);
// 3 填充参数(可选)
statement.setString(1, userPhone);
// 4 执行SQL语句
result = statement.executeQuery();
// 5 获得执行结果
while (result.next()) {
int id = result.getInt("id");
String number = result.getString("number");
String username = result.getString("username");
String company = result.getString("company");
String code = result.getString("code");
Timestamp inTime = result.getTimestamp("inTime");
Timestamp outTime = result.getTimestamp("outTime");
int status = result.getInt("status");
String sysPhone = result.getString("sysPhone");
Express e = new Express(id,number,username,userPhone,company,code,inTime,outTime,status,sysPhone);
data.add(e);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 6 释放资源
DruidUtil.close(connection, statement, result);
}
return data;
}
/**
* 根据录入人手机号码,查询录入的所有记录
*
* @param sysPhone 手机号码
* @return 查询的快递信息列表
*/
@Override
public List<Express> findBySysPhone(String sysPhone) {
List<Express> data = new ArrayList<>();
// 1 获取数据库连接
Connection connection = DruidUtil.getConnection();
PreparedStatement statement = null;
ResultSet result = null;
try {
// 2 预编译SQL语句
statement = connection.prepareStatement(SQL_FIND_BY_SYSPHONE);
// 3 填充参数(可选)
statement.setString(1, sysPhone);
// 4 执行SQL语句
result = statement.executeQuery();
// 5 获得执行结果
while (result.next()) {
int id = result.getInt("id");
String number = result.getString("number");
String username = result.getString("username");
String userPhone = result.getString("userPhone");
String company = result.getString("company");
String code = result.getString("code");
Timestamp inTime = result.getTimestamp("inTime");
Timestamp outTime = result.getTimestamp("outTime");
int status = result.getInt("status");
Express e = new Express(id,number,username,userPhone,company,code,inTime,outTime,status,sysPhone);
data.add(e);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 6 释放资源
DruidUtil.close(connection, statement, result);
}
return data;
}
/**
* 快递的录入
*
* @param e 要录入的快递对象
* @return 录入的结果,true表示成功,false表示失败
*/
@Override
public boolean insert(Express e) throws DuplicateCodeException {
//1. 连接的获取
Connection conn = DruidUtil.getConnection();
//2. 预编译SQL语句
PreparedStatement state = null;
try {
state = conn.prepareStatement(SQL_INSERT);
//3. 填充参数
state.setString(1,e.getNumber());
state.setString(2,e.getUsername());
state.setString(3,e.getUserphone());
state.setString(4,e.getCompany());
state.setString(5,e.getCode());
state.setString(6,e.getSysPhone());
//4. 执行SQL语句,并获取执行结果
return state.executeUpdate()>0?true:false;
} catch (SQLException e1) {
/*throwables.printStackTrace();*/
if(e1.getMessage().endsWith("for key 'code'")){
//是因为取件码重复,而出现了异常
DuplicateCodeException e2 = new DuplicateCodeException(e1.getMessage());
throw e2;
}else{
e1.printStackTrace();
}
}finally {
//5. 释放资源
DruidUtil.close(conn,state,null);
}
return false;
}
/**
* 快递的修改
*
* @param id 要修改的快递id
* @param newExpress 新的快递对象(number,company,username,userPhone)
* @return 修改的结果,true表示成功,false表示失败
*/
@Override
public boolean update(int id, Express newExpress) {
//1. 连接的获取
Connection conn = DruidUtil.getConnection();
PreparedStatement state = null;
//2. 预编译SQL语句
try {
state = conn.prepareStatement(SQL_UPDATE);
state.setString(1,newExpress.getNumber());
state.setString(2,newExpress.getUsername());
state.setString(3,newExpress.getCompany());
state.setInt(4,newExpress.getStatus());
state.setInt(5,id);
return state.executeUpdate()>0?true:false;
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DruidUtil.close(conn,state,null);
}
return false;
}
/**
* 更改快递的状态为1,表示取件完成
*
* @param code 要修改的快递取件码
* @return 修改的结果,true表示成功,false表示失败
*/
@Override
public boolean updateStatus(String code) {
//1. 连接的获取
Connection conn = DruidUtil.getConnection();
PreparedStatement state = null;
//2. 预编译SQL语句
try {
state = conn.prepareStatement(SQL_UPDATE_STATUS);
state.setString(1, code);
return state.executeUpdate()>0?true:false;
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DruidUtil.close(conn,state,null);
}
return false;
}
/**
* 根据id,删除单个快递信息
*
* @param id 要删除的快递id
* @return 删除的结果,true表示成功,false表示失败
*/
@Override
public boolean delete(int id) {
//1. 连接的获取
Connection conn = DruidUtil.getConnection();
PreparedStatement state = null;
//2. 预编译SQL语句
try {
state = conn.prepareStatement(SQL_DELETE);
state.setInt(1,id);
return state.executeUpdate()>0?true:false;
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DruidUtil.close(conn,state,null);
}
return false;
}
}
5.2 Dao测试
Ctrl+Shift+T:生成测试类
这里提供了部分测试的代码实现
package com.xxy.dao.impl;
import com.xxy.bean.Express;
import com.xxy.exception.DuplicateCodeException;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.*;
public class ExpressDaoMysqlTest {
ExpressDaoMysql dao = new ExpressDaoMysql();
@Test
public void console() {
List<Map<String, Integer>> console = dao.console();
System.out.println(console);
}
@Test
public void findAll() {
// List<Express> all = dao.findAll(true, 2, 3);// 从2开始查3个(offset从0开始)
List<Express> all = dao.findAll(false, 0, 0);
System.out.println(all);
}
@Test
public void findByNumber() {
Express e = dao.findByNumber("103");
System.out.println(e);
}
@Test
public void findByCode() {
Express e = dao.findByCode("123456");
System.out.println(e);
}
@Test
public void findByUserPhone() {
List<Express> byUserPhone = dao.findByUserPhone("13838411438");
System.out.println(byUserPhone);
}
@Test
public void findBySysPhone() {
List<Express> bySysPhone = dao.findBySysPhone("16666666666");
System.out.println(bySysPhone);
}
@Test
public void insert() {
Express e = new Express("123","迪卢克","18516955565","顺丰快递","123456","18888888888");
boolean result = false;
try {
result = dao.insert(e);
} catch (DuplicateCodeException e1) {
System.out.println("捕获取件码重复异常");
}
System.out.println(result);
}
@Test
public void update() {
}
@Test
public void updateStatus() {
}
@Test
public void delete() {
}
}
5.3 自定义异常
当随机生成的取件码code(不是快递单号number)重复时,应该给出信息,并针对此信息进行调整,避免直接报错(service层对该问题详细处理,这里只负责捕获)
继承Exception,并初始化两个构造方法
package com.xxy.exception;
public class DuplicateCodeException extends Exception {
/**
* Constructs a new exception with {@code null} as its detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to {@link #initCause}.
*/
public DuplicateCodeException() {
}
/**
* Constructs a new exception with the specified detail message. The
* cause is not initialized, and may subsequently be initialized by
* a call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public DuplicateCodeException(String message) {
super(message);
}
}
在ExpressDao实现类中识别该异常并抛出
测试类中捕获并显示该异常
6,ExpressService编写
注意里面的方法是static类型
package com.xxy.service;
import com.xxy.bean.Express;
import com.xxy.dao.BaseExpressDao;
import com.xxy.dao.impl.ExpressDaoMysql;
import com.xxy.exception.DuplicateCodeException;
import com.xxy.util.RandomUtil;
import java.util.List;
import java.util.Map;
public class ExpressService {
private static BaseExpressDao dao = new ExpressDaoMysql();
/**
* 用于查询数据库中的全部快递(总数+新增),待取件快递(总数+新增)
*
* @return [{size:总数,day:新增},{size:总数,day:新增}]
*/
public static List<Map<String, Integer>> console() {
return dao.console();
}
/**
* 用于查询所有快递
*
* @param limit 是否分页的标记,true表示分页。false表示查询所有快递
* @param offset SQL语句的起始索引
* @param pageNumber 页查询的数量
* @return 快递的集合
*/
public static List<Express> findAll(boolean limit, int offset, int pageNumber) {
return dao.findAll(limit, offset, pageNumber);
}
/**
* 根据快递单号,查询快递信息
*
* @param number 单号
* @return 查询的快递信息,单号不存在时返回null
*/
public static Express findByNumber(String number) {
return dao.findByNumber(number);
}
/**
* 根据快递取件码,查询快递信息
*
* @param code 取件码
* @return 查询的快递信息,取件码不存在时返回null
*/
public static Express findByCode(String code) {
return dao.findByCode(code);
}
/**
* 根据用户手机号码,查询他所有的快递信息
*
* @param userPhone 手机号码
* @return 查询的快递信息列表
*/
public static List<Express> findByUserPhone(String userPhone) {
return dao.findByUserPhone((userPhone));
}
/**
* 根据录入人手机号码,查询录入的所有记录
*
* @param sysPhone 手机号码
* @return 查询的快递信息列表
*/
public static List<Express> findBySysPhone(String sysPhone) {
return dao.findBySysPhone(sysPhone);
}
/**
* 快递的录入
*
* @param e 要录入的快递对象
* @return 录入的结果,true表示成功,false表示失败
*/
public static boolean insert(Express e) {
e.setCode(RandomUtil.getCode()+"");
try {
return dao.insert(e);
} catch (DuplicateCodeException e1) {
// 捕获到取件码重复后 递归调用插入
return insert(e);
}
}
/**
* 快递的修改
*
* @param id 要修改的快递id
* @param newExpress 新的快递对象(number,company,username,userPhone)
* @return 修改的结果,true表示成功,false表示失败
*/
public static boolean update(int id, Express newExpress) {
// ExpressDao实现类里关于update的操作也不是很清楚 这里插个眼,后面有问题再来修改
// 个人理解:当有修改收件人号码的需求时,由于涉及到重发短信的业务,需要重新执行插入(插入时,会向用户发送短信)
if (newExpress.getUserphone() != null) {
dao.delete(id);
return insert(newExpress);
} else {
// 这里的逻辑感觉不是很清晰,后面有问题再来修改吧
boolean update = dao.update(id, newExpress);
Express e = dao.findByNumber(newExpress.getNumber());
if (newExpress.getStatus() == 1) {
updateStatus(e.getCode());
}
return update;
}
}
/**
* 更改快递的状态为1,表示取件完成
*
* @param code 要修改的快递取件码
* @return 修改的结果,true表示成功,false表示失败
*/
public static boolean updateStatus(String code) {
return dao.updateStatus(code);
}
/**
* 根据id,删除单个快递信息
*
* @param id 要删除的快递id
* @return 删除的结果,true表示成功,false表示失败
*/
public static boolean delete(int id) {
return dao.delete(id);
}
}
用到的工具类RandomUtil
package com.xxy.util;
import java.util.Random;
public class RandomUtil {
private static Random r = new Random();
public static int getCode() { return r.nextInt(900000) + 100000; }
}
标签:return,07,Service,Express,id,快递,result,public,String From: https://blog.51cto.com/u_15849465/5803801章节汇总在这里(づ ̄3 ̄)づ╭❤~@&再见萤火虫&【07-项目训练】
对学习Java感兴趣的同学欢迎加入QQ学习交流群:1126298731
有问题欢迎提问,大家一起在学习Java的路上打怪升级!(o゜▽゜)o☆[BINGO!]