# springboot-datatables-demo
**Repository Path**: up99/springboot-datatables-demo
## Basic Information
- **Project Name**: springboot-datatables-demo
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 2
- **Forks**: 1
- **Created**: 2016-09-09
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
## 使用Spring Boot Jquery Datatables实现管理平台的表格
最近在公司做一个运营平台,增删改查的那种,需要一个多功能的表格,网上看到Jquery的DataTables功能很丰富,查询,排序,翻页等等功能完善,
但是[DataTables官网](http://www.datatables.net/)上的例子,表格数据都没有从服务端获取,生产上使用还得自己摸索下.另外,公司使用
Spring boot这个框架,配置简单,下面我们一起做一个整合的例子
### 新建Spring boot的应用
新建个项目springboot-datatables-demo,我使用的是intellij idea,创建个maven项目,在pom里面引用包后,创建main方法即可主要代码如下:
####pom.xml
```xml
4.0.0
com.larry
springboot-datatables-demo
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.2.5.RELEASE
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-test
mysql
mysql-connector-java
c3p0
c3p0
0.9.1.2
net.sourceforge.nekohtml
nekohtml
1.9.21
org.projectlombok
lombok
1.16.4
junit
junit
4.12
org.mockito
mockito-core
1.10.19
test
net.coobird
thumbnailator
0.4.8
com.alibaba
fastjson
1.2.8
org.springframework.boot
spring-boot-maven-plugin
spring-releases
Spring Releases
https://repo.spring.io/libs-release
spring-releases
Spring Releases
https://repo.spring.io/libs-release
```
#### SddApplication.java
```java
package com.larry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SddApplication {
public static void main(String[] args) {
SpringApplication.run(SddApplication.class, args);
}
}
```
ok,添加完上面两个主要的内容maven引入相关包以后,就可以运行啦,在SDDApplication上右击run一下.....额,是不是报错了,貌似把数据库给忘啦.
我们使用mysql数据库,需要添加数据库配置类config.java,添加配置文件application.properties
#### 建表
```bash
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `app`;
CREATE TABLE `app` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`create_time` datetime DEFAULT NULL,
`description` text,
`hot` int(8) DEFAULT '0',
`keywords` text,
`url` varchar(255) NOT NULL,
`disabled` int(2) NOT NULL DEFAULT '0',
`name` varchar(50) DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`is_delete` bit(1) DEFAULT NULL COMMENT '是否删除,0:删除',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
```
####application.properties添加数据库配置信息
```bash
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false
spring.datasource.c3p0.driver-class-name=com.mchange.v2.c3p0.ComboPooledDataSource
spring.datasource.c3p0.jdbc-url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=yes&characterEncoding=UTF-8
spring.datasource.c3p0.username=root
spring.datasource.c3p0.password=
spring.datasource.c3p0.min-evictable-idle-time-millis=30000
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
```
由于我使用的是thymeleaf模板引擎,需要在resource下面添加文件夹templates,再添加个html, 随便建个html叫index.html,在body里面写个 hello world! 好了。
然后,我们run一下试试
```bash
2016-04-03 15:29:27.850 INFO 1847 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-04-03 15:29:27.852 INFO 1847 --- [ main] com.larry.SddApplication : Started SddApplication in 4.522 seconds ...
```
当我们发现这两句话时,说明我们项目已经启动成功了.不过输入http://localhost:8080/浏览器仍然会报错,我们配置下路由让/路径默认跳转到index模板上.
###MvcConfig
```java
package com.larry.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("").setViewName("index");
}
}
```
重启后,浏览器中输入http://localhost:8080 哈哈,是不是看到熟悉的 hello world! 啦。
### datatables
啰嗦了半天终于进入主题了。。。。我在github上发现有个哥们已经封装了一套[spring data jpa + jquery datatables](https://github.com/darrachequesne/spring-data-jpa-datatables)的项目,直接pom里面引用下,就ok拉,下面看看具体怎么是用
#### Maven 依赖
```xml
com.github.darrachequesne
spring-data-jpa-datatables
2.0
```
注意这哥们使用的hibernate包是4.3.10.Final,这个和spring-boot用的hibernate要一致,否则启动的时候就会报错,所以我选择了1.2.5.RELEASE的版本的spring boot。
#### 启用DataTablesRepository工厂
```java
@SpringBootApplication
@EnableJpaRepositories(repositoryFactoryBeanClass = DataTablesRepositoryFactoryBean.class)
public class SddApplication {
public static void main(String[] args) {
SpringApplication.run(SddApplication.class, args);
}
}
```
#### 扩展DataTablesRepository接口
```java
public interface AppRepository extends DataTablesRepository {
}
```
#### 设置model属性
```java
public class App {
@Id
@GeneratedValue
private long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
@URL
private String url;
private String description;
private String keywords;
private boolean disabled;
private int hot;
@CreationTimestamp
private Date createTime;
@UpdateTimestamp
private Date updateTime;
private boolean isDelete;
}
```
#### 包含jquery.spring-friendly.js
It overrides jQuery data serialization to allow Spring MVC to correctly map input parameters (by changing column[0][data] to column[0].data in request payload)
#### index.html
index.html
```html
Title
|
操作 |
名称 |
地址 |
关键字 |
描述 |
热度 |
添加日期 |
更新时间 |
状态 |
```
#### AppController
```java
@ResponseBody
@RequestMapping(value = "all", method = RequestMethod.GET)
public DataTablesOutput messages(@Valid DataTablesInput input) {
return appRepository.findAll(input);
}
```
启动后,添加数据后,显示效果如下,已经可以分页搜索排序了。

到这里就可以看到,我们的基本目标已经完成了。不过仍然有个问题,现在的这个查询用的是`findAll(input)`,如果我们要添加过滤条件进行查询呢。其实[darrachequesne](https://github.com/darrachequesne)这个哥们已经封装了。'DataTablesOutput findAll(DataTablesInput var1, Specification var2);'调用这个就可以,下面我们来看看具体怎么用
### 使用过滤条件查询
过滤查询使用到了[Spring Data JPA - Specifications](https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/),自己要去熟悉了解下这个。
我们创建个Specification条件,然后调用'DataTablesOutput findAll(DataTablesInput var1, Specification var2);'
假设我们要查询所有删除状态为false的记录
#### AppSpec
```java
public class AppSpec {
public static Specification isNotDelete() throws Exception {
return (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.isFalse(root.get("isDelete"));
}
}
```
注意:使用了lambda表达式,其实就是创建了内部类
#### 修改AppController
```java
@RequestMapping(value = "all", method = RequestMethod.GET)
public DataTablesOutput messages(@Valid DataTablesInput input) {
try {
return appRepository.findAll(input, AppSpec.isNotDelete());
} catch (Exception e) {
return null;
}
}
```
重启,添加12条数据,设置6条记录删除状态为true,看看效果

表格下面显示:
Showing 1 to 6 of 6 entries (filtered from 12 total entries)
说明我们过滤成功了!
打完收工!