删除手写JDBC部分代码
This commit is contained in:
		@@ -1,16 +0,0 @@
 | 
			
		||||
package com.example.springdemo.dao.InterfaceDao;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
public interface AbstractDao<T> {
 | 
			
		||||
 | 
			
		||||
    int insert(T t); //增
 | 
			
		||||
 | 
			
		||||
    int delete(T t);//删
 | 
			
		||||
 | 
			
		||||
    int update(T t);//改
 | 
			
		||||
 | 
			
		||||
    List<T> search(T t);//查
 | 
			
		||||
 | 
			
		||||
    T searchID(Long id);//查 ByID
 | 
			
		||||
}
 | 
			
		||||
@@ -1,7 +0,0 @@
 | 
			
		||||
package com.example.springdemo.dao.InterfaceDao;
 | 
			
		||||
 | 
			
		||||
import com.example.springdemo.dao.InterfaceDao.AbstractDao;
 | 
			
		||||
import com.example.springdemo.entities.Users;
 | 
			
		||||
 | 
			
		||||
public interface UsersDao extends AbstractDao<Users> {
 | 
			
		||||
}
 | 
			
		||||
@@ -1,28 +0,0 @@
 | 
			
		||||
package com.example.springdemo.dao.impl;
 | 
			
		||||
 | 
			
		||||
import com.example.springdemo.utils.DataBaseUtil;
 | 
			
		||||
 | 
			
		||||
import java.sql.Connection;
 | 
			
		||||
import java.sql.PreparedStatement;
 | 
			
		||||
import java.sql.ResultSet;
 | 
			
		||||
import java.sql.SQLException;
 | 
			
		||||
 | 
			
		||||
public class CommonDao {
 | 
			
		||||
    DataBaseUtil dataBaseUtil = new DataBaseUtil();
 | 
			
