SpringBoot-快速搭建一套JPA

Bertha 。 2023-10-03 22:23 76阅读 0赞

文章目录

    • 结构
    • Maven
    • application.yml
    • 实体类
    • dao
    • service
    • controller
    • 测试

结构

在这里插入图片描述

Maven

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.7.6</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. <groupId>com.example</groupId>
  8. <artifactId>springboot-jpa</artifactId>
  9. <version>0.0.1-SNAPSHOT</version>
  10. <name>springboot-jpa</name>
  11. <description>springboot-jpa</description>
  12. <properties>
  13. <java.version>1.8</java.version>
  14. <!-- 文件拷贝时的编码-->
  15. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  16. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  17. <!-- 编译时的编码 这里就是你运行项目,会给你的文件进行编码-->
  18. <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-data-jpa</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>com.mysql</groupId>
  31. <artifactId>mysql-connector-j</artifactId>
  32. <scope>runtime</scope>
  33. </dependency>
  34. <dependency>
  35. <groupId>com.alibaba</groupId>
  36. <artifactId>druid</artifactId>
  37. <version>1.2.15</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-configuration-processor</artifactId>
  42. <optional>true</optional>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.projectlombok</groupId>
  46. <artifactId>lombok</artifactId>
  47. <optional>true</optional>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-starter-test</artifactId>
  52. <scope>test</scope>
  53. </dependency>
  54. </dependencies>
  55. <build>
  56. <plugins>
  57. <plugin>
  58. <groupId>org.springframework.boot</groupId>
  59. <artifactId>spring-boot-maven-plugin</artifactId>
  60. <configuration>
  61. <excludes>
  62. <exclude>
  63. <groupId>org.projectlombok</groupId>
  64. <artifactId>lombok</artifactId>
  65. </exclude>
  66. </excludes>
  67. </configuration>
  68. </plugin>
  69. </plugins>
  70. </build>

使用IDEA的可以安装下面的插件这样在使用JPA进行编码的时候非常舒服,各种提示
在这里插入图片描述

application.yml

  1. server:
  2. port: 9878
  3. spring:
  4. datasource:
  5. driver-class-name: com.mysql.cj.jdbc.Driver #驱动
  6. url: jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
  7. type: com.alibaba.druid.pool.DruidDataSource
  8. username: root
  9. password: root
  10. jpa:
  11. hibernate:
  12. ddl-auto: update #自动更新 ,每次运行程序,没有表格会新建表格,表内有数据不会清空,只会更新
  13. show-sql: true #日志中显示sql语句 ,生产环境不要开启
  14. properties:
  15. hibernate:
  16. enable_lazy_load_no_trans: true #懒加载

实体类

注意: 实体类中的字段类型 必须是包装类型,否则会导致查询出现默认值的现象

  1. package com.example.springbootjpa.entity;
  2. import lombok.*;
  3. import org.hibernate.Hibernate;
  4. import javax.persistence.*;
  5. import java.util.Date;
  6. import java.util.Objects;
  7. @Getter
  8. @Setter
  9. @ToString
  10. @RequiredArgsConstructor
  11. @Entity // 标识这是一个实体类,并且使用默认的orm规则,即class名即数据库表中表名,class字段名即表中的字段名
  12. @Table(name = "t_user") //来改变class名与数据库中表名的映射规则
  13. public class UserEntity {
  14. @Id
  15. // GenerationType.IDENTITY使用数据库的自增
  16. // 如果是oracle则需要使用GenerationType.SEQUENCE配合@SequenceGenerator
  17. @GeneratedValue(strategy = GenerationType.IDENTITY)
  18. @Column(name = "id", nullable = false)
  19. private Long id;
  20. @Column
  21. private String name;
  22. @Column
  23. private String email;
  24. @Column
  25. private String phone;
  26. @Column
  27. private String password;
  28. @Column
  29. private Integer age;
  30. @Column
  31. private Integer sex;
  32. @Column
  33. private Integer status;
  34. @Temporal(TemporalType.TIMESTAMP)
  35. @Column
  36. private Date creationTime;
  37. @Column
  38. private String site;
  39. @Override
  40. public boolean equals(Object o) {
  41. if (this == o) return true;
  42. if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
  43. UserEntity that = (UserEntity) o;
  44. return id != null && Objects.equals(id, that.id);
  45. }
  46. @Override
  47. public int hashCode() {
  48. return getClass().hashCode();
  49. }
  50. }

一般来说,我们都是先设计数据库然后在设计实体类, 如果你是使用的是jpa就可以先设计实体类,而对应的表在启动项目的时候就会自动创建了

dao

  1. public interface UserDao extends JpaRepository<UserEntity, Long> {
  2. }

一般来说继承JpaRepository即可 ,内部包括常规的增删改查

service

  1. package com.example.springbootjpa.service;
  2. import com.example.springbootjpa.entity.UserEntity;
  3. import java.util.List;
  4. public interface UserService {
  5. //查询全部用户
  6. List<UserEntity> findAll();
  7. }
  8. package com.example.springbootjpa.service.impl;
  9. import com.example.springbootjpa.dao.UserDao;
  10. import com.example.springbootjpa.entity.UserEntity;
  11. import com.example.springbootjpa.service.UserService;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Service;
  14. import java.util.List;
  15. @Service
  16. public class UserServiceImpl implements UserService {
  17. @Autowired
  18. private UserDao userDao;
  19. @Override
  20. public List<UserEntity> findAll() {
  21. return userDao.findAll();
  22. }
  23. }

controller

  1. package com.example.springbootjpa.controller;
  2. import com.example.springbootjpa.entity.UserEntity;
  3. import com.example.springbootjpa.service.UserService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.http.ResponseEntity;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import java.util.List;
  9. @RestController
  10. @RequestMapping("/user")
  11. public class UserController {
  12. @Autowired
  13. private UserService userService;
  14. @RequestMapping("/findAll")
  15. public ResponseEntity<List<UserEntity>> findAll(){
  16. return ResponseEntity.ok(userService.findAll());
  17. }
  18. }

测试

  1. package com.example.springbootjpa;
  2. import com.example.springbootjpa.controller.UserController;
  3. import com.example.springbootjpa.dao.UserDao;
  4. import com.example.springbootjpa.entity.UserEntity;
  5. import org.junit.jupiter.api.Test;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.http.ResponseEntity;
  9. import java.util.List;
  10. @SpringBootTest
  11. class SpringbootJpaApplicationTests {
  12. @Autowired
  13. private UserController userController;
  14. @Test
  15. void contextLoads() {
  16. ResponseEntity<List<UserEntity>> all = userController.findAll();
  17. List<UserEntity> body = all.getBody();
  18. System.out.println(body);
  19. }
  20. }

在这里插入图片描述

点赞 -收藏-关注-便于以后复习和收到最新内容有其他问题在评论区讨论-或者私信我-收到会在第一时间回复在本博客学习的技术不得以任何方式直接或者间接的从事违反中华人民共和国法律,内容仅供学习、交流与参考 免责声明:本文部分素材来源于网络,版权归原创者所有,如存在文章/图片/音视频等使用不当的情况,请随时私信联系我、以迅速采取适当措施,避免给双方造成不必要的经济损失。感谢,配合,希望我的努力对你有帮助^_^

发表评论

表情:
评论列表 (有 0 条评论,76人围观)

还没有评论,来说两句吧...

相关阅读