Software|简单的增删改查及数据分页


简单的增删改查及数据分页

  • 一、结构图
  • 二、相关代码
      • ① User类
      • ② Page类
      • ③ UserDao类
      • ④ CodeFilter类
      • ⑤ UserService 类
      • ⑥ IndexServlet 类
      • ⑦ UserDeleteServlet 类
      • ⑧ UserEditServlet 类
      • ⑨ edit.jsp
      • ⑩ index.jsp
      • ⑩① web.xml
      • ⑩② pom.xml
  • 三、效果截图

一、结构图 Software|简单的增删改查及数据分页
文章图片

二、相关代码 ① User类
package com.allen.entity; import java.util.Date; public class User {private Integer id; private String trueName; private String loginName; private String loginPassword; private Integer role; private Date createTime; private Date updateTime; public Integer getId() { return id; }public void setId(Integer id) { this.id = id; }public String getTrueName() { return trueName; }public void setTrueName(String trueName) { this.trueName = trueName; }public String getLoginName() { return loginName; }public void setLoginName(String loginName) { this.loginName = loginName; }public String getLoginPassword() { return loginPassword; }public void setLoginPassword(String loginPassword) { this.loginPassword = loginPassword; }public Integer getRole() { return role; }public void setRole(Integer role) { this.role = role; }public Date getCreateTime() { return createTime; }public void setCreateTime(Date createTime) { this.createTime = createTime; }public Date getUpdateTime() { return updateTime; }public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } }

② Page类
package com.allen.entity.ao; import java.util.List; public class Page { private List list; private long count; private int pageCount; private int page=1; private int size=10; public List getList() { return list; }public void setList(List list) { this.list = list; }public long getCount() { return count; }public void setCount(long count) { this.count = count; }public int getPageCount() { return pageCount; }public void setPageCount(int pageCount) { this.pageCount = pageCount; }public int getPage() { return page; }public void setPage(int page) { this.page = page; }public int getSize() { return size; }public void setSize(int size) { this.size = size; } }

③ UserDao类
package com.allen.dao; import com.allen.entity.User; import java.sql.*; import java.util.ArrayList; import java.util.Date; import java.util.List; public class UserDao {/** * 分页查询数据,模糊查询名字的数据 * * @param page * @param size * @param name * @return * @throws ClassNotFoundException * @throws SQLException */ public List find(int page, int size, String name) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql:///user-test?useSSL=false", "root", "123456"); PreparedStatement statement; if (name != null && name != "") { statement = connection.prepareStatement("select * from user where true_name like ? limit ?,?"); statement.setObject(1, "%" + name + "%"); statement.setObject(2, (page - 1) * size); statement.setObject(3, size); } else { statement = connection.prepareStatement("select * from user limit ?,?"); statement.setObject(1, (page - 1) * size); statement.setObject(2, size); } ResultSet resultSet = statement.executeQuery(); List list = new ArrayList<>(); while (resultSet.next()) { User user = new User(); user.setId(resultSet.getInt("id")); user.setTrueName(resultSet.getString("true_name")); user.setLoginName(resultSet.getString("login_name")); user.setLoginPassword(resultSet.getString("login_password")); user.setRole(resultSet.getInt("role")); //这里需要使用时间戳,才会显示年月日,时分秒 user.setCreateTime(resultSet.getTimestamp("create_time")); user.setUpdateTime(resultSet.getTimestamp("update_time")); list.add(user); } resultSet.close(); statement.close(); connection.close(); return list; }/** * 查询特定名字的人数或全部数据的数量 * * @param name * @return * @throws ClassNotFoundException * @throws SQLException */ public long count(String name) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql:///user-test?useSSL=false", "root", "123456"); PreparedStatement statement; if (name != null && name != "") { statement = connection.prepareStatement("select count(*) from user where true_name like ?"); statement.setObject(1, "%" + name + "%"); } else { statement = connection.prepareStatement("select count(*) from user"); } ResultSet resultSet = statement.executeQuery(); long count = 0; while (resultSet.next()) { count = resultSet.getLong(1); } resultSet.close(); statement.close(); connection.close(); return count; }/** * 根据ID删除对应的数据 * * @param id * @throws ClassNotFoundException * @throws SQLException */ public void deleteById(int id) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql:///user-test?useSSL=false", "root", "123456"); PreparedStatement statement = connection.prepareStatement("delete from user where id=?"); statement.setObject(1, id); int i = statement.executeUpdate(); statement.close(); connection.close(); }/** * 增加新用户 * @param user * @throws ClassNotFoundException * @throws SQLException */ public void insert(User user) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql:///user-test?useSSL=false", "root", "123456"); PreparedStatement statement = connection.prepareStatement("insert into user(true_name,login_name,login_password,role,create_time)"+" values (?,?,?,?,now())"); statement.setObject(1, user.getTrueName()); statement.setObject(2, user.getLoginName()); statement.setObject(3, user.getLoginPassword()); statement.setObject(4, user.getRole()); int i = statement.executeUpdate(); statement.close(); connection.close(); }/** * 根据ID查找用户 * @param id * @return * @throws ClassNotFoundException * @throws SQLException */ public User findById(int id) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql:///user-test?useSSL=false", "root", "123456"); PreparedStatement statement = connection.prepareStatement("select * from user where id=?"); statement.setObject(1,id); ResultSet resultSet = statement.executeQuery(); User user=null; if (resultSet.next()) { user = new User(); user.setId(id); user.setTrueName(resultSet.getString("true_name")); user.setLoginName(resultSet.getString("login_name")); user.setLoginPassword(resultSet.getString("login_password")); user.setRole(resultSet.getInt("role")); user.setCreateTime(resultSet.getTimestamp("create_time")); user.setUpdateTime(resultSet.getTimestamp("update_time")); } resultSet.close(); statement.close(); connection.close(); return user; }/** * 修改用户信息 * @param user * @throws ClassNotFoundException * @throws SQLException */ public void update(User user) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql:///user-test?useSSL=false", "root", "123456"); PreparedStatement statement = connection.prepareStatement("update user set true_name=?,login_name=?,login_password=?,role=?,update_time=now()"+" where id=?"); statement.setObject(1, user.getTrueName()); statement.setObject(2, user.getLoginName()); statement.setObject(3, user.getLoginPassword()); statement.setObject(4, user.getRole()); statement.setObject(5, user.getId()); int i = statement.executeUpdate(); statement.close(); connection.close(); } }

④ CodeFilter类
package com.allen.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebFilter("/*") public class CodeFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException {}@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse respons= (HttpServletResponse) servletResponse; request.setCharacterEncoding("UTF-8"); respons.setCharacterEncoding("UTF-8"); filterChain.doFilter(request,respons); }@Override public void destroy() {} }

