SpringBoot-快速搭建一套JPA
文章目录
- 结构
- Maven
- application.yml
- 实体类
- dao
- service
- controller
- 测试
结构
Maven
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-jpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-jpa</name>
<description>springboot-jpa</description>
<properties>
<java.version>1.8</java.version>
<!-- 文件拷贝时的编码-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 编译时的编码 这里就是你运行项目,会给你的文件进行编码-->
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
使用IDEA的可以安装下面的插件这样在使用JPA进行编码的时候非常舒服,各种提示
application.yml
server:
port: 9878
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver #驱动
url: jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
type: com.alibaba.druid.pool.DruidDataSource
username: root
password: root
jpa:
hibernate:
ddl-auto: update #自动更新 ,每次运行程序,没有表格会新建表格,表内有数据不会清空,只会更新
show-sql: true #日志中显示sql语句 ,生产环境不要开启
properties:
hibernate:
enable_lazy_load_no_trans: true #懒加载
实体类
注意: 实体类中的字段类型 必须是包装类型,否则会导致查询出现默认值的现象
package com.example.springbootjpa.entity;
import lombok.*;
import org.hibernate.Hibernate;
import javax.persistence.*;
import java.util.Date;
import java.util.Objects;
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Entity // 标识这是一个实体类,并且使用默认的orm规则,即class名即数据库表中表名,class字段名即表中的字段名
@Table(name = "t_user") //来改变class名与数据库中表名的映射规则
public class UserEntity {
@Id
// GenerationType.IDENTITY使用数据库的自增
// 如果是oracle则需要使用GenerationType.SEQUENCE配合@SequenceGenerator
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@Column
private String name;
@Column
private String email;
@Column
private String phone;
@Column
private String password;
@Column
private Integer age;
@Column
private Integer sex;
@Column
private Integer status;
@Temporal(TemporalType.TIMESTAMP)
@Column
private Date creationTime;
@Column
private String site;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
UserEntity that = (UserEntity) o;
return id != null && Objects.equals(id, that.id);
}
@Override
public int hashCode() {
return getClass().hashCode();
}
}
一般来说,我们都是先设计数据库然后在设计实体类, 如果你是使用的是jpa就可以先设计实体类,而对应的表在启动项目的时候就会自动创建了
dao
public interface UserDao extends JpaRepository<UserEntity, Long> {
}
一般来说继承JpaRepository即可 ,内部包括常规的增删改查
service
package com.example.springbootjpa.service;
import com.example.springbootjpa.entity.UserEntity;
import java.util.List;
public interface UserService {
//查询全部用户
List<UserEntity> findAll();
}
package com.example.springbootjpa.service.impl;
import com.example.springbootjpa.dao.UserDao;
import com.example.springbootjpa.entity.UserEntity;
import com.example.springbootjpa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<UserEntity> findAll() {
return userDao.findAll();
}
}
controller
package com.example.springbootjpa.controller;
import com.example.springbootjpa.entity.UserEntity;
import com.example.springbootjpa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findAll")
public ResponseEntity<List<UserEntity>> findAll(){
return ResponseEntity.ok(userService.findAll());
}
}
测试
package com.example.springbootjpa;
import com.example.springbootjpa.controller.UserController;
import com.example.springbootjpa.dao.UserDao;
import com.example.springbootjpa.entity.UserEntity;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import java.util.List;
@SpringBootTest
class SpringbootJpaApplicationTests {
@Autowired
private UserController userController;
@Test
void contextLoads() {
ResponseEntity<List<UserEntity>> all = userController.findAll();
List<UserEntity> body = all.getBody();
System.out.println(body);
}
}
点赞 -收藏-关注-便于以后复习和收到最新内容有其他问题在评论区讨论-或者私信我-收到会在第一时间回复在本博客学习的技术不得以任何方式直接或者间接的从事违反中华人民共和国法律,内容仅供学习、交流与参考 免责声明:本文部分素材来源于网络,版权归原创者所有,如存在文章/图片/音视频等使用不当的情况,请随时私信联系我、以迅速采取适当措施,避免给双方造成不必要的经济损失。感谢,配合,希望我的努力对你有帮助^_^
还没有评论,来说两句吧...