package com.tyust.dao.impl;
import com.tyust.commons.JdbcUtils;
import com.tyust.dao.UserManagerDao;
import com.tyust.pojo.Users;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
/**
* @author: Mr.Wu
* @description 用户管理接口实现类
* @date: 2021/12/16 14:45
*/
public class UserManagerDaoImpl implements UserManagerDao {
/**
* 更新用户
* @param users
*/
@Override
public void updateUser(Users users) {
Connection connection=null;
try {
connection=JdbcUtils.getConnection();
String sql="update users set username=?,usersex=?,phonenumber=?,qqnumber=? where userid=?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, users.getUsername());
ps.setString(2,users.getUsersex());
ps.setString(3,users.getPhonenumber());
ps.setString(4, users.getQqnumber());
ps.setInt(5,users.getUserid());
ps.executeUpdate();
}catch (Exception e){
e.printStackTrace();
JdbcUtils.rollbackConnection(connection);
}finally {
JdbcUtils.closeConnection(connection);
}
}
/**
* 根据用户id删除用户
* @param userid
*/
@Override
public void deleteUser(int userid) {
Connection connection=null;
try {
connection=JdbcUtils.getConnection();
String sql="delete from users where userid=?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1,userid);
ps.execute();
}catch (Exception e){
e.printStackTrace();
JdbcUtils.rollbackConnection(connection);
}finally {
JdbcUtils.closeConnection(connection);
}
}
/**
* 根据id查询用户
* @param userId
* @return
*/
@Override
public Users selectUserById(int userId) {
Connection connection=null;
Users user=null;
try {
connection=JdbcUtils.getConnection();
String sql="select * from users where userid=?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1,userId);
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()){
user=new Users();
user.setUserid(resultSet.getInt("userid"));
user.setUsername(resultSet.getString("username"));
user.setUsersex(resultSet.getString("usersex"));
user.setUserpwd(resultSet.getString("userpwd"));
user.setPhonenumber(resultSet.getString("phonenumber"));
user.setQqnumber(resultSet.getString("qqnumber"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
JdbcUtils.closeConnection(connection);
}
return user;
}
/**
* 添加用户
* @param users
*/
@Override
public void insertUser(Users users) {
Connection conn=null;
try{
conn=JdbcUtils.getConnection();
String sql="insert into users value (default,?,?,?,?,?)";
PreparedStatement ps= conn.prepareStatement(sql);
ps.setString(1, users.getUsername());
ps.setString(2, users.getUserpwd());
ps.setString(3, users.getUsersex());
ps.setString(4, users.getPhonenumber());
ps.setString(5, users.getQqnumber());
ps.execute();
}catch (Exception e){
e.printStackTrace();
JdbcUtils.rollbackConnection(conn);
}finally {
JdbcUtils.closeConnection(conn);
}
}
/**
* 查询用户
* @param users
* @return
*/
@Override
public List<Users> selectUserByProperty(Users users) {
Connection connection=null;
List<Users> list=new ArrayList<>();
try{
connection = JdbcUtils.getConnection();
String sql=this.createSql(users);
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()){
Users user=new Users();
user.setUserid(resultSet.getInt("userid"));
user.setUsername(resultSet.getString("username"));
user.setUsersex(resultSet.getString("usersex"));
user.setUserpwd(resultSet.getString("userpwd"));
user.setPhonenumber(resultSet.getString("phonenumber"));
user.setQqnumber(resultSet.getString("qqnumber"));
list.add(user);
}
}catch (Exception e){
e.printStackTrace();
}finally {
JdbcUtils.closeConnection(connection);
}
return list;
}
/**
* 拼接SQL语句
* @param users
* @return
*/
private String createSql(Users users) {
StringBuffer stringBuffer=new StringBuffer("select * from users where 1=1 ");
if (users.getUsername()!=null&& users.getUsername().length()>0){
stringBuffer.append(" and username= '"+users.getUsername()+"'");
}
if (users.getUsersex()!=null&&users.getUsersex().length()>0){
stringBuffer.append(" and usersex='"+users.getUsersex()+"'");
}
if (users.getQqnumber()!=null&& users.getQqnumber().length()>0){
stringBuffer.append(" and qqnumber='"+users.getQqnumber()+"'");
}
if (users.getPhonenumber()!=null&&users.getPhonenumber().length()>0){
stringBuffer.append(" and phonenumber='"+users.getPhonenumber()+"'");
}
System.out.println(stringBuffer.toString());
return stringBuffer.toString();
}
}