您的位置:首页 > 其它

用户列表基本页面搭建(框架流程、分页)

2016-06-01 18:03 204 查看
用户管理(查询用户)

1、以user_phone作为账号,所以要在表中将user_phone设为唯一性unique

设计表 --> 索引 --> 索引类型unique 

2、先写UserInfoService和UserInfoDAO,先进行单元测试

   1>在UserInfoDAO中增加一个查询用户信息的方法返回用户列表

/**
* 根据条件查询用户信息
* @param user 查询条件
* @return
*/
public List<UserInfo> getUserList(UserInfo user);


   2>在对应的映射文件UserInfoDAO.xml中添加映射(注册sql)

       注意:不要用*,要把属性罗列出来

  

     定义查询返回的结果类型resultMap:(这些代码可自动生成)
<resultMap type="UserInfo" id="usermap">
<id column="user_id" property="userId"/>
<result column="user_name" property="userName"/>
<result column="user_sex" property="userSex"/>
<result column="user_phone" property="userPhone"/>
<result column="user_pw" property="userPw"/>
<result column="user_type" property="userType"/>
</resultMap>


    注册查询的sql:(注意,要把配置好的结果类型通过resultMap引入)
<select id="getUserList" parameterType="UserInfo"

resultMap="usermap">
select * from user_info
<where>
<!--当userType不为空时才加上if内的条件-->
<if test="userType != null">
and user_type = #{userType}
</if>
<if test="userName != null">
and user_name like #{userName}
</if>
</where>
</select>


    3>编写IUserInfoService

/**
* 根据条件查询用户信息
* @param user 查询条件
* @return
*/
public List<UserInfo> getList(UserInfo user);


 

    4>修改实现类UserInfoServiceImpl,添加刚才的查询方法

public List<UserInfo> getList(UserInfo user) {
//user不为空
if (user != null) {
//用户名不为空
if (user.getUserName() != null && !user.getUserName

().equals("")) {
user.setUserName("%" + user.getUserName()
4000
+ "%");//模糊查

询
}
}
return userdao.getUserList(user);
}


   

    5>进行测试(每写一个模块的dao,要进行单元测试,通过再和前端整合

)

    UserTest:

@Test
public void testlist(){
UserInfo user = new UserInfo();
user.setUserName("华");
//调用接口中的方法进行查询
List<UserInfo> list = service.getList(user);

for(UserInfo u : list){
System.out.println(u);
}
}


     但是当:

                 user.setUserType("");

执行成功,但无记录查询出来

   

        修改:(and userType != '')

               <where>
       <if test="userType != null and userType != ''">
           and user_type = #{userType}
       </if>

3、当编写好UserInfoService和UserInfoDAO,并进行单元测试成功后,

   编写UserInfoController控制页面跳转并把IUserInfoService接口中的 

  数据提取到显示页面

@Autowired
private IUserInfoService userservice;

public String list(UserInfo user,Model model){
List<UserInfo> list = userservice.getList(user);

//把接口中获得的数据进行model中携带到前端页面
model.addAttribute("list", list);

return "userinfo/userinfo_list";
}


4、在前端页面userinfo_list.jsp中:

        1>${list}即可取值

        2>引用标签库:
<%@ taglib prefix="c"

uri="http://java.sun.com/jsp/jstl/core"%>


 

        3>进行循环遍历输出list中的值:
</pre><pre name="code" class="html">	<c:forEach items="${list }" var="user">
<tr>
<td>${user.userId }</td>
<td>${user.userName }</td>
<td>${user.userSex }</td>
<td>${user.userPhone }</td>
<td>${user.userPw }</td>
<td>${user.userType }</td>
<td><a href="user/loadupdate.do?userId=${user.userId }">

修改</a>
<a href="javascript:void(0)" onclick="del

('${user.userId}')">删除</a>

</td>
</tr>
</c:forEach>


5、根据名字查询,则利用到了配置文件中的
<select id="getUserList" parameterType="UserInfo"

