一个简单实例初识Spring Data JPA

本着同步理解.NET和Spring框架核心理念的原则,来到ORM框架这个话题。.NET的ORM框架可谓繁荣,有轻量级的Dapper,有大名鼎鼎全能无敌的EFCore;在Spring中,比较流行的ORM框架包括MyBatis和Hibernate,前者与Dapper接近,后者是Spring Data JPA标准的一个流行实现,功能强大与EFCore能有一战。

要想深入掌握Hibernate,需要同平时项目结合起来一起学习,本文以一个非常简单的实例一窥Hibernate的精妙!

Maven依赖文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>annotationProcessor</scope>
</dependency>

项目配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.153.128:3306/db01?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8&allowMultiQueries=true
spring.datasource.username=admin
spring.datasource.password=lizi710

logging.level.org.hibernate.orm.jdbc.bind="TRACE"

spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

spring.jpa.hibernate.ddl-auto=none

实体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.example.demojpa.entity;

import java.time.LocalDateTime;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;

@Entity
@Table(name = "sys_user")
@Data
public class SysUser {
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column
private String account;

@Column
private Boolean enabled;

@Column
private LocalDateTime createAt;
}

数据访问

1
2
3
4
5
6
7
8
9
10
11
package com.example.demojpa.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

import com.example.demojpa.entity.SysUser;

@Repository
public interface SysUserRepository extends JpaRepository<SysUser, Long>,JpaSpecificationExecutor<SysUser> {
}

控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.example.demojpa.controller;

import java.time.LocalDateTime;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import com.example.demojpa.entity.SysUser;
import com.example.demojpa.repository.SysUserRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;


@RestController
@RequestMapping("/test")
public class TestController {
static final Logger logger = LoggerFactory.getLogger(TestController.class);

@Autowired
private SysUserRepository sysUserRepository;

@GetMapping("/show")
public String show() {
SysUser sysUser = new SysUser();
sysUser.setAccount("springjpa");
sysUser.setEnabled(Boolean.TRUE);
sysUser.setCreateAt((LocalDateTime.now()));

this.sysUserRepository.saveAndFlush(sysUser);

Optional<SysUser> user = this.sysUserRepository.findById(sysUser.getId());
logger.info("user={}", user.get());
return "finished----------------";
}
}

主程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.example.demojpa;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories(basePackages = "com.example.demojpa.repository")
@EntityScan(basePackages = "com.example.demojpa.entity")
public class DemojpaApplication {
public static void main(String[] args) {
SpringApplication.run(DemojpaApplication.class, args);
}
}

评论