SSM项目layui分页实例

时间:2023-03-09 21:31:24
SSM项目layui分页实例

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

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

本人使用的是oracle数据库

我先贴出前台界面

SSM项目layui分页实例

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

SSM项目layui分页实例

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

最关键的代码就要来了

1、前台jsp界面

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="${pageContext.request.contextPath }/js/layui/css/layui.css"
charset="utf-8" rel="stylesheet" />
<script src="${pageContext.request.contextPath }/js/layui/layui.js"
charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"
charset="utf-8"></script>
<script type="text/javascript">
layui.use([ 'table' ], function() {
var table = layui.table;
});
</script>
</head>
<body class="layui-layout-body">
<table class="layui-table"
lay-data="{height:'full', url:'findallEmp.do',page:true,limit:5,limits:[3,5,10,15]}"
lay-filter="test1">
<thead>
<tr>
<th lay-data="{field:'empno',edit:'text',sort:true,width:'50px'}">编号</th>
<th lay-data="{field:'ename',edit:'text',width:'50px'}">雇员</th>
<th lay-data="{field:'job',edit:'text',width:'50px'}">工作</th>
<th lay-data="{field:'hiredate',edit:'text',width:'100px'}">入职日期</th>
<th lay-data="{field:'sal',edit:'text',width:'50px'}">工资</th>
</tr>
</thead>
</table>
</body>
</html>

2、界面跳转到.do

 package com.llh.controller;

 import java.util.List;

 import javax.annotation.Resource;

 import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.llh.entity.EmpInfo;
import com.llh.service.EmpService; import net.sf.json.JSONArray; @Controller
@Scope("prototype")
public class EmpController { @Resource
private EmpService empService; @RequestMapping(value="findallEmp",produces="text/html;charset=utf-8")
public @ResponseBody String findallEmp( int page, int limit){
int before = limit * (page - 1) + 1;
int after = page * limit;
List<EmpInfo> eilist = empService.findAllPage(before, after);
int count = empService.count();
JSONArray json = JSONArray.fromObject(eilist);
String js = json.toString();
String jso = "{\"code\":0,\"msg\":\"\",\"count\":"+count+",\"data\":"+js+"}";//转为layui需要的json格式
return jso;
}
}

3、自动装配的service类

 package com.llh.service;

 import java.util.List;

 import javax.annotation.Resource;

 import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.llh.dao.EmpDao;
import com.llh.entity.EmpInfo;
@Service
@Transactional
public class EmpService implements EmpDao{ @Resource
private EmpDao empDao; /**
* 查询数据
*/
public List<EmpInfo> findAllPage(@Param("before") int before,@Param("after") int after){
return empDao.findAllPage(before, after);
}
/**
* 查询条数
*/
public int count(){
return empDao.count();
} }

4、实现dao接口

 package com.llh.dao;

 import java.util.List;

 import org.apache.ibatis.annotations.Param;

 import com.llh.entity.EmpInfo;

 public interface EmpDao {

     public List<EmpInfo> findAllPage(@Param("before") int before,@Param("after") int after);

     public int count();
}

5、mapper.xml监控dao

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.llh.dao.EmpDao">
<select id="findAllPage" resultType="EmpInfo">
select * from (select row_number() over(order by empno) idx,e.* from emp e) s where idx between #{before} and #{after}
</select>
<select id="count" resultType="int">
select count(*) from emp
</select>
</mapper>

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

 <?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
"
>
<!-- 自动扫描包 -->
<context:component-scan base-package="com.llh"></context:component-scan> <!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</bean> <!-- mybatis配置 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.llh.entity"></property>
<property name="mapperLocations" value="classpath:com/llh/dao/*.xml"></property>
</bean> <!-- 映射配置器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.llh.dao"></property>
</bean> <!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 开启事务注解 -->
<tx:annotation-driven/> </beans>

7、springmvc.xml的配置

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
">
<context:component-scan base-package="com.llh"></context:component-scan>
<mvc:annotation-driven />
<mvc:default-servlet-handler /> <bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 上传下载 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
<property name="maxUploadSize" value="-1" />
</bean> </beans>

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

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>PageTestDemo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 加载spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param> <!-- 配置spring的监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 配置springmvc -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!-- 过滤器 -->
<filter>
<filter-name>characterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>

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

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

以及bootstrap的数据表格插件