欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MVC模式信息查询

程序员文章站 2024-03-16 21:59:34
...

1.查询:
(1)Userinfo数据库
MVC模式信息查询
(2)user.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"  
        pageEncoding="UTF-8"%>  
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <title>用户信息</title>  
    <link rel="stylesheet" href="/dabaMVC/resources/css/bootstrap.min.css">  
    <script src="/dabaMVC/resources/js/jquery.js"></script>  
    <script src="/dabaMVC/resources/js/bootstrap.min.js"></script>  
    </head>  
    <body>  
        <div class="col-md-2">  
            <form action="queryUser.do" method="post">  
                <input type="text" size=1 name="userId" class="form-control"  
                    placeholder="请输入用户编号" /> <input type="submit" value="查询"  
                    class="btn btn-success" />  
            </form>  
        </div>  
        <c:if test="${!empty requestScope.users }">  
            <table class="table table-hover">  
                <thead>  
                    <tr>  
                        <th>用户编号</th>  
                        <th>用户名</th>  
                        <th>密码</th>  
                    </tr>  
                </thead>  
                <c:forEach items="${requestScope.users}" var="user">  
                    <tbody>  
                        <tr>  
                            <td>${user.id}</td>  
                            <td>${user.name}</td>  
                            <td>${user.password}</td>  
                        </tr>  
                    </tbody>  
                </c:forEach>   
            </table>  
        </c:if>  
    </body>  
    </html>

(3)Dao,DaoImpl

    public interface UserDao {  
        public List<User> getAll();  
    }  
    public class UserDaoImpl extends DAO<User> implements UserDao {  

        @Override  
        public List<User> getAll() {  
            String sql="SELECT * FROM userinfo";  
            return getForList(sql);  
        }  
    }  

(4)servlet

private void queryUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{  
List<User> users=userDao.getAll();   request.setAttribute("users", users);              request.getRequestDispatcher("/jsp/user.jsp").forward(request, response);
}  

运行效果如下:
MVC模式信息查询
(5)domain较容易未展示
2.模糊查询(还以用户表为例)
(1)CriteriaUser.java(最好重建)

    public class CriteriaUser {  
        private String name;  

        public String getName() {  
            if (name==null) {             
                name="%%";  
            }  
            else {  
                name="%"+name+"%";  
            }  
            return name;  
        }  

        public void setName(String name) {  
            this.name = name;  
        }  

        @Override  
        public String toString() {  
            return "CriteriaUser [name=" + name + "]";  
        }  

        public CriteriaUser(String name) {  
            super();  
            this.name = name;  
        }  
(2)jsp页面同上,表单换成对name操作(string)
(3)Dao,DaoImpl
import java.util.List;  

import com.dabaMVC.model.CriteriaUser;  
import com.dabaMVC.model.Cyg;  
import com.dabaMVC.model.User;  

public interface UserDao {  
    public List<User> getAll();  

    //获取重复字段  
    public long getCountWithName(String name);  
    //模糊查询  
    public List<User> getForListWithUser(CriteriaUser criteriaUser);  
}  
public class UserDaoImpl extends DAO<User> implements UserDao {  

    @Override  
    public List<User> getAll() {  
        String sql="SELECT * FROM userinfo";  
        return getForList(sql);  
    }  

    @Override  
    public User query(Integer id) {  
        String sql = "SELECT * FROM userinfo WHERE id = ?";  
        return query(sql, id);  
    }  

    @Override  
    public long getCountWithName(String name) {  
        String sql="select count(name) from userinfo where name = ? ";  
        return getForValue(sql,name);     
    }  
    @Override  
    public List<User> getForListWithUser(CriteriaUser criteriaUser) {  
        String sql="select * from userinfo where name like ? ";  
        return getForList(sql, criteriaUser.getName());  
    }  

}  
(4)servlet
private void queryCriteriaUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{  
    request.setCharacterEncoding("UTF-8");  
    String name=request.getParameter("name");  
    CriteriaUser criteriaUser=new CriteriaUser(name);  
    List<User> users=userDao.getForListWithUser(criteriaUser);  
    request.setAttribute("users",users);  
    request.getRequestDispatcher("/jsp/user.jsp").forward(request, response);  

   }  

“`

运行效果如下:
MVC模式信息查询