⑤ UserService 类
package com.allen.service; import com.allen.dao.UserDao; import com.allen.entity.User; import com.allen.entity.ao.Page; import java.sql.SQLException; import java.util.List; public class UserService { UserDao userDao = new UserDao(); public Page page(Page page, String name) throws SQLException, ClassNotFoundException { int size = page.getSize(); List list = userDao.find(page.getPage(), size, name); page.setList(list); long count = userDao.count(name); page.setCount(count); page.setPageCount((int)((count%size==0)?(count/size):((count/size)+1))); return page; }public void delete(int id) throws SQLException, ClassNotFoundException { userDao.deleteById(id); }public void keep(User user) throws SQLException, ClassNotFoundException { if(user.getId()!=null){ userDao.update(user); }else{ userDao.insert(user); } }public User findById(int id) throws SQLException, ClassNotFoundException { return userDao.findById(id); } }

⑥ IndexServlet 类
package com.allen.servlet; import com.allen.entity.User; import com.allen.entity.ao.Page; import com.allen.service.UserService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/index") public class IndexServlet extends HttpServlet { UserService userService = new UserService(); @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { request.setCharacterEncoding("UTF-8"); Page pageAo = new Page<>(); String page = request.getParameter("page"); if (page!=null && page!="") { pageAo.setPage(Integer.parseInt(page)); } String size = request.getParameter("size"); if (size!=null && size!="") { pageAo.setSize(Integer.parseInt(size)); } Page userPage = userService.page(pageAo, request.getParameter("name")); request.setAttribute("userPage",userPage); request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request,response); }catch (Exception e){ e.printStackTrace(); } }@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {} }

⑦ UserDeleteServlet 类
package com.allen.servlet; import com.allen.service.UserService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/userDelete") public class UserDeleteServlet extends HttpServlet { UserService userService = new UserService(); @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String id = request.getParameter("id"); if (id!=null && id!="") { userService.delete(Integer.parseInt(id)); } response.sendRedirect("index"); }catch (Exception e){ e.printStackTrace(); } } }

⑧ UserEditServlet 类
package com.allen.servlet; import com.allen.entity.User; import com.allen.service.UserService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/userEdit") public class UserEditServlet extends HttpServlet { private UserService userService = new UserService(); @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String id = request.getParameter("id"); if (id!=null && id!="") { User user = userService.findById(Integer.parseInt(id)); request.setAttribute("user",user); } request.getRequestDispatcher("/WEB-INF/jsp/edit.jsp").forward(request,response); }catch(Exception e){ e.printStackTrace(); } }@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String loginName = request.getParameter("loginName"); String loginPassword = request.getParameter("loginPassword"); String role = request.getParameter("role"); String trueName = request.getParameter("trueName"); String id = request.getParameter("id"); User user = new User(); user.setLoginName(loginName); user.setLoginPassword(loginPassword); user.setRole(Integer.parseInt(role)); user.setTrueName(trueName); if (id!=null && id!="") { user.setId(Integer.parseInt(id)); } userService.keep(user); response.sendRedirect(request.getContextPath()+"/index"); }catch (Exception e){ e.printStackTrace(); } } }

⑨ edit.jsp
Title - 锐客网
用户编辑
name="role" class="form-control" id="inputRole" placeholder="角色" >
取消

⑩ index.jsp
用户管理 - 锐客网
用户管理
class="input-group-addon" onclick="findByName()">搜索
ID 姓名 登录名 密码 角色 创建时间 修改时间 编辑
${u.id} ${u.trueName} ${u.loginName} ${u.loginPassword} ${u.role==1?"超级管理员":u.role==2?"普通用户":u.role==3?"用户管理员":""} ${u.createTime} ${u.updateTime} 删除 修改
> function findByName() { let name = document.getElementById("name").value; location.href="https://www.it610.com/article/?name="+name; }

⑩① web.xml
index

⑩② pom.xml
4.0.0com.example user-test 1.0-SNAPSHOT user-test war1.8 1.8 5.7.1 javax.servlet javax.servlet-api 4.0.1 >provided org.junit.jupiter junit-jupiter-api ${junit.version} >test org.junit.jupiter junit-jupiter-engine ${junit.version} >test org.apache.maven.plugins maven-war-plugin 3.3.1

三、效果截图 Software|简单的增删改查及数据分页
文章图片

??用户添加如下:
Software|简单的增删改查及数据分页
文章图片

??用户修改如下:
【Software|简单的增删改查及数据分页】Software|简单的增删改查及数据分页
文章图片

??用户修改成功如下:
Software|简单的增删改查及数据分页
文章图片

??数据分页如下:
Software|简单的增删改查及数据分页
文章图片

??模糊查询结果如下:
Software|简单的增删改查及数据分页
文章图片

    推荐阅读