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

时间:2021-09-12 03:17:00
/**
* 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;
}
}

  

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;
}
}

  

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();
}
}

  

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

  

@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;
}
}

  

public interface UserInfoDao {

	public List<UserInfoDO> getUserInfo(UserInfoDO userInfoDo) throws SQLException;

	public int getTotalNum(UserInfoDO userInfoDo) throws SQLException;

}

  

@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>