wjy_branch
wangjiaoyan 3 weeks ago
parent 12f6fe2a18
commit 20ebba0694

@ -10,56 +10,77 @@ import java.util.List;
* @create 2020-08-21 15:04 * @create 2020-08-21 15:04
*/ */
public class UserDaoImpl extends BaseDao implements UserDao { public class UserDaoImpl extends BaseDao implements UserDao {
// 根据用户名查询用户信息
@Override @Override
public User querybyUsername(String username) { public User querybyUsername(String username) {
// SQL 查询语句,选择 t_user 表中的相关字段
String sql = "select `id`,`username`,`password`,`email`,`address` from t_user where username = ?"; String sql = "select `id`,`username`,`password`,`email`,`address` from t_user where username = ?";
return queryForOne(User.class,sql,username); // 执行查询并返回单个 User 对象
return queryForOne(User.class, sql, username);
} }
// 根据用户名和密码查询用户信息
@Override @Override
public User querybyUsernameAndPassword(String username, String password) { public User querybyUsernameAndPassword(String username, String password) {
// SQL 查询语句,通过用户名和密码查找用户
String sql = "select * from t_user where username = ? and password = ?"; String sql = "select * from t_user where username = ? and password = ?";
return queryForOne(User.class,sql,username,password); // 执行查询并返回单个 User 对象
return queryForOne(User.class, sql, username, password);
} }
// 保存用户信息
@Override @Override
public int saveUser(User user) { public int saveUser(User user) {
// SQL 插入语句,向 t_user 表中插入新的用户数据
String sql = "insert into t_user(`username`,`password`,`email`,`address`) values(?,?,?,?)"; String sql = "insert into t_user(`username`,`password`,`email`,`address`) values(?,?,?,?)";
return update(sql,user.getUsername(),user.getPassword(),user.getEmail(),user.getAddress()); // 执行更新操作,返回受影响的行数
return update(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getAddress());
} }
// 添加用户信息(功能与 saveUser 相同)
@Override @Override
public int addUser(User user) { public int addUser(User user) {
// SQL 插入语句,向 t_user 表中插入新的用户数据
String sql = "insert into t_user(`username`,`password`,`email`,`address`) values(?,?,?,?)"; String sql = "insert into t_user(`username`,`password`,`email`,`address`) values(?,?,?,?)";
return update(sql,user.getUsername(),user.getPassword(),user.getEmail(),user.getAddress()); // 执行更新操作,返回受影响的行数
return update(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getAddress());
} }
// 根据用户 ID 删除用户信息
@Override @Override
public int deleteUserById(int i) { public int deleteUserById(int i) {
// SQL 删除语句,根据用户 ID 删除对应的记录
String sql = "delete from t_user where id = ?"; String sql = "delete from t_user where id = ?";
return update(sql,i); // 执行更新操作,返回受影响的行数
return update(sql, i);
} }
// 更新用户信息
@Override @Override
public int updateUser(User user) { public int updateUser(User user) {
// SQL 更新语句,根据用户 ID 更新用户的各项信息
String sql = "update t_user set `username`=?,`password`=?,`email`=?,`address`=? where id=?"; String sql = "update t_user set `username`=?,`password`=?,`email`=?,`address`=? where id=?";
return update(sql,user.getUsername(),user.getPassword(),user.getEmail(),user.getAddress(),user.getId()); // 执行更新操作,返回受影响的行数
return update(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getAddress(), user.getId());
} }
@Override @Override
public User queryUserById(Integer id) { public User queryUserById(Integer id) {
// 根据用户ID查询单个用户信息
String sql = "select * from t_user where id=?"; String sql = "select * from t_user where id=?";
return queryForOne(User.class,sql,id); return queryForOne(User.class, sql, id);
} }
@Override @Override
public List<User> queryUsers() { public List<User> queryUsers() {
// 查询所有用户信息
String sql = "select * from t_user"; String sql = "select * from t_user";
return queryForList(User.class,sql); return queryForList(User.class, sql);
} }
@Override @Override
public Integer queryForPageTotalCount() { public Integer queryForPageTotalCount() {
// 查询用户表中总记录数,以便进行分页
String sql = "select count(*) from t_user"; String sql = "select count(*) from t_user";
Number count = (Number) queryForSingleValue(sql); Number count = (Number) queryForSingleValue(sql);
return count.intValue(); return count.intValue();
@ -67,7 +88,9 @@ public class UserDaoImpl extends BaseDao implements UserDao {
@Override @Override
public List<User> queryForPageItems(int begin, int pageSize) { public List<User> queryForPageItems(int begin, int pageSize) {
// 通过指定的偏移量和页面大小查询特定页的用户信息
String sql = "select * from t_user limit ?,?"; String sql = "select * from t_user limit ?,?";
return queryForList(User.class,sql,begin,pageSize); return queryForList(User.class, sql, begin, pageSize);
} }
} }

Loading…
Cancel
Save