先给 git 网址:https://github.com/kinber123/mysql.git
也提供免费CSDN 下载地址:https://download.csdn.net/download/wcy18818429914/12458344
(1)引入jar 包,pom.xml
mysql
mysql-connector-java
5.1.24
runtime
(2)application.yml 配置
#端口号
server:
port: 8082
#mysql 配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/oneself?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
typeAliasesPackage: com.example.demo.entity
(3)在数据库添加数据
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (1, 'wcy1', 1, 'YsB3ev9tTbAWxSWrtvk43fWMaR08fm9D');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (2, 'wx1', 1, '8SpvZlmoW5SVRkIeFacVsvZv5QBqyEYK');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (3, 'zwh1', 0, 'ZG2GgCaAqBaV68LjOPuHGnZjUuFTcncS');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (4, 'wcy2', 1, 'IPd3BrpcmB4iVoT0nHzA5NseL2Ijf6ae');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (5, 'wx2', 1, 'AxPESVUiQkTi8dhiUU6BPnzAVncV9iye');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (6, 'zwh2', 0, '06rKmbRwUfHLedDOixQfb1aFvuFuITY3');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (7, 'wcy3', 1, 'ajgidvE5A48ISgyIUsCADbuc6mv7sx0C');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (8, 'wx3', 1, '4XoOKEBPx8k6TZ4tb82vWlRfyzX6y62u');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (9, 'zwh3', 0, 'xy5w1YIvyVQFmNnI3tbRJJD0yhciUWyu');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (10, 'wcy4', 1, 'VZhQdOEbXHGZGp1gcGKULzXePLlfuMyj');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (11, 'wx5', 1, 'RLPyWh25WRSJOBo0hF0VUHsmxlwhq2Xu');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (12, 'zwh6', 0, 'wCtkhycsdjo58owwLOPYyqaJOM1KR0yF');
(4)实体类,数据库接口,mapper 如下
package com.example.demo.entity;
/**
* \* @author wcy
* \* @date: 2020-05-22 17:29
* \* Description: 类
* \
*/
public class Student {
/**
* // id 号
*/
private Integer id ;
/**
* // 名字
*/
private String name;
/**
* // 性别
*/
private Byte sex;
/**
* // 学号
*/
private String numberId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Byte getSex() {
return sex;
}
public void setSex(Byte sex) {
this.sex = sex;
}
public String getNumberId() {
return numberId;
}
public void setNumberId(String numberId) {
this.numberId = numberId;
}
}
package com.example.demo.dao;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.example.demo.entity.Student;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* \* @author wcy
* \* @date: 2020-05-22 17:29
* \* Description: 接口
* \
*/
@Mapper
public interface StudentDao extends BaseMapper {
/**
* 自己写MAPPER
* @return
*/
List getAllMapper();
}
id, name, sex, number_id
(5) 写个controller 类测试
package com.example.demo.controller;
import com.example.demo.entity.PageEntity;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* \* @author wcy
* \* @date: 2020-05-22 17:30
* \* Description: 类
* \
*/
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
/**
* 自己写的mapper
* @return
*/
@RequestMapping("/getAllMapper")
public Object getAllMapper(){
return studentService.getAllMapper();
}
@RequestMapping("/test")
public Object test(){
return "成功了";
}
}
(6)显示结果:http://localhost:8082/getAllMapper
(1)引入jar 包,pom.xml
mysql
mysql-connector-java
5.1.24
runtime
com.baomidou
mybatis-plus-boot-starter
2.3
(2)application.yml 配置
#端口号
server:
port: 8082
#mysql 配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/oneself?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
#mybatis 配置
mybatis-plus:
type-aliases-package: com.example.demo.entity
mapper-locations: classpath*:mapper/*Mapper.xml
global-config:
db-config:
id-type: auto
field-strategy: not_empty
#驼峰下划线转换
column-underline: true
#逻辑删除配置
logic-delete-value: 0
logic-not-delete-value: 1
db-type: mysql
refresh: false
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
(3)在数据库添加数据
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (1, 'wcy1', 1, 'YsB3ev9tTbAWxSWrtvk43fWMaR08fm9D');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (2, 'wx1', 1, '8SpvZlmoW5SVRkIeFacVsvZv5QBqyEYK');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (3, 'zwh1', 0, 'ZG2GgCaAqBaV68LjOPuHGnZjUuFTcncS');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (4, 'wcy2', 1, 'IPd3BrpcmB4iVoT0nHzA5NseL2Ijf6ae');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (5, 'wx2', 1, 'AxPESVUiQkTi8dhiUU6BPnzAVncV9iye');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (6, 'zwh2', 0, '06rKmbRwUfHLedDOixQfb1aFvuFuITY3');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (7, 'wcy3', 1, 'ajgidvE5A48ISgyIUsCADbuc6mv7sx0C');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (8, 'wx3', 1, '4XoOKEBPx8k6TZ4tb82vWlRfyzX6y62u');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (9, 'zwh3', 0, 'xy5w1YIvyVQFmNnI3tbRJJD0yhciUWyu');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (10, 'wcy4', 1, 'VZhQdOEbXHGZGp1gcGKULzXePLlfuMyj');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (11, 'wx5', 1, 'RLPyWh25WRSJOBo0hF0VUHsmxlwhq2Xu');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (12, 'zwh6', 0, 'wCtkhycsdjo58owwLOPYyqaJOM1KR0yF');
(4)实体类,数据库接口,mapper 如下
package com.example.demo.entity;
/**
* \* @author wcy
* \* @date: 2020-05-22 17:29
* \* Description: 类
* \
*/
public class Student {
/**
* // id 号
*/
private Integer id ;
/**
* // 名字
*/
private String name;
/**
* // 性别
*/
private Byte sex;
/**
* // 学号
*/
private String numberId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Byte getSex() {
return sex;
}
public void setSex(Byte sex) {
this.sex = sex;
}
public String getNumberId() {
return numberId;
}
public void setNumberId(String numberId) {
this.numberId = numberId;
}
}
package com.example.demo.dao;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.example.demo.entity.Student;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* \* @author wcy
* \* @date: 2020-05-22 17:29
* \* Description: 接口
* \
*/
@Mapper
public interface StudentDao extends BaseMapper {
/**
* 自己写MAPPER
* @return
*/
List getAllMapper();
}
id, name, sex, number_id
(5) 写个controller 类测试
package com.example.demo.controller;
import com.example.demo.entity.PageEntity;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* \* @author wcy
* \* @date: 2020-05-22 17:30
* \* Description: 类
* \
*/
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
/**
* mybatis-plus 自动调用的
* @return
*/
@RequestMapping("/getAll")
public Object getAll(){
return studentService.getAll();
}
/**
* 自己写的mapper
* @return
*/
@RequestMapping("/getAllMapper")
public Object getAllMapper(){
return studentService.getAllMapper();
}
@RequestMapping("/test")
public Object test(){
return "成功了";
}
}
(6)显示结果:http://localhost:8082/getAll
(1)引入jar 包,pom.xml
mysql
mysql-connector-java
5.1.24
runtime
com.baomidou
mybatis-plus-boot-starter
2.3
com.alibaba
fastjson
1.2.60
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.13
(2)application.yml 配置
#端口号
server:
port: 8082
#mysql 配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/oneself?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
#mybatis 配置
mybatis-plus:
type-aliases-package: com.example.demo.entity
mapper-locations: classpath*:mapper/*Mapper.xml
global-config:
db-config:
id-type: auto
field-strategy: not_empty
#驼峰下划线转换
column-underline: true
#逻辑删除配置
logic-delete-value: 0
logic-not-delete-value: 1
db-type: mysql
refresh: false
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
(3)在数据库添加数据
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (1, 'wcy1', 1, 'YsB3ev9tTbAWxSWrtvk43fWMaR08fm9D');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (2, 'wx1', 1, '8SpvZlmoW5SVRkIeFacVsvZv5QBqyEYK');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (3, 'zwh1', 0, 'ZG2GgCaAqBaV68LjOPuHGnZjUuFTcncS');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (4, 'wcy2', 1, 'IPd3BrpcmB4iVoT0nHzA5NseL2Ijf6ae');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (5, 'wx2', 1, 'AxPESVUiQkTi8dhiUU6BPnzAVncV9iye');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (6, 'zwh2', 0, '06rKmbRwUfHLedDOixQfb1aFvuFuITY3');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (7, 'wcy3', 1, 'ajgidvE5A48ISgyIUsCADbuc6mv7sx0C');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (8, 'wx3', 1, '4XoOKEBPx8k6TZ4tb82vWlRfyzX6y62u');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (9, 'zwh3', 0, 'xy5w1YIvyVQFmNnI3tbRJJD0yhciUWyu');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (10, 'wcy4', 1, 'VZhQdOEbXHGZGp1gcGKULzXePLlfuMyj');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (11, 'wx5', 1, 'RLPyWh25WRSJOBo0hF0VUHsmxlwhq2Xu');
INSERT INTO `student` (`id`, `name`, `sex`, `number_id`) VALUES (12, 'zwh6', 0, 'wCtkhycsdjo58owwLOPYyqaJOM1KR0yF');
(4)实体类,数据库接口,mapper 如下
package com.example.demo.entity;
/**
* \* @author wcy
* \* @date: 2020-05-22 17:29
* \* Description: 类
* \
*/
public class Student {
/**
* // id 号
*/
private Integer id ;
/**
* // 名字
*/
private String name;
/**
* // 性别
*/
private Byte sex;
/**
* // 学号
*/
private String numberId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Byte getSex() {
return sex;
}
public void setSex(Byte sex) {
this.sex = sex;
}
public String getNumberId() {
return numberId;
}
public void setNumberId(String numberId) {
this.numberId = numberId;
}
}
package com.example.demo.dao;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.example.demo.entity.Student;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* \* @author wcy
* \* @date: 2020-05-22 17:29
* \* Description: 接口
* \
*/
@Mapper
public interface StudentDao extends BaseMapper {
/**
* 自己写MAPPER
* @return
*/
List getAllMapper();
/**
* 分页查询
* @return
*/
List getUserList();
/**
* 分页查询
* @return
*/
Page getUserListPage();
}
id, name, sex, number_id
(5) 实现PageEntity 类
package com.example.demo.entity;
/**
* \* @author wcy
* \* @date: 2020-05-25 16:22
* \* Description: 类
* \
*/
public class PageEntity {
private Integer page;
private Integer rows;
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getRows() {
return rows;
}
public void setRows(Integer rows) {
this.rows = rows;
}
}
(6) 写个controller 类测试
package com.example.demo.controller;
import com.example.demo.entity.PageEntity;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* \* @author wcy
* \* @date: 2020-05-22 17:30
* \* Description: 类
* \
*/
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
/**
* mybatis-plus 自动调用的
* @return
*/
@RequestMapping("/getAll")
public Object getAll(){
return studentService.getAll();
}
/**
* 自己写的mapper
* @return
*/
@RequestMapping("/getAllMapper")
public Object getAllMapper(){
return studentService.getAllMapper();
}
/**
* pagehelper 分页查询1
* @return
*/
@RequestMapping("/getUserList")
public Object getUserList(PageEntity page){
return studentService.getUserList(page);
}
/**
* pagehelper 分页查询2
* @return
*/
@RequestMapping("/getUserListPage")
public Object getUserListPage(PageEntity page ){
return studentService.getUserListPage(page);
}
@RequestMapping("/test")
public Object test(){
return "成功了";
}
}
(6)显示结果:http://localhost:8082/getUserListPage?page=1&rows=5
注:有疑问,请留言。谢谢