spring boot 1.4.2.RELEASE+Thymeleaf+mybatis 集成通用maper,与分页插件:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<!-- 依赖版本 -->
<mybatis.version>3.4.0</mybatis.version>
<mybatis.spring.version>1.3.0</mybatis.spring.version>
<mapper.version>3.3.9</mapper.version>
<pagehelper.version>4.1.6</pagehelper.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>
<!--Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<!-- Mybatis Generator -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<!--分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<!--通用Mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>${mapper.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
UserPojo
@Table(name="user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private Date birthday;
private String email;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
UserService
@Service
@Transactional
public class UserService {
@Autowired
private UserMapper userMapper;
@Transactional(readOnly = true)
public PageInfo<User> findAll(String username, Date birthday, Integer page, Integer pageSize) {
PageHelper.startPage(page, pageSize);
Example example = new Example(User.class);
if (!StringUtils.isEmpty(username)) {
example.createCriteria().andEqualTo("username", username);
}
if (birthday != null) {
example.createCriteria().andEqualTo("birthday", birthday);
}
List<User> users = userMapper.selectByExample(example);
return new PageInfo<>(users);
}
}
UserController
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/user")
public String index(Model model, @RequestParam(required = false) String username,@RequestParam(required = false) Date birthday, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize) {
PageInfo<User> pageInfo = userService.findAll(username, birthday, pageNum, pageSize);
//获得当前页
model.addAttribute("pageNum", pageInfo.getPageNum());
//获得一页显示的条数
model.addAttribute("pageSize", pageInfo.getPageSize());
//是否是第一页
model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
//获得总页数
model.addAttribute("totalPages", pageInfo.getPages());
//是否是最后一页
model.addAttribute("isLastPage", pageInfo.isIsLastPage());
model.addAttribute("users", pageInfo.getList());
return "index";
}
}
前台模板页
<table class="table table-hover" style="border-collapse: 0"
width="50%">
<thead>
<tr>
<th>id</th>
<th>username</th>
<th>birthday</th>
<th>email</th>
</tr>
</thead>
<tr th:each="user : ${users}">
<th th:text="${user.id}"></th>
<th th:text="${user.username}"></th>
<th th:text="${#dates.format(user.birthday, 'yyyy-MM-dd')}"></th>
<th th:text="${user.email}"></th>
</tr>
</table>
<nav>
<ul class="pagination">
<li><a href="">«</a></li>
<li>
<a th:if="${not isFirstPage}" th:href="@{${'/user'}(pageNum=${pageNum-1},pageSize=${pageSize})}">Previous</a>
<a th:if="${isFirstPagee}" href="javascript:void(0);">Previous</a>
</li>
<li th:each="pageNo : ${#numbers.sequence(1, totalPages)}">
<a th:if="${pageNum eq pageNo}" href="javascript:void(0);">
<span th:text="${pageNo}"></span>
</a>
<a th:if="${not (pageNum eq pageNo)}" th:href="@{${'/user'}(pageNum=${pageNo},size=${pageSize})}">
<span th:text="${pageNo}"></span>
</a>
</li>
<li>
<a th:if="${not isLastPage}" th:href="@{${'/user'}(pageNum=${pageNum+1},size=${pageSize})}">Next</a>
<a th:if="${isLastPage}" href="javascript:void(0);">Next</a>
</li>
<li><a href="#">»</a></li>
</ul>
</nav>
转自:http://blog.csdn.net/gdhuyufei/article/details/53712805
spring boot 1.4.2.RELEASE+Thymeleaf+mybatis 集成通用maper,与分页插件:的更多相关文章
- Spring Boot学习笔记(二二) - 与Mybatis集成
Mybatis集成 Spring Boot中的JPA部分默认是使用的hibernate,而如果想使用Mybatis的话就需要自己做一些配置.使用方式有两种,第一种是Mybatis官方提供的 mybat ...
- spring boot +Thymeleaf+mybatis 集成通用PageHelper,做分页
controller: /** * 分页查询用户 * @param request * @param response * @return * @throws Exception */ @ ...
- spring cloud: 升级到spring boot 2.x/Finchley.RELEASE遇到的坑
spring boot2.x已经出来好一阵了,而且spring cloud 的最新Release版本Finchley.RELEASE,默认集成的就是spring boot 2.x,这几天将一个旧项目尝 ...
- Java Spring Boot VS .NetCore (五)MyBatis vs EFCore
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Spring Boot 2.2.2.RELEASE 版本中文参考文档【3.2 - 3.10】
Spring Boot 2.2.2.RELEASE版本中文文档持续更新中~如有需要获取参考文档文件,关注公众号JavaSo,回复“参考文档”即可. 3.2 结构化代码 Spring Boot不需要任何 ...
- 001-Spring Cloud Edgware.SR3 升级最新 Finchley.SR1,spring boot 1.5.9.RELEASE 升级2.0.4.RELEASE注意问题点
一.前提 升级前 => 升级后 Spring Boot 1.5.x => Spring Boot 2.0.4.RELEASE Spring Cloud Edgware SR3 => ...
- 解决Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of the configured nodes are available
Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of ...
- Spring Boot 2.2.2.RELEASE 版本中文参考文档【3.1】
使用Spring Boot 本节将详细介绍如何使用Spring Boot.它涵盖了诸如构建系统,自动配置以及如何运行应用程序之类的主题.我们还将介绍一些Spring Boot最佳实践.尽管Spring ...
- Spring Boot 2.2.2.RELEASE 版本中文参考文档
写在前面 在我初次接触MongoDB的时候,是为了做一个监控系统和日志分析系统.当时在用Java操作MongoDB数据里的数据的时候,都是在网上查找Demo示例然后完成的功能,相信大家也同样的体会,网 ...
随机推荐
- ASP.NET——配置文件——连接字符串
一.连接字符串 1.通过<connectionStrings>方式: 方式一:SqlServer身份验证 <connectionStrings> <add name=&q ...
- leetcode328
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNo ...
- Qt creator 使用qwt
.pro中添加 LIBS += -L”C:\Qt\Qt5.3.2\5.3\msvc2013_opengl\lib” -lqwt INCLUDEPATH += "C:\Qt\Qt5.3.2\5 ...
- C获取当前时间
#include <stdio.h> #include <time.h> #include <string> #include <windows.h> ...
- 第一天:tomcat相关知识和浏览器的访问机制
1.tomcat的目录结构 1)bin目录:启动和关闭tomcat以及其他的脚本命令 2)conf目录:存放各种配置文件 a.server.xml配置文件的配置: *<host/>标签: ...
- JAVA基础知识总结6(面向对象特征之一:多态)
多 态:函数本身就具备多态性,某一种事物有不同的具体的体现. 体现:父类引用或者接口的引用指向了自己的子类对象. Animal a = new Cat(); 多态的好处:提高了程序的扩展性. 多态的弊 ...
- activity的四种加载模式介绍
四种加载模式的介绍: a) Standard : 系统默认模式,一次跳转即会生成一个新的实例: b) SingleTop : 和 standard 类似,唯一的区别就是当跳转的对象是位于栈顶 ...
- java内存模型和线程安全
- Entity Framework Tutorial Basics(15):Querying with EDM
Querying with EDM: We have created EDM, DbContext, and entity classes in the previous sections. Here ...
- 发现C#winform编程中不常用的控件(一)<FlowLayoutPanel控件><拆分器控件Splitcontainer >
第一部分:FlowLayoutPanel控件 实现效果: 将FlowLayoutPanel做为导航菜单按钮的容器 以实现 某个菜单按钮不显示时 整体的导航菜单布局不至于"缺憾" 原 ...