SpringBoot如何整合JPA-Hibernate? | 带你读《SpringBoot实战教程》之二十四

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 本节通过在数据库添加用户、查找用户、删除用户的功能,来实现SpringBoot是怎样整合JPA-Hibernate的。

上一篇:SpringBoot的事务管理 | 带你读《SpringBoot实战教程》之二十三
下一篇:如何在SpringBoot中实现邮件的发送? | 带你读《SpringBoot实战教程》之二十五

本文来自于千锋教育在阿里云开发者社区学习中心上线课程《SpringBoot实战教程》,主讲人杨红艳,点击查看视频内容

SpringBoot整合JPA-Hibernate

添加依赖:

   <!-- springboot整合jpa -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>
      <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
       </dependency>

创建全局配置文件:application.properties,添加与JPA有关的信息以及连接数据库:使用db6数据库

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/db6

#让控制器输出json字符串格式
spring.jackson.serialization.indent-output=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

新建包:com.qianfeng.pojo,生成实体类

@Entity
@Table(name="t_user")
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    
    @Column
    private String name;
    private String password;
    private String email;
    private Date birthday;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
    
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
    
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
    
        this.password = password;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
    
        this.email = email;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
    
        this.birthday = birthday;
    }
}

实现添加功能
UserDao:

public interface UserDao extends JpaRepository<User, Integer>{
}

UserService:

public interface UserService {

    void addUser(User user);
}

UserServiceImpl:

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public void addUser(User user) {
        
        userDao.save(user);
    }
}

UserController:

@Controller
public class UserController {

    private UserService userService;
    
    @RequestMapping("/save")
    @ResponseBody
    public String saveUser() {
        User user = new User();
        user.setName("小花");
        user.setPassword("123");
        user.setEmail("xiaohua@163.com");
        user.setBirthday(new Date());

        userService.addUser(user);
        return "success";
    }
}

在启动类中添加所有需要扫描的包

@SpringBootApplication(scanBasePackages="com.qianfeng")
@EntityScan("com.qianfeng.pojo")     //扫描实体类
@EnableJpaRepositories("com.qianfeng.dao")      //扫描dao

执行结果:
image.png
image.png
image.png

添加查找和删除功能:
UserService:

    void findUser(Integer id);
    void delUser(Integer id);

UserServiceImpl:

@Override
public User findUser(Integer id) {
    return userDao.findOne(id);

}

public void delUser(Integer id) {

    userDao.delete(id);
}

UserController:

@RequestMapping("/find/{id}")
@ResponseBody
public User findUser(@PathVariable Integer id) {

    return userService.findUser(id);
}

@RequestMapping("/del/{id}")
@ResponseBody
public String delUser(@PathVariable Integer id) {

    userService.delUser(id);
    return "ok";
}

执行查找结果:
image.png
执行删除结果:
image.png
image.png

配套视频

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
XML Java 数据库连接
spring boot 参数的过滤注解与实战
在Spring Boot应用中,对于入参的过滤,通常会涉及到对Web层的数据验证和处理。Spring Boot借助Spring框架提供了强大的验证框架支持,主要基于JSR-303/JSR-380(Bean Validation API)规范,以及Spring自身的@Valid或@Validated注解来实现请求参数的验证。以下是一些常见的使用案例来展示如何对参数进行过滤和验证。
29 1
|
1月前
|
Java Spring 容器
【二十二】springboot整合拦截器实战并对比过滤器
【二十二】springboot整合拦截器实战并对比过滤器
33 0
|
2月前
|
Dubbo Java 应用服务中间件
实战指南:如何在Spring Boot中无缝整合Dubbo【四】
实战指南:如何在Spring Boot中无缝整合Dubbo【四】
45 0
|
2月前
|
存储 Java Maven
QR码应用实战:Spring Boot与ZXing完美结合
QR码应用实战:Spring Boot与ZXing完美结合
30 0
|
1月前
|
人工智能 JSON 前端开发
【Spring boot实战】Springboot+对话ai模型整体框架+高并发线程机制处理优化+提示词工程效果展示(按照框架自己修改可对接市面上百分之99的模型)
【Spring boot实战】Springboot+对话ai模型整体框架+高并发线程机制处理优化+提示词工程效果展示(按照框架自己修改可对接市面上百分之99的模型)
|
2月前
|
数据采集 存储 缓存
SpringBoot与布隆过滤器的完美邂逅:高效防护大规模数据的奇妙结合【实战】
SpringBoot与布隆过滤器的完美邂逅:高效防护大规模数据的奇妙结合【实战】
100 0
|
2月前
|
消息中间件 存储 监控
搭建消息时光机:深入探究RabbitMQ_recent_history_exchange在Spring Boot中的应用【RabbitMQ实战 二】
搭建消息时光机:深入探究RabbitMQ_recent_history_exchange在Spring Boot中的应用【RabbitMQ实战 二】
34 1
|
3月前
|
存储 NoSQL Redis
Redis+SpringBoot企业版集群实战------【华为云版】(上)
Redis+SpringBoot企业版集群实战------【华为云版】
64 0
|
2月前
|
消息中间件 NoSQL Java
Redis Streams在Spring Boot中的应用:构建可靠的消息队列解决方案【redis实战 二】
Redis Streams在Spring Boot中的应用:构建可靠的消息队列解决方案【redis实战 二】
222 1
|
2月前
|
监控 IDE Java
Java项目调试实战:如何高效调试Spring Boot项目中的GET请求,并通过equalsIgnoreCase()解决大小写不一致问题
Java项目调试实战:如何高效调试Spring Boot项目中的GET请求,并通过equalsIgnoreCase()解决大小写不一致问题
44 0