spring JPA整合hibernate,IDEA社区版,Java
本文基于IDEA社区版,不是IDEA企业版。
(1)首先用IDEA新建一个spring web项目。参考文章:
IDEA社区版(Community Edition)创建Springboot-Web项目,Java_zhangphil的博客-CSDN博客除了可以通过application.yml文件配置MyApp外,也可以通过application.properties配置,application.properties与application.yml配置写法不同。特别注意,如果resources下面同时存在application.yml和application.properties两个配置文件,那么application.properties里面的配置将覆盖application.yml的配置。(3)在main目录下建立项目的代码包(目录层次结构)https://zhangphil.blog.csdn.net/article/details/126894920
(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数据库环境配置和安装启动,Windows_zhangphil的博客-CSDN博客先下载mysql的zip压缩包:MySQL :: Download MySQL Community Serverhttps://dev.mysql.com/downloads/mysql/下载解压后,配置my.ini文件,放到mysql的程序目录里面,my.ini文件:[mysqld]port=3306basedir=D:\mysql\mysql-8.0.27-winx64datadir=D:\mysql\data[client] port=3306default-character-shttps://blog.csdn.net/zhangphil/article/details/122414377启动mysql后,创建一个名为 db_demo 的数据库(对应(3)中的数据库链接url里面数据库名字)
把账号root的密码改成(3)中的相同,本例是123456。参考:
mysql数据库重置root用户名和密码_zhangphil的博客-CSDN博客先下载mysql的zip压缩包:MySQL :: Download MySQL Community Serverhttps://dev.mysql.com/downloads/mysql/下载解压后,配置my.ini文件,放到mysql的程序目录里面,my.ini文件:[mysqld]port=3306basedir=D:\mysql\mysql-8.0.27-winx64datadir=D:\mysql\data[client] port=3306default-character-s。https://blog.csdn.net/zhangphil/article/details/126688161
命令:
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) {
this.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写入成功。