ssm框架查询数据并实现分页功能示例

来源:互联网 发布:怎么看淘宝代购真假 编辑:程序博客网 时间:2024/04/29 17:57

    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * DataGrid对象
 *
 */
@SuppressWarnings("rawtypes")
public class DataGrid {
    private int total = 0;
    private List rows = new ArrayList();
    public int getTotal() {
        return total;//数据总数
    }
    public void setTotal(int total) {
        this.total = total;
    }
    public List getRows() {
        return rows;//页面显示的数据
    }
    public void setRows(List rows) {
        this.rows = rows;
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class UserInfoDO {
    private String userName;// 用户姓名
    private String phoneNum;//用户电话
    private String identityNum;//卡号
    private String plStatus;//插件状态
    private int startNum;//分页-起始数
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPhoneNum() {
        return phoneNum;
    }
    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }
    public String getIdentityNum() {
        return identityNum;
    }
    public void setIdentityNum(String identityNum) {
        this.identityNum = identityNum;
    }
    public String getPlStatus() {
        return plStatus;
    }
    public void setPlStatus(String plStatus) {
        this.plStatus = plStatus;
    }
    public int getStartNum() {
        return startNum;
    }
    public void setStartNum(int startNum) {
        this.startNum = startNum;
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    /**
     * 查询
     *
     * @return
     */
    @RequestMapping(value = "/userInfo.do", method = RequestMethod.POST)
    @ResponseBody
    public String getList(@RequestBody UserInfoDO userInfoDo, HttpServletRequest request, HttpServletResponse response) {
        JSONObject jsonObject = new JSONObject();
        try {
            DataGrid dataGrid = userInfoService.getUserInfo(userInfoDo);
            jsonObject = JSONObject.fromObject(dataGrid);
        catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject.toString();
        }
}

  

1
2
3
public interface UserInfoService {
    public DataGrid getUserInfo(UserInfoDO userInfoDo) throws SQLException;
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Service
public class SignLogServiceImpl implements SignLogService {
    @Resource
    private SignLogDao signLogDao;
    @Override
    public DataGrid getSignLog(SignLogDO signLogDo) throws SQLException {
        DataGrid grid = new DataGrid();
        List<SignLogDO> signLogList = null;
        try {
            signLogList = signLogDao.getSignLog(signLogDo);
            grid.setTotal(signLogDao.getTotalNum(signLogDo));
            grid.setRows(signLogList);
        }
        catch (Exception e) {
        e.printStackTrace();
        }
        return grid;
    }
}

  

1
2
3
4
5
6
7
public interface UserInfoDao {
 
    public List<UserInfoDO> getUserInfo(UserInfoDO userInfoDo) throws SQLException;
 
    public int getTotalNum(UserInfoDO userInfoDo) throws SQLException;
 
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Repository
public class UserInfoDaoImpl extends SqlSessionDaoSupport implements UserInfoDao{
    @Resource
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
        super.setSqlSessionFactory(sqlSessionFactory);
    }
    @Override
    public List<UserInfoDO> getUserInfo(UserInfoDO userInfoDo) throws SQLException {
        return this.getSqlSession().selectList("userInfoDO.getUserInfo",userInfoDo);
    }
    @Override
    public int getTotalNum(UserInfoDO userInfoDo) throws SQLException {
        return this.getSqlSession().selectOne("userInfoDO.getTotalNum",userInfoDo);
    }
}

  

复制代码
<mapper namespace="userInfoDO">    <resultMap id="BaseResultMap" type="UserInfoDO">        <result column="phone_num" property="phoneNum" jdbcType="VARCHAR"/>        <result column="user_name" property="userName" jdbcType="VARCHAR"/>        <result column="identity_num" property="identityNum" jdbcType="VARCHAR"/>    </resultMap>    <select id="getUserInfo" resultMap="BaseResultMap" parameterType="UserInfoDO">        select phone_num,user_name,identity_num from tb_user         <where>            <if test="phoneNum!=null and phoneNum!=''">                phone_num like '%${phoneNum}%'            </if>            <if test="userName != '' and userName != null" >                and user_name like '%${userName}%'            </if>            <if test="identityNum != '' and identityNum != null" >                and identity_num like '%${identityNum}%'            </if>         </where>         limit #{startNum},5 //每页五条数据    </select>        <select id="getTotalNum" resultType="int" parameterType="UserInfoDO">        select count(*) from tb_user         <where>            <if test="phoneNum!=null and phoneNum!=''">                phone_num like '%${phoneNum}%'            </if>            <if test="userName != '' and userName != null" >                and user_name like '%${userName}%'            </if>            <if test="identityNum != '' and identityNum != null" >                and identity_num like '%${identityNum}%'            </if>         </where>    </select>  </mapper>
复制代码

 

  




转载网址:http://www.cnblogs.com/li--xin/p/6216068.html

相关阅读:SSM框架——实现分页和搜索分页

网址:http://blog.csdn.net/zhshulin/article/details/26447713