1. 创建maven项目 springbootdemo
2. 配置pom.xml,导入jar包
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<mybatis_spring_boot.version>1.3.2</mybatis_spring_boot.version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis_spring_boot.version}</version>
<artifactId>mysql-connector-java</artifactId>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<source>${java.version}</source>
<target>${java.version}</target>
3. 配置application.properties 文件
logging.level.cn.mrdear.mapper=trace
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/my_db?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
mybatis.typeAliasesPackage=com.demo.model
mybatis.mapperLocations=classpath:com/demo/dao/mapper/*.xml
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
server.port=11111
spring.devtools.restart.exclude=static/**,public/**
4. 创建 Appplication 启动类
@MapperScan(basePackages = { "com.demo.dao" })
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
5. 创建实体类 User.java
6. 创建 dao 层
(1)创建UserDao 接口
public List<User> selectByPage();
(2)创建 UserMapper.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.demo.dao.UserDao">
<resultMap id="BaseResultMap" type="com.demo.model.User">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="VARCHAR" property="password" />
<select id="selectByPage"
resultType="com.demo.model.User">
<include refid="Base_Column_List" />
<sql id="Base_Column_List">
7. 创建 service层
(1)创建service 接口
public List<User> selectByPage(int pageNo, int pageSize);
(2)实现service接口
public class UserServiceImpl implements UserService {
public List<User> selectByPage(int pageNo, int pageSize) {
PageHelper.startPage(pageNo, pageSize);
return userDao.selectByPage();
8. 创建 Controller 层
public class UserController {
private static Logger logger = LoggerFactory.getLogger(UserController.class);
private UserService userService;
@RequestMapping("selectByPage")
public ModelMap selectByPage(Integer page, Integer rows) {
logger.info("/user/selectByPage开始调用!");
ModelMap model = new ModelMap();
List<User> userList = userService.selectByPage(page, rows);
PageInfo<User> pageInfo = new PageInfo<User>(userList);
model.put("data", userList);
model.put("total", pageInfo.getTotal());
model.put("page", pageInfo.getPageNum());
logger.info("/user/selectByPage出现异常!!!");
9. 在 resources 目录下 创建 static 目录,用于存放静态资源
10. 运行项目,测试