1.导入依赖,这里我使用的JDK是1.7,SpringBoot版本1.5.9
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.docin.lzh</groupId>
<artifactId>db_boot</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--默认为jar方式-->
<packaging>jar</packaging>
<properties>
<java.version>1.7</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!--web启动器-->
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.23</version>
</dependency>
</dependencies>
<build>
<finalName>db_boot</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!--配置源代码使用的开发版本-->
<source>1.7</source>
<!--配置需要生成的目标class文件的编译版本-->
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.配置application.yml
server:
port: 8085
spring:
datasource:
username: xxx
password: xxx
url: jdbc:mysql://xxx:3306/vonibo_132?serverTimezone=GMT%2B8
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
3.配置log4j.properties
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%d{yyyy-MM-dd HH\:mm\:ss}] - %m%n
#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File =D:/db_test/db_boot/SpringBoot.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
4.设置网站图标
只需要在resource目录下放自己的图标即可,图标文件名favicon.ico
5.配置Mapper.xml
<?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.docin.lzh.mapper.HqwxLessonMapper">
<select id="getList" resultType="com.docin.lzh.pojo.HqwxLesson">
SELECT * FROM hqwx_lesson WHERE 1 = 1 ORDER BY created_date DESC LIMIT #{startIndex},#{endIndex}
</select>
<select id="getCount" resultType="int">
SELECT count(*) FROM hqwx_lesson WHERE 1 = 1
</select>
</mapper>
6.Mapper接口
import com.docin.lzh.pojo.HqwxLesson;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface HqwxLessonMapper {
public List<HqwxLesson> getList(@Param("startIndex") int startIndex, @Param("endIndex") int endIndex);
public int getCount();
}
7.service接口及实现类
public interface HqwxLessonService {
public List<HqwxLesson> getList(int startIndex, int endIndex);
public int getCount();
}
@Service
public class HqwxLessonServiceImpl implements HqwxLessonService {
@Autowired
private HqwxLessonMapper hqwxLessonMapper;
@Override
public List<HqwxLesson> getList(int startIndex, int endIndex) {
return hqwxLessonMapper.getList(startIndex, endIndex);
}
@Override
public int getCount() {
return hqwxLessonMapper.getCount();
}
}
8.controller
import com.docin.lzh.pojo.HqwxLesson;
import com.docin.lzh.service.HqwxLessonService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Controller
public class HqwxLessonController {
private static Logger logger = Logger.getLogger(HqwxLessonController.class);
@Autowired
private HqwxLessonService hqwxLessonService;
@RequestMapping("/hqwx/index")
public String index(HttpServletRequest request, HttpServletResponse response) {
int pageNum = 1;
if (request.getParameter("pageNum") != null) pageNum = Integer.parseInt(request.getParameter("pageNum"));
int pageSize = 10;
List<HqwxLesson> hqwxLessonList = hqwxLessonService.getList((pageNum - 1) * pageSize, pageSize);
int count = hqwxLessonService.getCount();
logger.warn("hqwxLessonList size : " + hqwxLessonList.size() + " ,pageNum:" + pageNum + " ,pageSize: " + pageSize+",totalPage:"+(count % 10 == 0 ? count / 10 : (count / 10) + 1));
request.setAttribute("hqwxLessonList", hqwxLessonList);
request.setAttribute("pageNum", pageNum);
request.setAttribute("pageSize", pageSize);
request.setAttribute("totalPage",count);
return "hqwx/index";
}
}
9.启动类
@SpringBootApplication
public class DbBootSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(DbBootSpringBootApplication.class,args);
}
}
10.tempaltes目录下index.html
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<!--<html lang="en" xmlns:th="http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"/>
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
<title>hqwx</title>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous"/>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<table class="table table-bordered">
<thead>
<tr>
<td>id</td>
<td>class_code</td>
<td>class_name</td>
<td>teacher_id</td>
<td>teacher_name</td>
<td>up_type_code</td>
<td>lesson_type_code</td>
<td>lesson_code</td>
</tr>
</thead>
<tbody>
<tr th:each="hqwxLesson:${#httpServletRequest.getAttribute('hqwxLessonList')}">
<td th:text="${hqwxLesson.id}"></td>
<td th:text="${hqwxLesson.class_code}"></td>
<td th:text="${hqwxLesson.class_name}"></td>
<td th:text="${hqwxLesson.teacher_id}"></td>
<td th:text="${hqwxLesson.teacher_name}"></td>
<td th:text="${hqwxLesson.up_type_code}"></td>
<td th:text="${hqwxLesson.lesson_type_code}"></td>
<td th:text="${hqwxLesson.lesson_code}"></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="myPagination"></div>
</div>
<div class="col-md-2"></div>
</div>
</div>
</body>
<!-- 分页插件 -->
<script type="text/javascript" src='/js/jquery-1.10.2.js'></script>
<script type="text/javascript" src="/js/Pagination.js"></script>
<script th:inline="javascript">
/*<![CDATA[*/
var pageNum = [[${#httpServletRequest.getAttribute('pageNum')}]];
var totalPage = [[${#httpServletRequest.getAttribute('totalPage')}]];
/*]]>*/
</script>
<script type="text/javascript">
$(".myPagination").Pagination({
page: pageNum,
count:totalPage,
groups: 10,
onPageChange:function (page) {
window.location.href = "/hqwx/index?pageNum="+page;
}
});
</script>
</html>
11.启动测试