package dao;
import pojo.User;
import util.BaseDao;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class UserDaoImpl extends BaseDao implements UserDao{
@Override
public List<User> queryAll(int pageSize,int pageNo,String condition) {
String sql = "select * from t_user where 1=1 "+ condition +"limit ?,?";
//select * from t_user where 1=1 模糊查询 若是不可能有返回值
Object [] obj = {(pageNo-1)*pageSize,pageSize};
List<User> list = new ArrayList<>();
try {
ResultSet select = this.select(sql, obj);
while (select.next()){
User user = new User();
user.setId(select.getInt(1));
user.setUsername(select.getString(2));
user.setPassword(select.getString(3));
user.setPhone(select.getString(4));
user.setSex(select.getString(6));
user.setMail(select.getString(7));
list.add(user);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
@Override
public int selectDataCount(String condition) {
String sql = "select count(*) from t_user where 1=1 "+condition;
Object[] obj = {};
int count = 0;
try {
ResultSet set = this.select(sql,obj);
while (set.next()){
count = set.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
@Override
public int addUser(User user) {
String sql = "insert into t_user (username,sex,phone,mail) values (?,?,?,?)";
Object [] obj = {
user.getUsername(),
user.getSex(),
user.getPhone(),
user.getMail(),
};
int i = this.setUpdate(sql, obj);
// //成功 返回i=1
return i;
}
@Override
public User selectById(int id) {
String sql = "select * from t_user where id=?";
Object [] obj ={id};
User user = new User();
try {
ResultSet set = this.select(sql,obj);
while (set.next()){
user.setId(set.getInt(1));
user.setUsername(set.getString(2));
user.setPassword(set.getString(3));
user.setPhone(set.getString(4));
user.setSex(set.getString(6));
user.setMail(set.getString(7));
}
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
@Override
public int UpdateById(User user) {
String sql = "update t_user set username=?,sex=?,phone=?,mail=? where id = ?";
Object [] obj = {
user.getUsername(),
user.getSex(),
user.getPhone(),
user.getMail(),
user.getId()
};
int i = this.setUpdate(sql,obj);
return i;
}
@Override
public int ResetById(int id) {
String sql = "update t_user set password ='liuyuanxiang' where id = ?";
Object [] obj = {id};
int i = this.setUpdate(sql, obj);
return i;
}
@Override
public int DelectById(int id) {
String sql = "delete from t_user where id = ?";
Object [] obj = {id};
int i = this.setUpdate(sql,obj);
return i;
}
@Override
public int Addnew(String name, String password, String phone, String mail) {
String sql = "insert into t_user(username,password,phone,mail) values(?,?,?,?)";
Object[] obj = {name, password,phone,mail};
int i = this.setUpdate(sql, obj);
return i;
}
}