按照pom文件补齐需要的依赖:
4.0.0
com.example
demo-1
0.0.1-SNAPSHOT
jar
demo-1
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
1.8
3.17
2.5.42
0.9.3
3.4.6
1.3.2
UTF-8
UTF-8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
runtime
com.alibaba
druid
1.0.29
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.3
org.springframework.boot
spring-boot-maven-plugin
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Demo2Application {
public static void main(String[] args) {
SpringApplication.run(Demo2Application.class, args);
}
}
application.properties
可以根据个人使用习惯选择使用properties
或者yml
文件,本项目使用的是yml配置文件,所以把原本application.properties
删除,在resource文件夹下创建一个application.yml
文件
#application port configuration
server.port=8080
#dataSource configuration
spring.datasource.name=springboots
spring.datasource.url=jdbc\:mysql\://127.0.0.1\:3306/mytest?useUnicode\=true&characterEncoding\=gbk&zeroDateTimeBehavior\=convertToNull
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.filters=stat
spring.datasource.maxActive=20
spring.datasource.initialSize=1
spring.datasource.maxWait=60000
spring.datasource.minIdle=1
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.maxOpenPreparedStatements=20
CREATE DATABASE mytest;
CREATE TABLE t_user(
userId INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
userName VARCHAR(255) NOT NULL ,
password VARCHAR(255) NOT NULL ,
phone VARCHAR(255) NOT NULL
) ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8;
UserDomain.java
package com.example.demo.entity;
public class UserDomain {
private Integer userId;
private String userName;
private String password;
private String phone;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
}
controller
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.demo.entity.UserDomain;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageHelper;
/**
* Created by Administrator on 2017/8/16.
*/
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@GetMapping("/all")
public Object findAllUser(int pageNum, int pageSize) {
//开始分页
PageHelper.startPage(pageNum,pageSize);
return userService.findAllUser(pageNum,pageSize);
}
}
service
package com.example.demo.service;
import java.util.List;
import com.example.demo.entity.UserDomain;
/**
* Created by Administrator on 2018/4/19.
*/
public interface UserService {
List findAllUser(int pageNum, int pageSize);
}
serviceImpl
package com.example.demo.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.entity.UserDomain;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageHelper;
/**
* Created by Administrator on 2017/8/16.
*/
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;//这里会报错,但是并不会影响
/*
* 这个方法中用到了我们开头配置依赖的分页插件pagehelper
* 很简单,只需要在service层传入参数,然后将参数传递给一个插件的一个静态方法即可;
* pageNum 开始页数
* pageSize 每页显示的数据条数
* */
@Override
public List findAllUser(int pageNum, int pageSize) {
//将参数传给这个方法就可以实现物理分页了,非常简单。
PageHelper.startPage(pageNum, pageSize);
return userMapper.selectUsers();
}
}
Mapper
package com.example.demo.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.example.demo.entity.UserDomain;
@Mapper
public interface UserMapper {
@Select(" SELECT\n" +
" user_id userId,\n" +
" user_name userName,\n" +
" password password,\n" +
" phone\n" +
" FROM\n" +
" t_user")
List selectUsers();
}
package com.example.demo.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.example.demo.entity.UserDomain;
@Mapper
public interface UserMapper {
int insert(UserDomain record);
List selectUsers();
}
mybatis.mapper-locations=classpath:mapper/*.xml
映射文件:UserMapper.xml
到这里所有的配置已经完成.在浏览器中访问:http://localhost:8080/user/all?pageNum=3&pageSize=4
其中参数是为了分页所用分别表示:pageNum:当前页数------pageSize:每页显示数量
代码资源地址: