【Demo系列】Spring Data JPA

项目准备

数据库脚本

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (
  `id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '主键id',
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名',
  `age` int(0) NULL DEFAULT NULL COMMENT '年龄',
  `gender` tinyint(1) NULL DEFAULT NULL COMMENT '性别',
  `create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('a90bcd22f811a', '张三', 18, 0, '2022-11-24 16:14:34');
INSERT INTO `student` VALUES ('a90bcd22f811b', '李四', 23, 1, '2022-11-24 16:53:02');

SET FOREIGN_KEY_CHECKS = 1;

代码

1.创建maven项目
2.引入依赖

    
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
        
            mysql
            mysql-connector-java
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

3.配置文件

# 数据源配置
spring.datasource.url = jdbc:mysql://localhost:3306/jpa_study?serverTimezone=Asia/Shanghai
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
# jpa设置-显示SQL语句
spring.jpa.show-sql = true
# jpa设置-表内有数据时不会清空, 只会更新
spring.jpa.hibernate.ddl-auto=update

实体类:

import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

@Data
@Entity
@Table(name = "student")
public class Student {
    @Id
    @Column
    private String id;

    @Column
    private String name;

    @Column
    private int age;

    @Column
    private boolean gender;

    @Column
    private Date createTime;
}

Dao层:

import com.example.jpa.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

@Repository
public interface StudentDao extends JpaRepository, JpaSpecificationExecutor {
}

Service层:

import com.example.jpa.dao.StudentDao;
import com.example.jpa.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class StudentService {
    @Autowired
    private StudentDao studentDao;

    public List findList(){
        return studentDao.findAll();
    }

}

Controller层:

import com.example.jpa.entity.Student;
import com.example.jpa.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping("/findList")
    public List findList(){
        return studentService.findList();
    }
}

你可能感兴趣的:(Demo系列,#,JPA,java,mysql,数据库,jpa)