一、呈现页面-Controller
@Controller
@RequestMapping("/role/")
public class SysRoleController {
// 列表页面呈现
@RequestMapping("doRoleListUI")
public String doRoleListUI(){
return "sys/role_list";
}
}
二、去添加主页上的列表点击事件
$(function(){
/* $("#load-log-id").click(function(){
var url="log/doLogListUI.do";
//load函数为一个特殊的ajax函数
$("#mainContentId").load(url);
}) */
/*$("#load-menu-id").click(function(){
var url="menu/doMenuListUI.do";
//load函数为一个特殊的ajax函数
$("#mainContentId").load(url);
})*/
doLoadUI("load-log-id","log/doLogListUI");
doLoadUI("load-menu-id","menu/doMenuListUI");
doLoadUI("load-dept-id","dept/doDeptListUI");
doLoadUI("load-role-id","role/doRoleListUI");
});
// 发现两个方法大量代码重复,可提取
function doLoadUI(id,url) {
$("#"+id).click(function(){
//load函数为一个特殊的ajax函数
$("#mainContentId").load(url,function () {
//初始化对象 如果有数据,移除
$("#mainContentId").removeData();
});
});
}
测试一下吧:
三、角色实体类
public class SysRole implements Serializable {
private static final long serialVersionUID = 2036333074712712067L;
private Integer id;
private String name;
private String note;
private Date createdTime;
private Date modifiedTime;
private String createdUser;
private String modifiedUser;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public Date getCreatedTime() {
return createdTime;
}
public void setCreatedTime(Date createdTime) {
this.createdTime = createdTime;
}
public Date getModifiedTime() {
return modifiedTime;
}
public void setModifiedTime(Date modifiedTime) {
this.modifiedTime = modifiedTime;
}
public String getCreatedUser() {
return createdUser;
}
public void setCreatedUser(String createdUser) {
this.createdUser = createdUser;
}
public String getModifiedUser() {
return modifiedUser;
}
public void setModifiedUser(String modifiedUser) {
this.modifiedUser = modifiedUser;
}
}
四、实现分页功能-Dao-要求:按姓名查看
public interface SysRoleDao {
// 分页查询:按名字查询,设定起始页面,页面容量
List<SysRole> findPageObjects(@Param("name") String name,
@Param("startIndex")Integer startIndex,
@Param("pageSize")Integer pageSize);
// 总记录数:按名字查询总记录数
int getRowCount(@Param("name")String name);
}
五、mapper.xml-拷贝log的
<!-- 分页查询当前页记录 -->
<select id="findPageObjects"
resultType="com.jt.sys.SysRole">
select *
from sys_roles
<include refid="queryWhereId"/>
order by createdTime desc
limit #{startIndex},#{pageSize}
</select>
<!-- 按条件统计总记录数 -->
<select id="getRowCount"
resultType="int">
select count(*)
from sys_roles
<include refid="queryWhereId"/>
</select>
六、service
public interface SysRoleService {
// 分页查询:按名字查询,当前页码值
// 返回当前页记录以及分页信息
PageObject<SysRole> findPageObjects(String name,
Integer pageCurrent);
}
@Service
public class SysRoleServiceImpl implements SysRoleService {
// 建立连接
@Autowired
private SysRoleDao sysRoleDao;
// 分页查询
@Override
public PageObject<SysRole> findPageObjects(String name, Integer pageCurrent) {
// 1.验证参数合法性
if(pageCurrent==null&&pageCurrent<1){
throw new IllegalArgumentException("当前页设定不合法");
}
// 2.查询总记录数 进行验证
int rowCount =sysRoleDao.getRowCount(name); //根据姓名查询总记录数
if(rowCount==0){
throw new ServiceException("没有找到对应记录");
}
// 3.当前页的记录信息
int pageSize=2;
int startIndex=(pageCurrent-1)*pageSize;
List<SysRole> records =
sysRoleDao.findPageObjects(name,startIndex,pageSize);
// 4.封装结果并返回
PageObject<SysRole> po = new PageObject<>();
po.setRecords(records);
po.setPageCurrent(pageCurrent);
po.setPageSize(pageSize);
po.setRowCount(rowCount);
// 总页数的计算 提取到PageObject对应的getPageCount-get方法中
// po.setPageCount((rowCount-1)/pageSize+1);
return po;
}
}
public int getPageCount() {
// return pageCount;
return (rowCount-1)/pageSize+1;
}
七、controller
// 分页查询
@RequestMapping("doFindPageObjects")
@ResponseBody
public JsonResult doFindPageObjects(String name,Integer pageCurrent){
return new JsonResult(sysRoleService.findPageObjects(name,pageCurrent));
}
八、前端页面
延伸:
添加序号列
i是一个字符串,需要转换为数字,再进行加1