springboot支持mybatis【分页】 03

1、环境约束

  • win10 64位操作系统
  • idea2018.1.5
  • maven-3.0.5
  • jdk-8u162-windows-x64
  • mysql6.5

2、前提约束

完成springboot创建web项目 https://www.jianshu.com/p/de979f53ad80
注意:笔者创建项目的时候约束的包前缀是net.wanho.springboot.springbootweb,读者可以自行创建包名,只是要注意本文中的代码也要修改包名

3、修改pom.xml

在dependencies标签中加入以下依赖:

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

4、在主入口类同等路径下分别创建controller、service、mapper、entity包

修改主启动类,加入mapper扫描

@SpringBootApplication
@MapperScan("net.wanho.springboot.springbootweb.mapper")//扫描Mapper
public class SpringbootTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootTestApplication.class, args);
    }

}

在entity中加入User.java

package net.wanho.springboot.springbootweb.entity;
public class User {
    private int id;
    private String name;

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public User() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

在mapper中加入UserMapper.java

package net.wanho.springboot.springbootweb.mapper;

import net.wanho.springboot.springbootweb.entity.User;

import java.util.List;

public interface UserMapper {
    List<User> query();
}

在service中加入UserService.java

package net.wanho.springboot.springbootweb.service;

import net.wanho.springboot.springbootweb.entity.User;
import net.wanho.springboot.springbootweb.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public List<User> queryUsers()
    {
        return userMapper.query();
    }
}

在controller中加入UserController.java

package net.wanho.springboot.springbootweb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.wanho.springboot.springbootweb.entity.User;
import net.wanho.springboot.springbootweb.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/user/query")
    @ResponseBody
    public PageInfo<User> queryUsers() {

        //显示第1页,每页显示2条记录
        PageHelper.startPage(1,2);

        PageInfo<User> pageInfo = new PageInfo<>(userService.queryUsers());

        return pageInfo;
    }
}

5、在resources下创建一个mapper文件夹

在该文件下加入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="net.wanho.springboot.springbootweb.mapper.UserMapper">
    <select id="query" resultType="net.wanho.springboot.springbootweb.entity.User">
        select * from t_user
    </select>
</mapper>

6、修改application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
#笔者的数据库密码是zhangli,请读者根据自己数据库密码修改
spring.datasource.password=zhangli
mybatis.mapper-locations=classpath:mapper/*.xml

7、登录mysql数据库,创建表并完成初始化

执行以下sql脚本

create database test;
use test;
create table t_user(id int , name varchar(20));
insert into t_user(id,name) values(1,‘ali‘);
insert into t_user(id,name) values(2,‘zhangli‘);
insert into t_user(id,name) values(3,‘xiaoli‘);

8、启动并测试

在浏览器中输入http://localhost:8080/user/query,回车查看结果。具体操作如下:
springboot支持mybatis【分页】 03
至此,我们完成了springboot对mybatis的支持,并完成了测试。

相关推荐