使用springboot+jpa方法搭建数据访问接口

  • 建立实体类,跟数据库表字段一致

  • 写具体操作数据库类JpaRepository接口

  • 建立Controller类,作数据展示

创建spring工程文件:

勾选需要导入的spring依赖:(7个)

Spring Boot DevTools

Lombok

Spring Web

JDBC API

Spring Data JPA

Spring Data JDBC

MySQL Driver

修改总配置文件后缀为.yml,并且完成基本数据库连接配置。

完成后正常运行

查询操作:

1、创建domain实体类文件(需要和数据库表名一致):

domain\t_user:

package com.lyd.springtestapi.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;

import javax.persistence.Entity;
import javax.persistence.Id;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Accessors(chain = true)
@Entity //注意声明为实体类
public class t_user {
    @Id //注意声明主键
    private Integer id;
    private Integer role;
    private String username;
    private String email;
    private String gender;
    private String createtime;
    private Integer flag;
    private String activatecode;

}

2、写具体操作数据库类JpaRepository接口

dao\t_userRepository:

package com.lyd.springtestapi.dao;

import com.lyd.springtestapi.domain.t_user;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 增删改查操作示例
 */
@Transactional//加上事物声明 @Transactional(readOnly = true)
public interface t_userRepository extends JpaRepository<t_user, Integer> {
    @Query(value = "SELECT * FROM t_user WHERE id=? and role=?;", nativeQuery = true)
    List<t_user> getAllUser(Integer id, Integer role);

    //    @Transactional//加上事物声明
    @Modifying(clearAutomatically = true)//自动清除实体里保存的数据(更新缓存)。
    @Query(value = "update t_user set username = ? where id = ?;", nativeQuery = true)
    int setUserName(String username, Integer id);


    @Modifying
    @Query(value = "delete from t_user  where id = ?", nativeQuery = true)
    int deleteUser(Integer id);

    @Modifying
    @Query(value = "INSERT INTO t_user VALUES (?, ?, ?, '123456' ,?, ?, ?, ?, ?);", nativeQuery = true)
    int insertUser(Integer id, Integer role, String username, String email, String gender, String createtime, Integer flag, String activatecode);
}

dao\ResultVO:

package com.lyd.springtestapi.domain;

import lombok.Data;

@Data
public class ResultVO<T> {
    private Integer code;
    private String msg;
    private T data;
}

3、创建Controller类,作数据展示

controller\t_userController:

package com.lyd.springtestapi.controller;

import com.lyd.springtestapi.dao.t_userRepository;
import com.lyd.springtestapi.domain.ResultVO;
import com.lyd.springtestapi.utils.ResultUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController //声明控制类
public class t_userController {
    @Resource
    private t_userRepository t_userRepository;


    @GetMapping("/getUser")
    public ResultVO getUser() {
        return ResultUtils.success(t_userRepository.findAll());
    }

    @PostMapping("/getUser1") //声明不同请求类型
    public ResultVO getUser1() {
        return ResultUtils.success(t_userRepository.findAll());
    }

    @GetMapping("/getAllUser")
    public ResultVO getAllUser(Integer id, Integer role) {
        return ResultUtils.success(t_userRepository.getAllUser(id, role));
    }
    /*@RequestMapping("/getAllUser")
    public List<t_user> getAllUser(Integer id ,Integer role) {
        return t_userRepository.getAllUser(id,role);
    }*/

    @PostMapping("/setUserName")
    public ResultVO setUserName(String username, Integer id) {
        return ResultUtils.success(t_userRepository.setUserName(username, id));
    }
}

utils\ResultUtils:

package com.lyd.springtestapi.utils;

import com.lyd.springtestapi.domain.ResultVO;

public class ResultUtils {
    public static ResultVO success(Object object) {
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(200);
        resultVO.setMsg("成功");
        resultVO.setData(object);
        return resultVO;
    }
}

标准输出格式:

{
    "code": 200,
    "msg": "成功",
    "data": [
        {
            "id": 14,
            "role": 1,
            "username": "lili",
            "email": "1365662837@qq.com",
            "gender": "男",
            "createtime": "2021-06-23 12:11:55",
            "flag": 2,
            "activatecode": "20210623121155770216"
        }
    ]
}
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