		||||
    public int delete(Connection ct, PreparedStatement ps, ResultSet rs,
 | 
			
		||||
                             String sql, Long id) {
 | 
			
		||||
        int flag;
 | 
			
		||||
        try {
 | 
			
		||||
            ct = dataBaseUtil.getConnection();
 | 
			
		||||
            // FIXME: 未对传入sql语句检测,可能有sql注入攻击
 | 
			
		||||
            ps = ct.prepareStatement(sql);
 | 
			
		||||
            ps.setLong(1, id);
 | 
			
		||||
            flag = ps.executeUpdate();
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            dataBaseUtil.close(ct, ps, rs);
 | 
			
		||||
        }
 | 
			
		||||
        return flag;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,138 +0,0 @@
 | 
			
		||||
package com.example.springdemo.dao.impl;
 | 
			
		||||
 | 
			
		||||
import com.example.springdemo.dao.InterfaceDao.UsersDao;
 | 
			
		||||
import com.example.springdemo.entities.Users;
 | 
			
		||||
import com.example.springdemo.utils.DataBaseUtil;
 | 
			
		||||
import org.jetbrains.annotations.NotNull;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
 | 
			
		||||
import java.sql.Connection;
 | 
			
		||||
import java.sql.PreparedStatement;
 | 
			
		||||
import java.sql.ResultSet;
 | 
			
		||||
import java.sql.SQLException;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
@Component
 | 
			
		||||
public class UsersDaoImpl implements UsersDao {
 | 
			
		||||
    private Connection connection = null;
 | 
			
		||||
    private PreparedStatement preparedStatement = null;
 | 
			
		||||
    private ResultSet resultSet = null;
 | 
			
		||||
    DataBaseUtil dataBaseUtil = new DataBaseUtil();
 | 
			
		||||
 | 
			
		||||
    //新建用户
 | 
			
		||||
    @Override
 | 
			
		||||
    public int insert(@NotNull Users users) {
 | 
			
		||||
        int flag;
 | 
			
		||||
        try {
 | 
			
		||||
            connection = dataBaseUtil.getConnection();
 | 
			
		||||
            String insert_sql = "insert into Users(name, phoneNumber, address, password) " + "values (?,?,?,?)";
 | 
			
		||||
            preparedStatement = connection.prepareStatement(insert_sql);
 | 
			
		||||
 | 
			
		||||
            preparedStatement.setString(1, users.getName());
 | 
			
		||||
            preparedStatement.setString(2, users.getPhoneNumber());
 | 
			
		||||
            preparedStatement.setString(3, users.getAddress());
 | 
			
		||||
            preparedStatement.setString(4, users.getPassword());
 | 
			
		||||
 | 
			
		||||
            flag = preparedStatement.executeUpdate();
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            dataBaseUtil.close(connection, preparedStatement, resultSet);
 | 
			
		||||
        }
 | 
			
		||||
        return flag;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //删除
 | 
			
		||||
    @Override
 | 
			
		||||
    public int delete(@NotNull Users user) {
 | 
			
		||||
        String delete_sql = "delete from Users where id=?";
 | 
			
		||||
        CommonDao dao = new CommonDao();
 | 
			
		||||
        return dao.delete(connection, preparedStatement, resultSet,
 | 
			
		||||
                delete_sql, user.getId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //更新用户个人信息
 | 
			
		||||
    //@TODO 只能更新所有信息,不能选择性更新
 | 
			
		||||
    @Override
 | 
			
		||||
    public int update(@NotNull Users users) {
 | 
			
		||||
        int flag;
 | 
			
		||||
        String update_sql = "update Users " + "set name = ?, phoneNumber = ?, address = ? " + "where id = ?";
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            connection = dataBaseUtil.getConnection();
 | 
			
		||||
            preparedStatement = connection.prepareStatement(update_sql);
 | 
			
		||||
 | 
			
		||||
            preparedStatement.setString(1, users.getName());
 | 
			
		||||
            preparedStatement.setString(2, users.getPhoneNumber());
 | 
			
		||||
            preparedStatement.setString(3, users.getAddress());
 | 
			
		||||
            preparedStatement.setLong(4, users.getId());
 | 
			
		||||
 | 
			
		||||
            flag = preparedStatement.executeUpdate();
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        }
 | 
			
		||||
        return flag;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //查询所有用户
 | 
			
		||||
    @Override
 | 
			
		||||
    public @NotNull List<Users> search(@NotNull Users user) {
 | 
			
		||||
        List<Users> list = new ArrayList<>();
 | 
			
		||||
        String selectAll_sql = "select * from Users";
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            connection = dataBaseUtil.getConnection();
 | 
			
		||||
 | 
			
		||||
            preparedStatement = connection.prepareStatement(selectAll_sql);
 | 
			
		||||
            resultSet = preparedStatement.executeQuery();
 | 
			
		||||
 | 
			
		||||
            while (resultSet.next()) {
 | 
			
		||||
                user = new Users();
 | 
			
		||||
                SetAttribute(user, resultSet);
 | 
			
		||||
                list.add(user);
 | 
			
		||||
            }
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            dataBaseUtil.close(connection, preparedStatement, resultSet);
 | 
			
		||||
        }
 | 
			
		||||
        return list;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //用ID查询用户
 | 
			
		||||
    @Override
 | 
			
		||||
    public @NotNull Users searchID(@NotNull Long id) {
 | 
			
		||||
        Users user = null;
 | 
			
		||||
        String selectID_sql = "select * from Users where id = ?";
 | 
			
		||||
        try {
 | 
			
		||||
            connection = dataBaseUtil.getConnection();
 | 
			
		||||
            preparedStatement = connection.prepareStatement(selectID_sql);
 | 
			
		||||
            preparedStatement.setLong(1, id);
 | 
			
		||||
            resultSet = preparedStatement.executeQuery();
 | 
			
		||||
 | 
			
		||||
            while (resultSet.next()) {
 | 
			
		||||
                user = new Users();
 | 
			
		||||
                SetAttribute(user, resultSet);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        } catch (SQLException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        } finally {
 | 
			
		||||
            dataBaseUtil.close(connection, preparedStatement, resultSet);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (user == null) {
 | 
			
		||||
            throw new RuntimeException("用户不存在");
 | 
			
		||||
        } else {
 | 
			
		||||
            return user;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void SetAttribute(@NotNull Users user, @NotNull ResultSet rs) throws SQLException {
 | 
			
		||||
        user.setId(rs.getLong("id"));
 | 
			
		||||
        user.setName(rs.getString("name"));
 | 
			
		||||
        user.setPhoneNumber(rs.getString("phoneNumber"));
 | 
			
		||||
        user.setAddress(rs.getString("address"));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user