在课堂项目的基础之上实现如下需求,并测试,截图展示结果:
- 根据用户名查询用户信息
-
package com.qiku.app; import com.qiku.entity.User; import com.qiku.utils.DruidUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.junit.Test; import java.sql.SQLException; public class ZYX { // 需求 根据用户名查询用户信息 public User findUserMessage(String username) throws SQLException { QueryRunner qr = new QueryRunner(DruidUtils.dataSource); String sql = "select*from user where username = ?"; User user = qr.query(sql, new BeanHandler<User>(User.class), username); return user; } @Test public void TestFindUserMessage() throws SQLException { User userMessage = findUserMessage("渣渣辉"); System.out.println(userMessage); } }
- 查询所有的用户信息
-
package com.qiku.app; import com.qiku.entity.User; import com.qiku.utils.DruidUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.junit.Test; import java.sql.SQLException; import java.util.List; public class ZYX { // 查询所有的用户信息 public List<User> findAllUserMessage() throws SQLException { QueryRunner qr = new QueryRunner(DruidUtils.dataSource); String sql = "select*from user"; List<User> userList = qr.query(sql, new BeanListHandler<User>(User.class)); return userList; } @Test public void TestFindAllUserMessage() throws SQLException { List<User> userListMessage = findAllUserMessage(); for (User userMessage : userListMessage){ System.out.println(userMessage); } } }
- 修改指定用户的信息
package com.qiku.app;
import com.qiku.entity.User;
import com.qiku.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.junit.Test;
import java.sql.SQLException;
public class ZYX {
QueryRunner qr = new QueryRunner(DruidUtils.dataSource);
public int updateUserMessage(String username, String password) throws SQLException {
String sql = "update user set password = ? where username = ?";
int i = qr.update(sql, password, username);
return i;
}
public String findUserPassWord(String username) throws SQLException {
String sql = "select password from user where username = ?";
return qr.query(sql, new BeanHandler<User>(User.class), username).getPassword();
}
@Test
public void testLogin() throws SQLException {
String username = "渣渣辉";
String newPassword = "000000";
int i = updateUserMessage(username, newPassword);
if (i > 0) {
System.out.println("操作成功!");
} else {
System.out.println("操作失败~");
}
}
}
修改前:
修改后: