模糊查询
在allUser中加入搜索表单
写web
<servlet>
<servlet-name>SearchServlet</servlet-name>
<servlet-class>com.zhongruan.servlet.SearchServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SearchServlet</servlet-name>
<url-pattern>/search</url-pattern>
</servlet-mapping>
写SearchServlet
public class SearchServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
IUserService userService=new UserServiceImpl();
String username=req.getParameter("username");
List<User> users=userService.search(username);
HttpSession session=req.getSession();
PageInfo pageInfo=new PageInfo();
pageInfo.setList(users);
session.setAttribute("pageInfo",pageInfo);
req.getRequestDispatcher("/allUser.jsp").forward(req,resp);
}
}
在IUserDao中写search
@Override
public List<Consumer> search(String username) {
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
ListConsuerr>consumers = new ArrayList<>();
try {
connection = DBUtil.getConnection();
String sql = "select * from consumer where name like ?";
statement = connection.prepareStatement(sql);
statement.setString(1,"%"+username+"%");
resultSet = statement.executeQuery();
while (resultSet.next()) {
Consumer consumer = new Consumer();
consumer.setId(resultSet.getInt(1));
consumer.setName(resultSet.getString(2));
consumer.setPassword(resultSet.getString(3));
consumers.add(consumer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeALL(resultSet, statement, connection);
}
return consumers;
}
在UserServicelmpl中添加以下代码
@Override
public List<consumer> search(String name) {
return iConsumerDao.search(name);
}