本文根據一個簡單的user表爲例,展現 springboot集成mybatis,再到前端分頁完整代碼;javascript
先看java部分css
pom.xml 加入html
<!--支持 Web 應用開發,包含 Tomcat 和 spring-mvc。 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--模板引擎--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--Mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <!-- mybatis pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>3.6.3</version> </dependency> <!--Mysql / DataSource--> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--springboot 集成Mybatis所需jar配置 end-->
application.properties文件前端
spring.datasource.platform=mysql spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driverClassName=com.mysql.jdbc.Driver # Advanced configuration... spring.datasource.max-active=50 spring.datasource.max-idle=6 spring.datasource.min-idle=2 spring.datasource.initial-size=6 #create table spring.jpa.hibernate.ddl-auto=validate spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.cache=false
啓動類 Application.javajava
@SpringBootApplication @MapperScan("com.boot.mapper") public class Application { //定義一個全局的記錄器,經過LoggerFactory獲取 private final static Logger logger = LoggerFactory.getLogger(Application.class); //----------------------------mybaits配置start------------------------------------------- //DataSource @Bean @ConfigurationProperties(prefix="spring.datasource") public DataSource dataSource() { return new org.apache.tomcat.jdbc.pool.DataSource(); } //SqlSessionFactory @Bean public SqlSessionFactory sqlSessionFactoryBean() throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); return sqlSessionFactoryBean.getObject(); } @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); } //------------------------------mybaits配置end--------------------------------- public static void main(String[] args){ System.out.println("Hello World!"); SpringApplication.run(Application.class, args); } }
以一個簡單的user對象爲例mysql
private Integer id; private String name; private String password;
controller層git
@RestController public class UserController { @Autowired UserService uSer; @RequestMapping("userlist") public Object userlist(@RequestParam(value="pageon",defaultValue="1")int pageon ,ModelAndView mv){ mv.addAllObjects(uSer.UserList(pageon));
mv.setViewName("userlist"); return mv; } }
service層github
public Map<String, Object> UserList(int pageon) { // TODO Auto-generated method stub Map<String,Object> map=new HashMap<String, Object>(); Page page=new Page(pageon); page.setRowcountAndCompute(userMapper.selectPageListCount(null)); map.put("page", page); map.put("list", userMapper.selectPageList(map)); return map; }
UserMapper.xmlweb
<select id="selectPageList" parameterType="java.util.Map" resultMap="BaseResultMap" > select id, name, password from user order by id limit #{page.start},#{page.row} </select> <select id="selectPageListCount" parameterType="java.util.Map" resultType="int" > select count(1) from user </select>
到此時後臺代碼結束spring
附加個page工具類
package com.boot.utils; import java.io.Serializable; public class Page implements Serializable { /** * */ private static final long serialVersionUID = 1L; public int getPageon() { return pageon; } public void setPageon(int pageon) { this.pageon = pageon; } public int getRowcount() { return rowcount; } public void setRowcount(int rowcount) { this.rowcount = rowcount; } public int getPagecount() { return pagecount; } public void setPagecount(int pagecount) { this.pagecount = pagecount; } public int getRow() { return row; } public void setRow(int row) { this.row = row; } public Page(int pageon, int row, int rowcount) { pageNumber = 11; this.pageon = pageon; this.row = row; this.rowcount = rowcount; compute(); } public Page(int pageon, int row) { pageNumber = 11; this.pageon = pageon; this.row = row; } public Page(int pageon) { pageNumber = 11; this.pageon = pageon; } public Page() { pageNumber = 11; } public int getPageNumber() { return pageNumber; } public void setPageNumber(int pageNumber) { this.pageNumber = pageNumber; } public void compute() { if (rowcount <= 0) return; if (row <= 0) row = 10; pagecount = rowcount % row != 0 ? rowcount / row + 1 : rowcount / row; if (pageon > pagecount) pageon = pagecount; if (pageon < 1) pageon = 1; start = (pageon - 1) * row; end = pageon * row; if (end > rowcount) end = rowcount; } 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; } public void setRowcountAndCompute(int rowcount) { this.rowcount = rowcount; compute(); } protected int pageon; protected int rowcount; protected int pagecount; protected int row; protected int start; protected int end; protected int pageNumber; }
前端代碼開始
statis目錄下加入以下幾個文件
在templates目錄下創建一個前端分頁工具頁面 page.html,(雖然代碼很少,封裝這點東西花費我半天時間!!)
<!DOCTYPE html > <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <div th:fragment="page"> <div id="page"></div> <link rel="stylesheet" th:href="@{/css/style.css}" /> <script type="text/javascript" th:src="@{/js/page.js}"></script> <script th:inline="javascript">/*<![CDATA[*/ var size=[[${page.pagecount}]]; if(size>1) Pagination.Init(document.getElementById('page'), { size: size, // pages size page: [[${page.pageon}]], // selected page step: 3 // pages before and after current }); /*]]>*/ function back(page_index){ /*<![CDATA[*/ if([[${page.pageon}]]==page_index) return; var url=document.getElementById('page').parentNode.getAttribute('url');var tourl; if(url.indexOf('?')>0) tourl=url+'&pageon='+page_index; else tourl=url+'?pageon='+page_index; window.location.href=tourl; /*]]>*/ } </script> </div> </html>
建立userlist.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>用戶列表</title> </head> <body> <div style="width: 1000px;" > <table style="border-width: 1px;border-style: dashed;"> <tr> <td>ID</td> <td>姓名</td> <td>密碼</td> </tr> <tr th:each="user:${list}"> <td th:text="${user.id}">ID</td> <td th:text="${user.name}">姓名</td> <td th:text="${user.password}">密碼</td> </tr> </table> <div th:include="page :: page" url="/userlist"></div> </div> </body> </html>
能夠看到 引用分頁的代碼 只有一句,是否是很好用
<div th:include="page :: page" url="/userlist"></div>
分頁效果圖
謝謝觀看
下載插件鏈接 (有兩個插件 一個是滾動顯示,一個相似 博客園分頁)
http://files.cnblogs.com/files/changhai/%E5%88%86%E9%A1%B5%E6%8F%92%E4%BB%B6.zip