前言
致想要与本文一起实现案例的小伙伴:
本文包含实现的全部过程,有想要一起实现案例的小伙伴可以跟着一步一步实现。
由于本案例的重点是JavaWeb,且实现了前后端分离,所以在文章中不会有前端代码的实现过程。
不过前端的部分我已经写好啦,并且已经打包编译了,所以小伙伴们可以直接下载打包好的前端文件到自己的电脑上去(大概2M左右),然后用浏览器打开就可以直接运行啦~~~~
前端文件下载地址:https://github.com/BEIJIU601/NewStuReport
前端文件详细说明和使用:https://blog.csdn.net/weixin_43760020/article/details/105281230
博客的上篇在这里:https://blog.csdn.net/weixin_43760020/article/details/105228035
注意:假设我们在做一个前后端分离、需要通过接口文档对接的项目。要仔细查看前端所需的api接口和参数字段,不然与后端的交互有可能会失败!
1、 项目Java环境搭建
2、后端代码编写
- (1)、简单回顾案例结构
- (2)、实现JavaBean对象(数据封装处理)
- (3)、实现控制器controller(前后端交互)
- (4)、实现service层(业务逻辑层)
- (5)、实现dao层(连接数据库)
二、后端详细实现
下篇主要有后端Java的详细实现,包含全部Java代码。
案例搭建使用到的工具如下,
- 数据库:MySQL
- 数据库图形工具:SQLyog
- 编译器:IDEA
- 服务器软件:Tomcat
- 涉及的Jar包:
①mysql-connector ---------JDBC连接MySQL数据库的驱动
②druid --------阿里巴巴的数据库连接池,能够重复利用数据库连接,提高对请求的响应时间和服务器的性能。
③commons-dbutils ---------Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影响程序的性能。
④fastjson --------阿里巴巴开发的一款专门用于Java开发的包,可以方便的实现json对象与JavaBean对象的转换,实现JavaBean对象与json字符串的转换,实现json对象与json字符串的转换。
如下:
1、 项目Java环境搭建
首先使用IDEA来创建项目:
查看前端接口文档:https://blog.csdn.net/weixin_43760020/article/details/105281230
发现前端请求的基础地址为:http://localhost:9601/reactLogin
故配置Tomcat如下:
(点击锤子旁边的Tomcat,在下拉框中选择 Edit Configurations…)
接下来是导入Jar包:
(1)、首先创建一个lib目录
(2)、将所需的jar包复制到该目录
(3)、然后将其添加到 Libray 即可。
在 lib 目录上右键 —> add Libray —> 点击确定即可。
至此所有环境搭建完毕,可以开始编写代码了。
2、 后端编写
(1)、简单回顾案例结构
上一篇,我们已经新建了五个包,
- beans:存放的是JavaBean。即实体类包,负责封装数据。
- controller:存放的是servlet,即负责控制器的内容。
- service:存放的是与业务逻辑相关的类,负责业务处理,实现核心需求
- dao:存放与数据库连接相关的类,负责连接数据库
- utils:存放的是连接数据库的工具类,是用来连接数据库的工具
分为五个层次,调用关系如下:
(2)、实现JavaBean对象
我们先实现JavaBean:
由上篇得出,数据库一共有两张表,即 新生登录表 和 新生报道表。
所以我们的JavaBean即为两个实体类,NewStu.java 和 Report.java
(即在beans包下新建两个类NewStu.java 和 Report.java)
参考上篇的字段,可得到两个实体类的属性和方法如下:
NewStu.java
//对应数据库的新生登录表
import java.io.Serializable;
public class NewStu implements Serializable {
private long newstuid;
private String newstuname;
public NewStu(long newstuid, String newstuname) {
this.newstuid = newstuid;
this.newstuname = newstuname;
}
public NewStu() {
}
public long getNewstuid() {
return newstuid;
}
public void setNewstuid(long newstuid) {
this.newstuid = newstuid;
}
public String getNewstuname() {
return newstuname;
}
public void setNewstuname(String newstuname) {
this.newstuname = newstuname;
}
@Override
public String toString() {
return "NewStu{" +
"newstuid=" + newstuid +
", newstuname='" + newstuname + '\'' +
'}';
}
}
Report.java
import com.alibaba.fastjson.annotation.JSONField;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
public class Report implements Serializable {
private long r_id;
private String newstuname;
private String sex;
private String major;
private String classes;
private String province;
private String telephone;
//JSONField注解用于格式化时间,如果不使用,在JavaBean和JSON字符串互相转换时会出错。
@JSONField(format="yyyy-MM-dd")
private Date to_date;
@JSONField(format="HH:mm")
private Date to_time;
private String transport;
private String transport_num;
private String trsp_remark;
private short is_loan;
private BigDecimal loan_amount;
private BigDecimal loan_pay;
private String pay_way;
private String pay_remark;
private short is_photo;
private String rec_people;
private String rec_phone;
public Report() {
}
public Report(long r_id, String newstuname,
String sex, String major,
String classes, String province,
String telephone, Date to_date,
Date to_time, String transport,
String transport_num, String trsp_remark,
short is_loan, BigDecimal loan_amount,
BigDecimal loan_pay, String pay_way,
String pay_remark, short is_photo,
String rec_people, String rec_phone)
{
this.r_id = r_id;
this.newstuname = newstuname;
this.sex = sex;
this.major = major;
this.classes = classes;
this.province = province;
this.telephone = telephone;
this.to_date = to_date;
this.to_time = to_time;
this.transport = transport;
this.transport_num = transport_num;
this.trsp_remark = trsp_remark;
this.is_loan = is_loan;
this.loan_amount = loan_amount;
this.loan_pay = loan_pay;
this.pay_way = pay_way;
this.pay_remark = pay_remark;
this.is_photo = is_photo;
this.rec_people = rec_people;
this.rec_phone = rec_phone;
}
public long getR_id() {
return r_id;
}
public void setR_id(long r_id) {
this.r_id = r_id;
}
public String getNewstuname() {
return newstuname;
}
public void setNewstuname(String newstuname) {
this.newstuname = newstuname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
public String getProvince() {
return province;
}
public void setProvince(String province