java实现layui分页_SSM项目layui分页实例

这篇博客介绍了如何在SSM(Spring、SpringMVC、MyBatis)项目中结合layui实现分页功能。作者提供了详细的步骤,从前端页面到后台控制器、Service、DAO的配置,以及数据库查询和JSON数据的转换,展示了完整的分页查询流程。
摘要由CSDN通过智能技术生成

最近学了layui,发现其中的分页挺有意思的,所以整理了一下,一遍自己随时查看。(官方文档上已经很详细了,当中有不足的地方欢迎大家指出)

关于前台的js文件,css样式,js样式,大家可以到官网下

本人使用的是oracle数据库

我先贴出前台界面

20180110133716551382.png

spring和spring-mvc,mybatis大家需要的jar包,有需要的可以联系我。

20180110133716593376.png

这里使用到了json传数据,所以需要json的jar包

最关键的代码就要来了

1、前台jsp界面

1

2 pageEncoding="UTF-8"%>

3

4

5

6

7

Insert title here

8

9 charset="utf-8" rel="stylesheet" />

10

12

14

15 layui.use([ ‘table‘], function() {16 var table =layui.table;17 });18

19

20

21

22 lay-data="{height:‘full‘, url:‘findallEmp.do‘,page:true,limit:5,limits:[3,5,10,15]}"

23 lay-filter="test1">

24

25

26

编号

27

雇员

28

工作

29

入职日期

30

工资

31

32

33

34

35

2、界面跳转到.do

1 packagecom.llh.controller;2

3 importjava.util.List;4

5 importjavax.annotation.Resource;6

7 importorg.springframework.context.annotation.Scope;8 importorg.springframework.stereotype.Controller;9 importorg.springframework.web.bind.annotation.RequestMapping;10 importorg.springframework.web.bind.annotation.ResponseBody;11

12 importcom.llh.entity.EmpInfo;13 importcom.llh.service.EmpService;14

15 importnet.sf.json.JSONArray;16

17

18 @Controller19 @Scope("prototype")20 public classEmpController {21

22 @Resource23 privateEmpService empService;24

25 @RequestMapping(value="findallEmp",produces="text/html;charset=utf-8")26 public @ResponseBody String findallEmp( int page, intlimit){27 int before = limit * (page - 1) + 1;28 int after = page *limit;29 List eilist =empService.findAllPage(before, after);30 int count =empService.count();31 JSONArray json =JSONArray.fromObject(eilist);32 String js =json.toString();33 String jso = "{\"code\":0,\"msg\":\"\",\"count\":"+count+",\"data\":"+js+"}";//转为layui需要的json格式

34 returnjso;35 }36 }

3、自动装配的service类

1 packagecom.llh.service;2

3 importjava.util.List;4

5 importjavax.annotation.Resource;6

7 importorg.apache.ibatis.annotations.Param;8 importorg.springframework.stereotype.Service;9 importorg.springframework.transaction.annotation.Transactional;10

11 importcom.llh.dao.EmpDao;12 importcom.llh.entity.EmpInfo;13 @Service14 @Transactional15 public class EmpService implementsEmpDao{16

17 @Resource18 privateEmpDao empDao;19

20 /**

21 * 查询数据22 */

23 public List findAllPage(@Param("before") int before,@Param("after") intafter){24 returnempDao.findAllPage(before, after);25 }26 /**

27 * 查询条数28 */

29 public intcount(){30 returnempDao.count();31 }32

33 }

4、实现dao接口

1 packagecom.llh.dao;2

3 importjava.util.List;4

5 importorg.apache.ibatis.annotations.Param;6

7 importcom.llh.entity.EmpInfo;8

9 public interfaceEmpDao {10

11 public List findAllPage(@Param("before") int before,@Param("after") intafter);12

13 public intcount();14 }

5、mapper.xml监控dao

1 <?xml version="1.0" encoding="UTF-8"?>

2 /p>

4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

5

6

7 select * from (select row_number() over(order by empno) idx,e.*from emp e) s where idx between #{before} and #{after}8

9

10 select count(*) from emp11

12

6、至于spring-mybatis.xml也贴出来一下,供有需要的人来看

1 <?xml version="1.0" encoding="UTF-8"?>

2

4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

5 xmlns:context="http://www.springframework.org/schema/context"

6 xmlns:tx="http://www.springframework.org/schema/tx"

7 xsi:schemaLocation="http://www.springframework.org/schema/beans

8 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd

9 http://www.springframework.org/schema/context

10 http://www.springframework.org/schema/context/spring-context-4.3.xsd

11 http://www.springframework.org/schema/tx

12 http://www.springframework.org/schema/tx/spring-tx-4.3.xsd

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

38

39

40

41

42

43

44

45

46

47

7、springmvc.xml的配置

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

4 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"

5 xsi:schemaLocation="http://www.springframework.org/schema/beans

6 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd

7 http://www.springframework.org/schema/context

8 http://www.springframework.org/schema/context/spring-context-4.3.xsd

9 http://www.springframework.org/schema/mvc

10 http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd

11 http://www.springframework.org/schema/tx

12 http://www.springframework.org/schema/tx/spring-tx-4.3.xsd

13 ">

14

15

16

17

18

19

20

21 class="org.springframework.web.servlet.view.InternalResourceViewResolver">

22

23

24

25

26

27 class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

28

29

30

31

32

33

34

35

8、web.xml实现字符编码,过滤器,spring-mybatis的监听,springmvc的监听

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 PageTestDemo

4

5 index.jsp

6

7

8

9 contextConfigLocation

10 classpath:spring-mybatis.xml

11

12

13

14

15 org.springframework.web.context.ContextLoaderListener

16

17

18

19

20 dispatcherServlet

21 org.springframework.web.servlet.DispatcherServlet

22

23 contextConfigLocation

24 classpath:springmvc.xml

25

26

27

28 dispatcherServlet

29 *.do

30

31

32

33

34 characterEncoding

35 org.springframework.web.filter.CharacterEncodingFilter

36

37 encoding

38 UTF-8

39

40

41

42 characterEncoding

43 /*44 45

46

47

------------------------------至此,leyui最简单的分页查询数据就可以了。

过后将会更新layui的上传,动态下载。

以及bootstrap的数据表格插件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值