spring JPA整合hibernate,IDEA社区版,Java
本文基于IDEA社区版,不是IDEA企业版。
(1)首先用IDEA新建一个spring web项目。
(2)pom.xml添加引用:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
(3)在resources下面的application.properties里面写入配置,如果application.properties不存在,新建application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/db_demo?useSSL=false&allowPublicKeyRetrieval=true&allowMultiQueries=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.generate_statistics=false
(4)在本机上启动mysql数据库。启动端口在3306。
命令:
mysql -uroot -p123456
登录mysql数据库,创建db_demo:
create database db_demo;
show databases;
(5)在IDEA开发工具的左下角打开JPA Structure,新建一个JPA的实体User
再新建一个Spring Data Repository,名字可以取为UserRepository。
此时的main/java/test代码包里面有User.java和UserRepository.java,
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
}
在IDEA自动生成的代码基础上补全User.java,根据自己的需要添加字段(name和age是自己添加的):
import javax.persistence.*;
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false)
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
private String name;
public int age;
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
新建一个UserController,这个类主要为了测试往数据库写数据(不重要,和JPA无关,仅作数据添加):
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping(path = "/add")
public User addUser() {
User user = new User();
user.setName("zhang");
user.setAge(18);
User save = userRepository.save(user);
return save;
}
}
启动的main application类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Main.class, args);
}
}
(6)最终的项目代码结构为:
(7)以上完成后,启动Main.java,日志输出部分内容为:
spring jpa自动连接Mysql数据库创建user表结构。此时在mysql执行命令查看JPA对mysql的写的表:
use db_demo;
show tables;
describe user;
说明JPA正常工作了。
(8)此时在浏览器访问 localhost:8080/add
在mysql控制台查看JPA是否真正写入了mysql数据:
JPA写入成功。