Spring Boot 系统之四:Spring Boot 整合JPA

上一篇我们讲了Spring Boot 整合jbdcTemplate 来进行数据的持久化。

这篇我们来说下怎么通过Spring Boot 整合JPA来实现数据的持久化。

一、代码实现

1、修改pom.xml,引入依赖。

        <!-- 引入jpa 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

2、修改application.properties,配置相关信息。

# 配置JPA
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent_output=true

3、创建实体类。

可以讲上一篇中的User类直接进行修改;

package com.learn.spring.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

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

    @Id @GeneratedValue(strategy= GenerationType.AUTO)
    private Integer id;
    private String userName;
    private String password;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

4、创建repository接口并继承CrudRepository。

package com.learn.spring.repository;

import com.learn.spring.entity.User;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

/**
 * 注意:
 * 1.这里这里是interface,不是class
 *
 * 2.CrudRepository里面的泛型,第一个是实体类,第二个是主键的类型
 *
 * 3.由于crudRepository 里面已经有一些接口了,如deleteAll,findOne等, 我们直接调用即可
 *
 * 4.当然,我们也可以根据自己的情况来实现自己的接口,如下面的getUser()方法,jpql语句和hql语句差不多
 *
 * */
public interface UserRepository extends CrudRepository<User, Integer> {

    /**
     * 我们这里只需要写接口,不需要写实现,spring boot会帮忙自动实现
     *
     * */

    @Query("from User where id =:id ")
    public User getUser(@Param("id") Integer id);
}

5、创建Service。

(1)接口

package com.learn.spring.service;

import com.learn.spring.entity.User;


public interface UserService {
    public User getUser(Integer id);
}

(2)实现

package com.learn.spring.service.impl;

import com.learn.spring.entity.User;
import com.learn.spring.repository.UserRepository;
import com.learn.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserRepository repository;
    
    @Override
    public User getUser(Integer id) {
        //有两种方式:
        //1.调用crudRepository的接口
//        return repository.findOne(id);
        //2.调用我们自己写的接口
        return repository.getUser(id);
    }

    
}

6、创建controller。

package com.learn.spring.controller;

import com.learn.spring.entity.User;
import com.learn.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    UserService service;
    
    @RequestMapping("/getUser/{id}")
    public User getUser(@PathVariable("id") Integer id){
        
        return service.getUser(id);
    }
}

7、运行,页面以json格式显示数据库值。

 二、总结

关于Repository的知识点,可以参考的文章:https://segmentfault.com/a/1190000012346333

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值