resultMap="usermap">
select * from user_info
<where>
<!--当userType不为空时才加上if内的条件-->
<if test="userType != null">
and user_type = #{userType}
</if>


   当用户输入名字进行查询时,名字不为空则按名字进行查询;当用户不输 

   入名字时,则名字为空,就不加上名字这个查询条件,所以就查询出全 

   部

  

6、对查询结果进行分页:

   select * from user_info limit 0,10

   由于每个实体类都需要对结果进行分页显示,进行提取做utils,进行继  

 承

   

   1>utils --> BaseBean.java

     然后每个实体类对BaseBean进行继承

package cn.zy.utils;

public class BaseBean {

//起始记录数
private int start;

//每次查询条数
private int length;

public int getStart() {
return start;
}

public void setStart(int start) {
this.start = start;
}

public int getLength() {
return length;
}

public void setLength(int length) {
this.length = length;
}

}


   2>对UserInfoDAO.xml进行配置修改:

     添加:
order by user_id desc
limit #{start},#{length}


    3>utils --> BaseController.java

      处理分页,对各个页面进行初始化分页信息

 
package cn.zy.utils;

import javax.servlet.http.HttpServletRequest;

public class BaseController {
/**
* 起始记录数
*/
private int pageNo = 0;

/**
* 总记录数
*/
private long total;

/**
* 每页显示记录数
*/
public final static  int PAGE_NUM_BIG = 10;

/**
* 初始化分页信息
*/
public void initPage(HttpServletRequest request){
String page_str = request.getParameter

("pager.offset");
if(page_str!=null && !page_str.equals("")){
pageNo = Integer.parseInt(page_str);
}
}

public int getPageNo() {
return pageNo;
}

public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}

public long getTotal() {
return total;
}

public void setTotal(long total) {
this.total = total;
}

public static int getPageNumBig() {
return PAGE_NUM_BIG;
}
}


   4>如果要分页,则继承BaseController

     UserInfoController继承BaseController

   5>UserInfoController.java:

     添加参数HttpServletRequest request
if(user!=null){
this.initPage(request);
user.setStart(this.getPageNo());//起始记录数
user.setLength(PAGE_NUM_BIG);//每页显示记录数
}


   还差总记录数,才能初始化分页

   所以dao层还需要添加一个查询总记录数的方法

   6>UserInfoDAO.java:(需要添加参数user,因为按条件分页要用到)
/**
* 获取总记录数
* @param user
* @return
*/
public long gercount(UserInfo user);


   7>对应的,在UserInfoDAO.xml中配置相关sql

         <!--这里返回类型为long  -->
<select id="getcount" parameterType="UserInfo"

resultType="long">
select conut(1) as total from user_info
<where>
<if test="userName != null and userName != ''">
user_name like #{userName}
</if>
</where>
</select>


    注意这里返回类型为long,用resultType声明   

    8>对应的,在service层IUserInfoService添加查询总记录数的方法

    9>对应的,在service的实现层UserInfoServiceImpl中添加方法

    10>在UserInfoController中

      this.setTotal(userservice.getcount(user));//获取总记录数

 

    11>前端的分页设置res/jsp/pager_tag.jsp

       在对应页面调用分页:

       1、引入分页标签(pom.xml中引入了开源分页标签)
<%@ taglib prefix="pg"

uri="http://jsptags.com/tags/navigation/pager"%>


      

       2、在应用处添加如下代码
<div align="right" style="padding: 10px;">
<pg:pager items="${total }" url="user/list.do"

maxIndexPages="3"

export="currentPageNumber=pageNumber" scope="request">
<!-- 分页条件 -->
<pg:param name="userName" value="${userName }" />
<!-- 将分页模板jsp页面包含进来-->
<jsp:include page="../../../res/jsp/pager_tag.jsp"

flush="true" />
</pg:pager>
</div>


        "../../../res/jsp/pager_tag.jsp"意思为,往上返回三层目录  

      结构到res再取得模板jsp

     12>单元测试testcount()

     13>注意UserInfoController要用model向页面返回值!!

     
model.addAttribute("total", userservice.getcount(user));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: