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

上一篇: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

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

添加查找和删除功能:
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";
}

执行查找结果:
SpringBoot如何整合JPA-Hibernate? | 带你读《SpringBoot实战教程》之二十四
执行删除结果:
SpringBoot如何整合JPA-Hibernate? | 带你读《SpringBoot实战教程》之二十四
SpringBoot如何整合JPA-Hibernate? | 带你读《SpringBoot实战教程》之二十四

配套视频

上一篇:SpringBoot2系列教程(一)简介及HelloWorld项目


下一篇:PHP框架Swoole的一个定时器Timer特性