我们做一些web项目中,在一些查询页面上,总会有搜索框,今天就在此记录一下自己的做法。
在页面上的搜索框是这样的
在我的xml中,因为我是用的存储过程分页,这是我项目里面的,重点就是后面的if标签,因为是测试,所以我就写了三个条件。
Mybatis中的xml:
<!-- 调用分页查询的存储过程 -->
<select id="selectCaptialinfotest" resultMap="BaseResultMap"
parameterType="YW.Eos.Frame.pojo.CAPITAL_INFO">
{
call [mfs_分页] (@tab = N'capital_info ci
inner join capital_info_name
cie on ci.capital_id=cie.capital_id
LEFT outer join capital_info_state
cis on cis.capital_id=ci.capital_id
left outer join bill_flow_state bfs
on bfs.bill_id=cis.capital_flow_id
left join s_headship hd on
(hd.headship_id=bfs.next_headship_id AND
bfs.NEXT_HEADSHIP_ID_TYPE=2)
left join s_user_info ui3 on (ui3.user_id=bfs.next_headship_id AND
bfs.NEXT_HEADSHIP_ID_TYPE=1)',
@strFld = N'ci.[CAPITAL_ID]
,ci.[CAPITAL_NO]
,ci.[CAPITAL_NAME]
,ci.[CAPITAL_TYPE]
,ci.[CAPITAL_TYPE_DESC]
,ci.[CAPITAL_CONFIG]
,ci.[USE_DATE]
,ci.APPLY_DATE
,ci.USE_END_DATA
,ci.[CAPITAL_USAGE]
,ci.[STORE_PLACE]
,ci.[AMOUNT]
,ci.[OWNER]
,ci.[STATUS]
,ci.[CAPITAL_FLAG]
,ci.[SUB_NUMBER]
,ci.[CAPITAL_PARENT_ID]
,ci.[BILL_ID],
cis.capital_flow_id,
cis.status
flowstatus,
cie.OWNER_NAME,
cie.KEEPER_NAME,
cie.HS_DEPT_NAME,
cie.CAPITAL_TYPE_NAME,
cie.STORE_PLACE_NAME,
cie.OPT_NAME,
cie.HS_COMPANY_NAME,
cie.USE_COMPANY_NAME',
@strWhere = N' ci.status not
like ''fei%'' and
ci.hs_system_id=''HS001''
and (cis.STATUS not in
(''rej'',''cancel'') or
isnull(cis.STATUS,'''')='''' or
cis.CREATE_USER=''admin1'')
<if test="capitalName != null and capitalName != ''">
and ci.CAPITAL_NAME =''${capitalName}''
</if>
<if test="capitalType != null and capitalType != ''">
and ci.CAPITAL_TYPE = ''${capitalType}''
</if>
<if test="ownername != null and ownername != ''">
and cie.OWNER_NAME = ''${ownername}''
</if>
'
,
@PageIndex =#{pageIndex,mode=IN,jdbcType=INTEGER},
@PageSize=#{pageSize,mode=IN,jdbcType=INTEGER},
@Sort =
N'ci.last_modify_time',
@IsGetCount = 0
)
}
</select>
然后再Mapper的接口里面,要接收这些参数
List<CAPITAL_INFO> selectCaptialinfotest(@Param("pageSize") int pageSize,
@Param("pageIndex") int pageIndex,
@Param("capitalName") String capitalName,
@Param("capitalType") String capitalType,
@Param("ownername") String ownername);
你的xml里面有几个#{},你就得写几个@Param传值
然后就在service实现,这个就不写了,很简单,就是return一个mapper里面的接口。
然后在Controller里面接收前台的值,和返回JSON给前台页面
Controller代码:
@RequestMapping("selectCaptialinfotest")
@ResponseBody
public Page<CAPITAL_INFO> selectCaptialinfotest(HttpServletRequest request,int pageSize,int pageIndex,@RequestParam(value="capitalName",defaultValue="") String capitalName,
@RequestParam(value="capitalType",defaultValue="") String capitalType,
@RequestParam(value="keepername",defaultValue="") String keepername) {
//获取总条数,即totalRecord
int count = capitalInfomapper.getEmployeeTotal();
System.out.println("得到的capitalname:"+capitalName);
List<CAPITAL_INFO> mlis=IcapitalInfoService.selectCaptialinfotest(pageSize, pageIndex,capitalName,capitalType,keepername);
String token = request.getParameter("access_token");
BASIC_USER_INFO basicuserinfo = JWTutils.unsign(token, BASIC_USER_INFO.class);
logger.info("用户"+basicuserinfo.getUserId()+"查询资产信息:");
//返回所有的分页参数和数据
return new Page<CAPITAL_INFO>(200,"success",pageIndex,pageSize,count,mlis);
}
获取总行数,是因为我的前台需要这个值,然后我的Page类是一个泛型,里面定义的是Json数据的类型。
@RequestMapping和@ResponseBody就不用多说了吧,@RequestParam括号里面的defaultValue是设置默认值,我写个空的话就是搜索框不输入值就是搜索所有的数据,因为在我的xml里面if标签有判断。
下面附上我的Page类,做分页给前台JSON很好用也很常用的一个类。
Page:
package YW.Eos.Frame.pojo;
import java.util.List;
public class Page<T> {
private int pagNum;// 当前的页数
private int pageSize;// 每页显示数量,limit函数第二个参数
private int totalRecord;// 总记录数
private int count;// 总页数
private int startIndex;// 开始位置,limit函数第一个参数
private int start;
private int end;
private int code;
private String message;
private List<T> data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Page(int code,String msg,int pageIndex,int pageSize,int count,List<T> data) {
this.pagNum=pageIndex;
this.code=code;
this.message=msg;
this.pageSize=pageSize;
this.count=count;
this.data=data;
}
public Page(int pagNum, int pageSize, int totalRecord) {
this.pagNum = pagNum;
this.pageSize = pageSize;
this.totalRecord = totalRecord;
// 计算总页数
if (totalRecord % pageSize == 0) {
this.count = totalRecord / pageSize;
} else {
this.count = (totalRecord / pageSize) + 1;
}
// 确定limit函数的第一个参数的值
this.startIndex = pagNum * pageSize;
this.start = 1;
this.end = 5;
if (count <= 5) {
this.end = this.count;
} else {// pagNum=6;start=4;end=8
this.start = pagNum - 2;
this.end = pagNum + 2;
}
if (start < 0) {
this.start = 1;
this.end = 5;
}
// 举个例子:一共有10页,现在就在第10页,那么根据上面的逻辑end=12,错误;则end=this.totalPage;且下面导航栏还是要出现5个,则start=end-5
if (end > this.count) {
this.end = count;
this.start = end - 5;
}
}
public int getPagNum() {
return pagNum;
}
public void setPagNum(int pagNum) {
this.pagNum = pagNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
public List<T> getData() {
return data;
}
public void setData(List<T> data) {
this.data = data;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
}
sql server和Mysql都可以用,因为mysql是limit函数,但是sql server需要调用储存过程。
好的 记录到此 ,有问题可留言。