Hibernate是一个开源的对象关系映射(ORM)框架,国外使用比较多,国内主要使用的mybatis。
JPA,Spring Data JPA和Hibernate
JPA只是一个ORM框架的规范, 对该规范的实现比较完整就是Spring Data JPA(底层基于Hibernate实现),是基于Spring的数据持久层框架,也就是说它只能用在Spring环境内。
添加依赖
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.4</version></dependency>
</dependencies>
配置数据库
在application.properties文件中添加数据库相关的配置
# 数据库相关配置
spring.datasource.url=jdbc:mysql://localhost:3306/hibernate_demo
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver# Hibernate配置
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
tails/132394884
创建实体类
import lombok.Getter;
import lombok.Setter;import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;@Entity
@Table(name = "user")
@Setter
@Getter
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Integer id;private String name;private Integer age;public User(String name, Integer age) {this.name = name;this.age = age;}
}
import lombok.Getter;
import lombok.Setter;import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;@Setter
@Getter
public class UserRequestDto implements Serializable {@NotNull@Size(min = 1)private String name;private Integer age;public User toUser() {return new User(name, age);}
}
创建Repository接口
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
}
编写业务逻辑
import org.example.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class UserService {@Autowiredprivate UserRepository userRepository;public void save(User user) {userRepository.save(user);}public User getById(Integer id) {return userRepository.findById(id).orElse(null);}
}
创建Controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/users")
public class UserContoller {@AutowiredUserService userService;@RequestMapping(path = "/{id}", method = RequestMethod.GET)public User getUser(@PathVariable Integer id) {return userService.getById(id);}@RequestMapping(method = RequestMethod.POST)public ResponseEntity<String> saveUser(@Validated @RequestBody UserRequestDto userRequestDto) {userService.save(userRequestDto.toUser());return ResponseEntity.ok().build();}
}
github代码:GitHub - JJJ2018/SpringDataHibernate_Mysql
参考:SpringBoot 使用Hibernate (JPA)_springboot使用hibernate-CSDN博客