springboot终于迎来了2.0版本,很多新的特性让springboot更加强大,之前使用1.5.6版本整合了Mybatis,现在2.0版本就已经不适用了,所以,在摸索中搭建了2.0版本整合Mybatis
添加基础的依赖:
按照pom文件补齐需要的依赖:
4.0.0
com.sao
demo
0.0.1-SNAPSHOT
jar
demo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-jdbc
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
org.apache.commons
commons-lang3
3.4
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
com.alibaba
druid-spring-boot-starter
1.1.9
com.alibaba
fastjson
1.1.29
org.springframework.boot
spring-boot-maven-plugin
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
${basedir}/src/main/resources/generator/generatorConfig.xml
true
true
项目启动类:
package com.winterchen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.winterchen.dao")
public class Springboot2MybatisDemoApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot2MybatisDemoApplication.class, args);
}
}
可以根据个人使用习惯选择使用
properties
或者yml
文件,本项目使用的是yml配置文件,所以把原本application.properties
删除,创建一个application.yml
文件
在resource文件夹下创建application.yml
server:
port: 8080
spring:
datasource:
name: mysql_test
type: com.alibaba.druid.pool.DruidDataSource
#druid相关配置
druid:
#监控统计拦截的filters
filters: stat
driver-class-name: com.mysql.jdbc.Driver
#基本属性
url: jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
username: root
password: root
#配置初始化大小/最小/最大
initial-size: 1
min-idle: 1
max-active: 20
#获取连接等待超时时间
max-wait: 60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
time-between-eviction-runs-millis: 60000
#一个连接在池中最小生存的时间
min-evictable-idle-time-millis: 300000
validation-query: SELECT 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
#打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: 20
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.winterchen.model
#pagehelper
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
returnPageInfo: check
model
,dao
,mapper
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.winterchen.model;
public class UserDomain {
private Integer userId;
private String userName;
private String password;
private String phone;
// get,set方法略...
}
UserDao.java
package com.winterchen.dao;
import com.winterchen.model.UserDomain;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
public interface UserDao {
int insert(UserDomain record);
List selectUsers();
}
UserMapper.xml
t_user
userId,userName,password,phone
INSERT INTO
userName,password,
phone,
#{userName, jdbcType=VARCHAR},#{password, jdbcType=VARCHAR},
#{phone, jdbcType=VARCHAR},
controller
,service
包和文件UserService.java
package com.winterchen.service.user;
import com.github.pagehelper.PageInfo;
import com.winterchen.model.UserDomain;
import java.util.List;
/**
* Created by Administrator on 2018/4/19.
*/
public interface UserService {
int addUser(UserDomain user);
PageInfo findAllUser(int pageNum, int pageSize);
}
UserServiceImpl
package com.winterchen.service.user.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.winterchen.dao.UserDao;
import com.winterchen.model.UserDomain;
import com.winterchen.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by Administrator on 2017/8/16.
*/
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;//这里会报错,但是并不会影响
@Override
public int addUser(UserDomain user) {
return userDao.insert(user);
}
/*
* 这个方法中用到了我们开头配置依赖的分页插件pagehelper
* 很简单,只需要在service层传入参数,然后将参数传递给一个插件的一个静态方法即可;
* pageNum 开始页数
* pageSize 每页显示的数据条数
* */
@Override
public PageInfo findAllUser(int pageNum, int pageSize) {
//将参数传给这个方法就可以实现物理分页了,非常简单。
PageHelper.startPage(pageNum, pageSize);
List userDomains = userDao.selectUsers();
PageInfo result = new PageInfo(userDomains);
return result;
}
}
UserController.java
package com.winterchen.controller;
import com.github.pagehelper.PageHelper;
import com.winterchen.model.UserDomain;
import com.winterchen.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/**
* Created by Administrator on 2017/8/16.
*/
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@PostMapping("/add")
public int addUser(UserDomain user){
return userService.addUser(user);
}
@ResponseBody
@GetMapping("/all")
public Object findAllUser(
@RequestParam(name = "pageNum", required = false, defaultValue = "1")
int pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "10")
int pageSize){
return userService.findAllUser(pageNum,pageSize);
}
}
到这里如果项目就成功搭建完成了,如果还是报错的话,请仔细看看配置,后面会给出源码地址,程序员就是要不断和bug进行斗争,加油。
启动项目
这样就表示启动成功了
然后,开始测试吧,博主使用的是postMan,一个进行http请求的测试工具
个人分类: java学习之路
springboot终于迎来了2.0版本,很多新的特性让springboot更加强大,之前使用1.5.6版本整合了Mybatis,现在2.0版本就已经不适用了,所以,在摸索中搭建了2.0版本整合Mybatis
添加基础的依赖:
按照pom文件补齐需要的依赖:
4.0.0
com.sao
demo
0.0.1-SNAPSHOT
jar
demo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-jdbc
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
org.apache.commons
commons-lang3
3.4
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
com.alibaba
druid-spring-boot-starter
1.1.9
com.alibaba
fastjson
1.1.29
org.springframework.boot
spring-boot-maven-plugin
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
${basedir}/src/main/resources/generator/generatorConfig.xml
true
true
项目启动类:
package com.winterchen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.winterchen.dao")
public class Springboot2MybatisDemoApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot2MybatisDemoApplication.class, args);
}
}
可以根据个人使用习惯选择使用
properties
或者yml
文件,本项目使用的是yml配置文件,所以把原本application.properties
删除,创建一个application.yml
文件
在resource文件夹下创建application.yml
server:
port: 8080
spring:
datasource:
name: mysql_test
type: com.alibaba.druid.pool.DruidDataSource
#druid相关配置
druid:
#监控统计拦截的filters
filters: stat
driver-class-name: com.mysql.jdbc.Driver
#基本属性
url: jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
username: root
password: root
#配置初始化大小/最小/最大
initial-size: 1
min-idle: 1
max-active: 20
#获取连接等待超时时间
max-wait: 60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
time-between-eviction-runs-millis: 60000
#一个连接在池中最小生存的时间
min-evictable-idle-time-millis: 300000
validation-query: SELECT 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
#打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: 20
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.winterchen.model
#pagehelper
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
returnPageInfo: check
model
,dao
,mapper
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.winterchen.model;
public class UserDomain {
private Integer userId;
private String userName;
private String password;
private String phone;
// get,set方法略...
}
UserDao.java
package com.winterchen.dao;
import com.winterchen.model.UserDomain;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
public interface UserDao {
int insert(UserDomain record);
List selectUsers();
}
UserMapper.xml
t_user
userId,userName,password,phone
INSERT INTO
userName,password,
phone,
#{userName, jdbcType=VARCHAR},#{password, jdbcType=VARCHAR},
#{phone, jdbcType=VARCHAR},
controller
,service
包和文件UserService.java
package com.winterchen.service.user;
import com.github.pagehelper.PageInfo;
import com.winterchen.model.UserDomain;
import java.util.List;
/**
* Created by Administrator on 2018/4/19.
*/
public interface UserService {
int addUser(UserDomain user);
PageInfo findAllUser(int pageNum, int pageSize);
}
UserServiceImpl
package com.winterchen.service.user.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.winterchen.dao.UserDao;
import com.winterchen.model.UserDomain;
import com.winterchen.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by Administrator on 2017/8/16.
*/
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;//这里会报错,但是并不会影响
@Override
public int addUser(UserDomain user) {
return userDao.insert(user);
}
/*
* 这个方法中用到了我们开头配置依赖的分页插件pagehelper
* 很简单,只需要在service层传入参数,然后将参数传递给一个插件的一个静态方法即可;
* pageNum 开始页数
* pageSize 每页显示的数据条数
* */
@Override
public PageInfo findAllUser(int pageNum, int pageSize) {
//将参数传给这个方法就可以实现物理分页了,非常简单。
PageHelper.startPage(pageNum, pageSize);
List userDomains = userDao.selectUsers();
PageInfo result = new PageInfo(userDomains);
return result;
}
}
UserController.java
package com.winterchen.controller;
import com.github.pagehelper.PageHelper;
import com.winterchen.model.UserDomain;
import com.winterchen.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/**
* Created by Administrator on 2017/8/16.
*/
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@PostMapping("/add")
public int addUser(UserDomain user){
return userService.addUser(user);
}
@ResponseBody
@GetMapping("/all")
public Object findAllUser(
@RequestParam(name = "pageNum", required = false, defaultValue = "1")
int pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "10")
int pageSize){
return userService.findAllUser(pageNum,pageSize);
}
}
到这里如果项目就成功搭建完成了,如果还是报错的话,请仔细看看配置,后面会给出源码地址,程序员就是要不断和bug进行斗争,加油。
启动项目
这样就表示启动成功了
然后,开始测试吧,博主使用的是postMan,一个进行http请求的测试工具
springboot终于迎来了2.0版本,很多新的特性让springboot更加强大,之前使用1.5.6版本整合了Mybatis,现在2.0版本就已经不适用了,所以,在摸索中搭建了2.0版本整合Mybatis
添加基础的依赖:
按照pom文件补齐需要的依赖:
4.0.0
com.sao
demo
0.0.1-SNAPSHOT
jar
demo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-jdbc
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
org.apache.commons
commons-lang3
3.4
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
com.alibaba
druid-spring-boot-starter
1.1.9
com.alibaba
fastjson
1.1.29
org.springframework.boot
spring-boot-maven-plugin
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
${basedir}/src/main/resources/generator/generatorConfig.xml
true
true
项目启动类:
package com.winterchen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.winterchen.dao")
public class Springboot2MybatisDemoApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot2MybatisDemoApplication.class, args);
}
}
可以根据个人使用习惯选择使用
properties
或者yml
文件,本项目使用的是yml配置文件,所以把原本application.properties
删除,创建一个application.yml
文件
在resource文件夹下创建application.yml
server:
port: 8080
spring:
datasource:
name: mysql_test
type: com.alibaba.druid.pool.DruidDataSource
#druid相关配置
druid:
#监控统计拦截的filters
filters: stat
driver-class-name: com.mysql.jdbc.Driver
#基本属性
url: jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
username: root
password: root
#配置初始化大小/最小/最大
initial-size: 1
min-idle: 1
max-active: 20
#获取连接等待超时时间
max-wait: 60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
time-between-eviction-runs-millis: 60000
#一个连接在池中最小生存的时间
min-evictable-idle-time-millis: 300000
validation-query: SELECT 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
#打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: 20
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.winterchen.model
#pagehelper
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
returnPageInfo: check
model
,dao
,mapper
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.winterchen.model;
public class UserDomain {
private Integer userId;
private String userName;
private String password;
private String phone;
// get,set方法略...
}
UserDao.java
package com.winterchen.dao;
import com.winterchen.model.UserDomain;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
public interface UserDao {
int insert(UserDomain record);
List selectUsers();
}
UserMapper.xml
t_user
userId,userName,password,phone
INSERT INTO
userName,password,
phone,
#{userName, jdbcType=VARCHAR},#{password, jdbcType=VARCHAR},
#{phone, jdbcType=VARCHAR},
controller
,service
包和文件UserService.java
package com.winterchen.service.user;
import com.github.pagehelper.PageInfo;
import com.winterchen.model.UserDomain;
import java.util.List;
/**
* Created by Administrator on 2018/4/19.
*/
public interface UserService {
int addUser(UserDomain user);
PageInfo findAllUser(int pageNum, int pageSize);
}
UserServiceImpl
package com.winterchen.service.user.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.winterchen.dao.UserDao;
import com.winterchen.model.UserDomain;
import com.winterchen.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by Administrator on 2017/8/16.
*/
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;//这里会报错,但是并不会影响
@Override
public int addUser(UserDomain user) {
return userDao.insert(user);
}
/*
* 这个方法中用到了我们开头配置依赖的分页插件pagehelper
* 很简单,只需要在service层传入参数,然后将参数传递给一个插件的一个静态方法即可;
* pageNum 开始页数
* pageSize 每页显示的数据条数
* */
@Override
public PageInfo findAllUser(int pageNum, int pageSize) {
//将参数传给这个方法就可以实现物理分页了,非常简单。
PageHelper.startPage(pageNum, pageSize);
List userDomains = userDao.selectUsers();
PageInfo result = new PageInfo(userDomains);
return result;
}
}
UserController.java
package com.winterchen.controller;
import com.github.pagehelper.PageHelper;
import com.winterchen.model.UserDomain;
import com.winterchen.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/**
* Created by Administrator on 2017/8/16.
*/
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@PostMapping("/add")
public int addUser(UserDomain user){
return userService.addUser(user);
}
@ResponseBody
@GetMapping("/all")
public Object findAllUser(
@RequestParam(name = "pageNum", required = false, defaultValue = "1")
int pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "10")
int pageSize){
return userService.findAllUser(pageNum,pageSize);
}
}
到这里如果项目就成功搭建完成了,如果还是报错的话,请仔细看看配置,后面会给出源码地址,程序员就是要不断和bug进行斗争,加油。
启动项目
这样就表示启动成功了
然后,开始测试吧,博主使用的是postMan,一个进行http请求的测试工具
个人分类: java学习之路
springboot终于迎来了2.0版本,很多新的特性让springboot更加强大,之前使用1.5.6版本整合了Mybatis,现在2.0版本就已经不适用了,所以,在摸索中搭建了2.0版本整合Mybatis
添加基础的依赖:
按照pom文件补齐需要的依赖:
4.0.0
com.sao
demo
0.0.1-SNAPSHOT
jar
demo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-jdbc
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
org.apache.commons
commons-lang3
3.4
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
com.alibaba
druid-spring-boot-starter
1.1.9
com.alibaba
fastjson
1.1.29
org.springframework.boot
spring-boot-maven-plugin
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
${basedir}/src/main/resources/generator/generatorConfig.xml
true
true
项目启动类:
package com.winterchen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.winterchen.dao")
public class Springboot2MybatisDemoApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot2MybatisDemoApplication.class, args);
}
}
可以根据个人使用习惯选择使用
properties
或者yml
文件,本项目使用的是yml配置文件,所以把原本application.properties
删除,创建一个application.yml
文件
在resource文件夹下创建application.yml
server:
port: 8080
spring:
datasource:
name: mysql_test
type: com.alibaba.druid.pool.DruidDataSource
#druid相关配置
druid:
#监控统计拦截的filters
filters: stat
driver-class-name: com.mysql.jdbc.Driver
#基本属性
url: jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
username: root
password: root
#配置初始化大小/最小/最大
initial-size: 1
min-idle: 1
max-active: 20
#获取连接等待超时时间
max-wait: 60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
time-between-eviction-runs-millis: 60000
#一个连接在池中最小生存的时间
min-evictable-idle-time-millis: 300000
validation-query: SELECT 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
#打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: 20
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.winterchen.model
#pagehelper
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
returnPageInfo: check
model
,dao
,mapper
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.winterchen.model;
public class UserDomain {
private Integer userId;
private String userName;
private String password;
private String phone;
// get,set方法略...
}
UserDao.java
package com.winterchen.dao;
import com.winterchen.model.UserDomain;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
public interface UserDao {
int insert(UserDomain record);
List selectUsers();
}
UserMapper.xml
t_user
userId,userName,password,phone
INSERT INTO
userName,password,
phone,
#{userName, jdbcType=VARCHAR},#{password, jdbcType=VARCHAR},
#{phone, jdbcType=VARCHAR},
controller
,service
包和文件UserService.java
package com.winterchen.service.user;
import com.github.pagehelper.PageInfo;
import com.winterchen.model.UserDomain;
import java.util.List;
/**
* Created by Administrator on 2018/4/19.
*/
public interface UserService {
int addUser(UserDomain user);
PageInfo findAllUser(int pageNum, int pageSize);
}
UserServiceImpl
package com.winterchen.service.user.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.winterchen.dao.UserDao;
import com.winterchen.model.UserDomain;
import com.winterchen.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by Administrator on 2017/8/16.
*/
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;//这里会报错,但是并不会影响
@Override
public int addUser(UserDomain user) {
return userDao.insert(user);
}
/*
* 这个方法中用到了我们开头配置依赖的分页插件pagehelper
* 很简单,只需要在service层传入参数,然后将参数传递给一个插件的一个静态方法即可;
* pageNum 开始页数
* pageSize 每页显示的数据条数
* */
@Override
public PageInfo findAllUser(int pageNum, int pageSize) {
//将参数传给这个方法就可以实现物理分页了,非常简单。
PageHelper.startPage(pageNum, pageSize);
List userDomains = userDao.selectUsers();
PageInfo result = new PageInfo(userDomains);
return result;
}
}
UserController.java
package com.winterchen.controller;
import com.github.pagehelper.PageHelper;
import com.winterchen.model.UserDomain;
import com.winterchen.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/**
* Created by Administrator on 2017/8/16.
*/
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@PostMapping("/add")
public int addUser(UserDomain user){
return userService.addUser(user);
}
@ResponseBody
@GetMapping("/all")
public Object findAllUser(
@RequestParam(name = "pageNum", required = false, defaultValue = "1")
int pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "10")
int pageSize){
return userService.findAllUser(pageNum,pageSize);
}
}
到这里如果项目就成功搭建完成了,如果还是报错的话,请仔细看看配置,后面会给出源码地址,程序员就是要不断和bug进行斗争,加油。
启动项目
这样就表示启动成功了
然后,开始测试吧,博主使用的是postMan,一个进行http请求的测试工具
个人分类: java学习之路