當前位置:
首頁 > 知識 > Spring boot+MyBatis+PageHelper+JSON

Spring boot+MyBatis+PageHelper+JSON

Spring boot+MyBatis+PageHelper+JSON

Spring

Spring boot+MyBatis+PageHelper+JSON

Spring boot

Spring boot+MyBatis+PageHelper+JSON

MyBatis

時間緊,找了好多資料,花了半天來學習和搭建,有錯誤請不吝指出。

這也看出Spring boot確實很方便開發。

有什麼需要添加的以後再補充吧。


1、pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.leo

springboot

0.0.1-SNAPSHOT

jar

springboot

http://maven.apache.org

org.springframework.boot

spring-boot-starter-parent

1.5.4.RELEASE

1.8

UTF-8

org.springframework.boot

spring-boot-starter-web

com.alibaba

druid

1.0.31

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.0

com.github.pagehelper

pagehelper-spring-boot-starter

1.1.2

mysql

mysql-connector-java

org.springframework.boot

spring-boot-maven-plugin

org.springframework

springloaded

1.2.7.RELEASE

2、application.yml

文件放在srcmain
esourcesconfig目錄下


spring:

aop:

proxy-target-class: true

datasource:

name: ssm

url: jdbc:mysql://192.168.56.102:3306/ssm?useUnicode=true&characterEncoding=utf-8&useSSL=false

username: root

password: MyNewPwd4!

# 使用druid數據源

type: com.alibaba.druid.pool.DruidDataSource

driver-class-name: com.mysql.jdbc.Driver

filters: stat

maxActive: 20

initialSize: 1

maxWait: 60000

minIdle: 1

timeBetweenEvictionRunsMillis: 60000

minEvictableIdleTimeMillis: 300000

validationQuery: select "x"

testWhileIdle: true

testOnBorrow: false

testOnReturn: false

poolPreparedStatements: true

maxOpenPreparedStatements: 20

mybatis:

mapperLocations: classpath:mapper/*.xml

typeAliasesPackage: org.leo.springboot.pojo

#pagehelper.

pagehelper:

autoDialect: true

closeConn: true

3、mapper

mapper的XML放在srcmain
esourcesmapper下


Mapper介面:


package org.leo.springboot.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import org.leo.springboot.pojo.TUser;

@Mapper

public interface TUserMapper {

public List findList(TUser user);

}

4、Service

介面就不附上了。


package org.leo.springboot.service.impl;

import java.util.List;

import org.leo.springboot.mapper.TUserMapper;

import org.leo.springboot.pojo.TUser;

import org.leo.springboot.service.IUserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

@Service

@Transactional

public class UserService implements IUserService {

@Autowired

private TUserMapper tUserMapper;

@Override

public List findList(TUser user) {

return tUserMapper.findList(user);

}

}

5、Controller

package org.leo.springboot.controller;

import java.util.List;

import org.leo.springboot.pojo.TUser;

import org.leo.springboot.service.IUserService;

import org.leo.springboot.vo.UserReq;

import org.leo.springboot.vo.UserRes;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;

@RestController

@EnableAutoConfiguration

@RequestMapping("/user")

public class UserController {

@Autowired

private IUserService userService;

@RequestMapping("/{id}")

List testGet(@PathVariable("id") int id) {

List users1 = userService.findList(new TUser(id, null, null, null));

System.out.println("---" + users1.toString());

PageHelper.startPage(1, 1);

List users2 = userService.findList(new TUser());

System.out.println("===" + users2.toString());

List users3 = userService.findList(new TUser());

System.out.println("用戶id是 " + id + " 的username是 " + users1.get(0).getUsername() + " 。員工總數是" + users3.size());

return users3;

}

@RequestMapping(value = "/testpost", method = RequestMethod.POST)

@ResponseBody

UserRes testPost(@RequestBody UserReq req) {

System.out.println(req.toString());

List users1 = userService.findList(new TUser(req.getId(), null, null, null));

System.out.println(users1.toString());

return new UserRes(0, "成功", users1.get(0));

}

}

這裡有兩個方法,都返回json數據:

1、 testGet,用來演示get方法,其中包含使用PageHelper來分頁的代碼。

2、 testPost,用來演示post接收json數據。

裡面具體的實體類就不附上了。


6、Spring boot啟動

package org.leo.springboot;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class);

}

}

直接運行。


7、測試

Get

Spring boot+MyBatis+PageHelper+JSON

瀏覽器訪問測試Get方法

POST

Spring boot+MyBatis+PageHelper+JSON

Postman測試POST方法

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 Java個人學習心得 的精彩文章:

Java操作Linux命令分割合并文本文件及其他
Dubbo管理控制台安裝

TAG:Java個人學習心得 |

您可能感興趣

SpringBoot:SpringDataRedis緩存改造
Spring security + oauth2.0 + redis + mybatis plus 搭建微服務
乾貨——Spring-Security-Mybatis-Demo
Spring data MongoDB 之 MongoRepository
Spring Security 5.0 的 DelegatingPasswordEncoder 詳解
Spring4+hibernate+SpringMvc整合
Spring中BeanFactory和ApplicationContext 的區別
Springboot2.X之切換使用Servlet容器Jetty、Tomcat、Undertow
Mysql8.0主從搭建,shardingsphere+springboot+mybatis讀寫分離
Spring MVC之DispatcherServlet初始化詳解
Spring整合Hibernate.Final
SPRING 2018 COUTURE Alberta Ferretti Limited Edition
使用Spring Boot + Resilience 4j實現斷路器
深入 JVM 分析 spring-boot 應用 hibernate-validatorNoClassDefFoundError
新鮮秀場 | SPRING 2018 COUTURE Alberta Ferretti Limited Edition
API網關性能比較:NGINX vs.ZUUL vs.Spring Cloud Gateway vs.Linkerd
Spring Cloud Alibaba Sentinel 整合 Feign 的設計實現
Spring MVC接受List Array Map參數
帶著新人學springboot的應用04(springboot+mybatis+redis 完)
Spring Cloud Gateway的After路由斷言工廠