技术框架
数据库:mysql
服务器:tomcat
框架:JDBC,druid,javeweb
前端:html+css+jquery+ajax+jsp
项目展示:
登录界面(分为管理员登录和用户–宿管登录;注册)
注册界面:
使用宿管账号采用管理员方式登录会报错
界面内容
主要功能
1 采用管理员方式和宿管方式两种不同登录,并显示不同界面
2 不同用户登录显示不同用户名
3 账户注销登录:销毁存储登录信息的session
4 添加,删除,查询宿舍,学生,楼寓信息
5 数据库多表查询,模糊查询,条件查询
6 数据库的逻辑删除和物理删除
基本思路
浏览器(用户)
点击页面中的链接或者按钮携带参数会向服务器发送请求,DNS解析器负责解析url,分析访问的ip地址和应用端口(tomcat默认为8080),然后将请求发送到web服务器。
服务器
web服务器加载servlet容器,加载servelet实体类。servelet调用service方法,判断请求方式(默认是get),然后进行方式分配,执行servlet内相应service方法,调用request方法获取相应参数。
service不同方法调用数据库查询,将结果一步步传回到servlet。最后用request请求转发和reponse或者利用输入流写回到页面。
页面解析服务器传回数据,修改和添加数据,进行页面的重新渲染。
dao层连接数据库,并将查询结果包装成实体类进行返回给相应service方法
package com.cainiao.dao.impl;
import com.cainiao.dao.BuildingDao;
import com.cainiao.pojo.Building;
import com.cainiao.pojo.Dormitory;
import com.cainiao.pojo.SystemAdmin;
import com.cainiao.util.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BuildingDaoImpl implements BuildingDao {
@Override
public List<Building> list() throws SQLException {
Connection connection = JDBCUtils.getConnection();
String sql = "select b.id,b.name,b.introduction,d.name,d.id from dormitory.dormitory_admin d ,dormitory.building b where b.admin_id =d.id";
PreparedStatement statement = null; //预编译 防止sql注入
ResultSet resultSet = null; //结果集
List<Building> list = new ArrayList<>();
try {
statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt(1);
String name = resultSet.getString(2);
String introduction = resultSet.getString(3);
String adminName = resultSet.getString(4);
Integer adminId = resultSet.getInt(5);
list.add(new Building(id,name,introduction,adminId,adminName));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(connection, statement);
}
return list;
}
@Override
public List<Building> search(String key, String value) throws SQLException {
Connection connection = JDBCUtils.getConnection();
String sql = "select b.id,b.name,b.introduction,d.name,d.id from dormitory.dormitory_admin d ,dormitory.building b where b.admin_id =d.id and b."+key+" like '%"+value+"%' ";
PreparedStatement statement = null; //预编译 防止sql注入
ResultSet resultSet = null; //结果集
List<Building> list = new ArrayList<>();
try {
statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt(1);
String name = resultSet.getString(2);
String introduction = resultSet.getString(3);
String adminName = resultSet.getString(4);
Integer adminId = resultSet.getInt(5);
list.add(new Building(id,name,introduction,adminId,adminName));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(connection, statement);
}
return list;
}
@Override
public Integer save(String name, String introduction, Integer adminId) throws SQLException {
Connection connection = JDBCUtils.getConnection();
String sql = "insert into dormitory.building(name,introduction,admin_id) values(?,?,?) ";
PreparedStatement statement = null; //预编译 防止sql注入
Integer result = null;
try {
statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.setString(2, introduction);
statement.setInt(3, adminId);
result = statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(connection, statement);
}
return result;
}
@Override
public Integer update(Integer id, String name, String introduction, Integer adminId) throws SQLException {
Connection connection = JDBCUtils.getConnection();
String sql = "update dormitory.building set name=?,introduction=?,admin_id=? where id= ?";
PreparedStatement statement = null; //预编译 防止sql注入
Integer result = null;
try {
statement = connection.prepareStatement(sql);
statement.setString(1,name);
statement.setString(2, introduction);
statement.setInt(3, adminId);
statement.setInt(4, id);
result = statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(connection, statement);
}
return result;
}
@Override
public Integer delete(Integer id) throws SQLException {
Connection connection = JDBCUtils.getConnection();
String sql = "delete from dormitory.building where id= ?";
PreparedStatement statement = null; //预编译 防止sql注入
Integer result = null;
try {
statement = connection.prepareStatement(sql);
statement.setInt(1,id);
result = statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(connection, statement);
}
return result;
}
}
service方法:业务层 负责调用不同方法
package com.cainiao.service.impl;
import com.cainiao.dao.BuildingDao;
import com.cainiao.dao.DormitoryDao;
import com.cainiao.dao.StudentManagerDao;
import com.cainiao.dao.impl.BuildingDaoImpl;
import com.cainiao.dao.impl.DormitoryDaoImpl;
import com.cainiao.dao.impl.StudentManagerDaoImpl;
import com.cainiao.pojo.Building;
import com.cainiao.pojo.Dormitory;
import com.cainiao.service.BuildingService;
import java.sql.SQLException;
import java.util.List;
public class BuildingServiceImpl implements BuildingService {
private BuildingDao buildingDao = new BuildingDaoImpl();
private DormitoryDao dormitoryDao =new DormitoryDaoImpl();
private StudentManagerDao studentManagerDao = new StudentManagerDaoImpl();
@Override
public List<Building> list() throws SQLException {
return this.buildingDao.list();
}
@Override
public List<Building> search(String key, String value) throws SQLException {
if (value.equals("")) return this.buildingDao.list();
return this.buildingDao.search(key, value);
}
@Override
public void save(String name, String introduction, Integer adminId) throws SQLException {
Integer save = this.buildingDao.save(name,introduction,adminId);
if (save!=1) {
throw new RuntimeException("宿管信息添加失败");
}
}
@Override
public void update(Integer id, String name, String introduction, Integer adminId) throws SQLException {
Integer update = this.buildingDao.update(id, name, introduction, adminId);
if (update != 1) throw new RuntimeException("更新樓宇信息失敗");
}
@Override
public void delete(Integer id) throws SQLException {
//學生換宿舍
List<Integer> dormitoryIdList = this.dormitoryDao.findDormitoryIdByBuildingId(id);//通过宿舍楼找里面的所有宿舍
for (Integer dormitoryId : dormitoryIdList) { //遍历每个宿舍
List<Integer> studentManagerIdList = this.studentManagerDao.findStudentManagerByDormitoryId(dormitoryId); //通过宿舍找宿舍里的所有学生
for (Integer studentManagerId : studentManagerIdList) { //对学生遍历
Integer availableId = this.dormitoryDao.availableId(); //找到所有空宿舍
Integer updateDormitory = this.studentManagerDao.updateDormitory(studentManagerId, availableId); //把学上调到有空宿舍
Integer subAvailable = this.dormitoryDao.subAvailable(availableId); //有空宿舍减去一个空床位
if (updateDormitory != 1 || subAvailable != 1) throw new RuntimeException("学生更新宿舍失败");
}
}
// 删除宿舍
for (Integer integer : dormitoryIdList) {
Integer delete = this.dormitoryDao.delete(integer);
if (delete != 1) throw new RuntimeException("宿舍信息删除失敗");
}
//删除楼宇
Integer delete = this.buildingDao.delete(id);
if (delete !=1 ) throw new RuntimeException("楼宇信息删除失败");
}
}
控制层:负责接收页面请求数据,然后进行方法分发,执行不同service方法
package com.cainiao.web;
import com.cainiao.dto.DormitoryAdminDto;
import com.cainiao.dto.SystemAdminDto;
import com.cainiao.service.DormitoryAdminService;
import com.cainiao.service.impl.DormitoryAdminServiceImpl;
import com.cainiao.service.impl.SystemAdminServiceImpl;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;
@WebServlet("/account")
public class AccountServlet extends HttpServlet {
SystemAdminServiceImpl systemAdminService = new SystemAdminServiceImpl();
DormitoryAdminService dormitoryAdminService = new DormitoryAdminServiceImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8"); //设置编码格式,防止中文乱码
String type = request.getParameter("type"); //获取登录账户类型
System.out.println(type);
// String registerType = request.getParameter("registerType");//获取注册账户类型
// System.out.println(registerType);
String method =request.getParameter("method");
String username = request.getParameter("username"); //获取登录界面账号
String password = request.getParameter("password"); //获取登录界面密码
String dormitoryUsername = request.getParameter("dormitoryUsername"); //获取注册界面账号
String dormitoryPassword = request.getParameter("dormitoryPassword"); //获取注册界面密码
String dormitoryName = request.getParameter("dormitoryName"); //获取注册界面姓名
String dormitoryGender = request.getParameter("dormitoryGender"); //获取注册界面性别
System.out.println(dormitoryGender);
String dormitoryTelephone = request.getParameter("dormitoryTelephone"); //获取注册界面电话
/*对登录用户类型进行判断*/
if(type.equals("systemAdmin")){ //管理员类型管理员
switch(method){
case "logout":
request.getSession().invalidate(); //销毁session,跳转页面
response.sendRedirect("login.jsp");
break;
case "login":
try {
SystemAdminDto systemAdminDto = systemAdminService.login(username, password);
//判断用户名是否存在,进行信息反馈
switch(systemAdminDto.getCode()){
case -1:
request.setAttribute("usernameError","用户名不存在");
request.getRequestDispatcher("login.jsp").forward(request,response);
break;
case 0:
request.setAttribute("passwordError","密码错误");
request.getRequestDispatcher("login.jsp").forward(request,response);
break;
case 1:
System.out.println("登录成功");
HttpSession session = request.getSession();
session.setAttribute("systemAdmin",systemAdminDto.getSystemAdmin());
session.setAttribute("type",type);
response.sendRedirect("systemadmin.jsp");
break;
}
} catch (SQLException e) {
e.printStackTrace();
}
break;
}
}else{ // 楼管类型管理员
switch(method){
case "dormitoryCheckUsername": //用ajax异步核对是否存在重复账号
try {
DormitoryAdminDto dormitoryAdminDto = systemAdminService.dormitoryCheckUsername(dormitoryUsername);
//判断用户名是否存在,进行信息反馈
switch(dormitoryAdminDto.getCode()){
// case -1:
// request.setAttribute("usernameError","用户名存在");
// response.getWriter().write("-1");
request.getRequestDispatcher("login.jsp").forward(request,response);
// break;
case 0:
response.getWriter().write("0");
// request.getRequestDispatcher("login.jsp").forward(request,response);
break;
case 1:
response.getWriter().write("1");
break;
}
} catch (SQLException e) {
e.printStackTrace();
}
break;
case "dormitorySave": //把注册好的账号进行保存
try {
Integer integer = systemAdminService.dormitorySave(dormitoryUsername, dormitoryPassword, dormitoryName, dormitoryGender, dormitoryTelephone);
response.getWriter().write("注册成功,请登录");
response.sendRedirect("login.jsp");
} catch (SQLException e) {
e.printStackTrace();
}
break;
case "login": //楼管登录
try {
DormitoryAdminDto login = dormitoryAdminService.login(username, password);
//判断用户名是否存在,进行信息反馈
switch(login.getCode()){
case -1:
request.setAttribute("usernameError","用户名不存在");
request.getRequestDispatcher("login.jsp").forward(request,response);
break;
case 0:
request.setAttribute("passwordError","密码错误");
request.getRequestDispatcher("login.jsp").forward(request,response);
break;
case 1:
HttpSession session = request.getSession();
session.setAttribute("dormitoryAdmin",login.getDormitoryAdmin());
session.setAttribute("type","dormitoryAdmin");
response.sendRedirect("systemadmin.jsp");
break;
}
} catch (SQLException e) {
e.printStackTrace();
}
break;
case "logout":
request.getSession().invalidate(); //销毁session,跳转页面
response.sendRedirect("login.jsp");
break;
}
}
// System.out.println(request.getContextPath());
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
页面视图层view
//点击登录按钮将表单数据以post方式发送到名字为account的servlet那里
<form class="form-horizontal col-md-offset-3" id="login_form" action="<%= request.getContextPath()%>/account?method=login" method="post"> <%--request.getContextPath() /web-demo --%>
<h3 class="form-title">用户登录</h3>
<div class="col-md-9">
<div class="form-group">
<i class="fa fa-user fa-lg"></i>
<span style="color: red;font-size: 13px;margin-left: -17px;">${usernameError}</span>
<input class="form-control required" required placeholder="请输入用户名" type="text" name="username"/>
</div>
<div class="form-group">
<i class="fa fa-lock fa-lg"></i>
<span style="color: red;font-size: 13px;margin-left: -17px;">${passwordError}</span>
<input class="form-control required" required placeholder="请输入密码" type="password" name="password"/>
</div>
<div class="form-group">
<label class="radio-inline">
<input type="radio" name="type" checked value="systemAdmin" class="radio-inline"> 系统管理员
</label>
<label class="radio-inline">
<input type="radio" name="type" value="dormitoryAdmin" class="radio-inline" > 宿舍管理员
</label>
</div>
<div class="form-group col-md-offset-9">
<button type="submit" class="btn btn-success pull-left" name="submit">登录</button>
<button type="reset" class="btn btn-success pull-right" name="submit">重置</button>
<a href="register.html" style="cursor: pointer;font-size: small">没有账号,点击注册</a>
</div>
</div>
</form>