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

Posted faramita_of_mine

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的增删改查及数据分页相关的知识,希望对你有一定的参考价值。

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

一、结构图

二、相关代码

① 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<T> 
    private List<T> list;
    private long count;
    private int pageCount;
    private int page=1;
    private int size=10;

    public List<T> getList() 
        return list;
    

    public void setList(List<T> 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<User> 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<User> 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<User> page(Page<User> page, String name) throws SQLException, ClassNotFoundException 
        int size = page.getSize();
        List<User> list = userDao.find(page.<

以上是关于简单的增删改查及数据分页的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot-Vue实现增删改查及分页小DEMO

04-springboot整合elasticsearch初识-简单增删改查及复杂排序,分页,聚合操作

python中字典的增删改查及相关知识点

列表(list)的增删改查及操作方法和元组(tuple)的查

Python集合 (set) 的增删改查及 copy()方法

python课堂整理34----类的增删改查及实例属性增删改查