1.项目的整个结构
2.需要引入的spring-data-jpa的依赖为
org.springframework.boot
spring-boot-starter-data-jpa
2.4.5
3.项目的整个依赖
org.springframework.boot
spring-boot-starter-parent
2.4.5
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-data-jdbc
org.springframework.boot
spring-boot-starter-data-jpa
2.4.5
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
8.0.19
org.apache.commons
commons-lang3
3.9
log4j
log4j
1.2.12
com.alibaba
fastjson
1.2.75
org.projectlombok
lombok
4.配置文件application.yml的具体内容为
server:
port: 9000
#配置数据库
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/shiro?useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC
username: root
password: 123456
# 开启jpa自动建表
jpa:
# 该配置的的主要作用是:自动创建、更新、验证数据库表结构。该参数的几种配置如下:
#create:每次加载Hibernate时都会删除上一次生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。
#create-drop:每次加载Hibernate时根据model类生成表,但是SessionFactory一关闭,表就会自动删除
#update:最常用的属性,第一次加载Hibernate时根据model类会自动建立起表的架构(前提是先建立好数据库),以后加载Hibernate时根据model类自动更新表结构,即使表结构改变了,但表中的行仍然存在不会删除以前的行。要注意的是,当部署到服务器后,表结构不会马上建立起来的, 是要等应用第一次运行起来后才会。
#validate:每次加载Hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值
hibernate:
ddl-auto: update
#看见SQL语句 true:控制台看见 false:控制台直接显示数据
show-sql: true
#日志的配置
logging:
level:
edu.zut.manager.dao: debug #指定打印指定包下的日志级别
5. SysUser.java
import javax.persistence.*;
import java.io.Serializable;
@Entity //表示为一个实体类,开启spring-data-jpa自动生成表时需要添加该注解
@Table //映射数据库中的表可以指定数据库中表的名称 例如:@Table(name = "sys_user")
@Data //lombok提供的注解,不用写 get,set方法, lombok还有以下两个注解
//@AllArgsConstructor : 注在类上,提供类的全参构造
//@NoArgsConstructor : 注在类上,提供类的无参构造
public class SysUser implements Serializable {
public SysUser(){
}
public SysUser(String name, String password) {
this.name = name;
this.password = password;
}
public SysUser(String name, String password,Integer status, String role) {
this.name = name;
this.password = password;
this.status = status;
this.role = role;
}
@Id //标注为主键
//主键生成策略,GenerationType.IDENTITY,数据库自增策略,不用指定id,这个一般就够用了
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
//@Column(name = "name")对应生成数据库中的字段,可以用name属性指定字段名
@Column(name = "name")
private String name;
@Column(name = "password")
private String password;
@Column(name = "status")
private Integer status;
@Column(name = "role")
private String role;
@Override
public String toString() {
return "SysUser{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", status=" + status +
", role='" + role + '\'' +
'}';
}
}
6.QueryInfo.java
import lombok.Data;
//定义的传输数据的实体类
@Data
public class QueryInfo {
public QueryInfo() {
}
private String query;
private int pageNum;
private int pageSize;
}
7.SysUserDao.java
import com.example.demo.entity.SysUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.List;
@Component
public interface SysUserDao extends JpaRepository {
//我这里直接继承JpaRepository<>,两个参数为实体类,和主键类型
//JpaRepository<>,已经为我们提供了日常CURD的方法
SysUser findSysUserById(Long id); //自己定义的方法,jpa会根据方法名为我们实现该方法
}
8. SysUserService.java
import com.example.demo.entity.SysUser;
import java.util.List;
public interface SysUserService {
List findAll();
SysUser findSysUserById(Long id);
SysUser updateState(SysUser sysUser);
}
9.SysUserServiceImpl
import com.example.demo.dao.SysUserDao;
import com.example.demo.entity.SysUser;
import com.example.demo.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class SysUserServiceImpl implements SysUserService {
@Autowired
private SysUserDao sysUserDao;
@Override
public List findAll() {
List userList = sysUserDao.findAll();
return userList;
}
@Override
public SysUser findSysUserById(Long id) {
return sysUserDao.findSysUserById(id);
}
@Override
public SysUser updateState(SysUser sysUser) {
//直接调用SysUserDao继承JpaRepository中的save方法,该方法会根据是否已经有主键,进行保存或更新数据库中的数据
return sysUserDao.save(sysUser);
}
public Page findAllUser(int page, int size) {
Pageable pageable = PageRequest.of(page,size);
Page sysUserPage = sysUserDao.findAll(pageable);
return sysUserPage;
}
}
10.SysUserController.java
import com.alibaba.fastjson.JSON;
import com.example.demo.entity.QueryInfo;
import com.example.demo.entity.SysUser;
import com.example.demo.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@RestController
public class SysUserController {
@Autowired
SysUserServiceImpl sysUserService;
/**
*
* @param pageNum 第几页
* @param pageSize 每页的数量
* @return
*/
@RequestMapping("/findAll")
public String findAllUser(QueryInfo queryInfo){
//int pageNum,int pageSize
//QueryInfo queryInfo
int pageNum = queryInfo.getPageNum();
int pageSize = queryInfo.getPageSize();
Page page = sysUserService.findAllUser(pageNum,pageSize);
Long total = page.getTotalElements();
HashMap map = new HashMap<>();
map.put("number",total);
map.put("userList",page.getContent());
//将Map集合转化为JSON数据格式
String res_string = JSON.toJSONString(map);
return res_string;
}
@RequestMapping("/changeState")
public String changeState(SysUser sysUser){
SysUser sysUser1 = sysUserService.findSysUserById(sysUser.getId());
sysUser1.setStatus(sysUser.getStatus());
sysUserService.updateState(sysUser1);
return "ok";
}
}
11.进行接口测试,传输参数为下列格式,?后边为请求体的属性值,之间用&连接
http://127.0.0.1:9000/findAll?pageNum=0&pageSize=12
12.结果展示