11-SpringBoot之数据库(二)——JPA

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huangjun0210/article/details/84140991

1. 添加pom依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

2. JPA配置

在application.yml文件中spring节点下配置jpa,如下:

 #JPA配置
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

3. 实例开发

3.1 创建实体类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.Date;

/**
 * Created by HuangJun
 * 14:01 2018/11/12
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="jpa_user")
public class JpaUser {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(nullable = false)
    private String username;
    @Column(nullable = false)
    private Date birthday;
    @Column(nullable = false)
    private String sex;
    @Column(nullable = false)
    private String address;

    public JpaUser(String username, Date birthday, String sex, String address) {
        this.username = username;
        this.birthday = birthday;
        this.sex = sex;
        this.address = address;
    }
}

3.2 DAO

import com.springboot.web.model.JpaUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by HuangJun
 * 14:06 2018/11/12
 */
@Repository
public interface JpaUserRepository  extends JpaRepository<JpaUser, Integer> {
    List<JpaUser> findByUsername(String username);
}

3.3 Service

service接口:

import com.springboot.web.model.JpaUser;

import java.util.List;

/**
 * Created by HuangJun
 * 14:11 2018/11/12
 */
public interface JpaUserService {
    List<JpaUser> findAll();
    List<JpaUser> findByUsername(String username);
    JpaUser save(JpaUser jpaUser);
    void deleteById(Integer id);
}

service实现:

import com.springboot.web.dao.JpaUserRepository;
import com.springboot.web.model.JpaUser;
import com.springboot.web.service.JpaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by HuangJun
 * 14:11 2018/11/12
 */
@Service
public class JpaUserServiceImpl implements JpaUserService {

    @Autowired
    private JpaUserRepository jpaUserRepository;

    @Override
    public List<JpaUser> findAll() {
        return jpaUserRepository.findAll();
    }

    @Override
    public JpaUser save(JpaUser jpaUser) {
        return jpaUserRepository.save(jpaUser);
    }

    @Override
    public void deleteById(Integer id) {
        jpaUserRepository.deleteById(id);
    }

    @Override
    public List<JpaUser> findByUsername(String username) {
        return jpaUserRepository.findByUsername(username);
    }
}

3.4 Controller

import com.springboot.web.model.JpaUser;
import com.springboot.web.service.JpaUserService;
import com.springboot.web.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by HuangJun
 * 14:23 2018/11/12
 */
@RestController
@RequestMapping("/jpaUser")
public class JpaUserController {
    @Autowired
    private JpaUserService jpaUserService;

    @GetMapping("/list")
    public List<JpaUser> findAll() {
        return jpaUserService.findAll();
    }

    @GetMapping("/findByUsername")
    public List<JpaUser> findByUsername(@RequestParam("username") String username) {
        List<JpaUser> list = jpaUserService.findByUsername(username);
        return list;
    }

    @PostMapping("/add")
    public void add(@RequestParam("username") String username,@RequestParam("birthday") String birthday,
                    @RequestParam("sex") String sex,@RequestParam("address") String address) {

        JpaUser user = new JpaUser(username, DateFormatUtil.String2Date(birthday),sex,address);
        jpaUserService.save(user);
    }

    @DeleteMapping("/deleteById/{id}")
    public void deleteUserByUserId(@PathVariable("id")  Integer id){
        jpaUserService.deleteById(id);
    }

}

3.5 测试

使用postman进行测试

测试add():
在这里插入图片描述

测试list():在这里插入图片描述

测试findByUsername():在这里插入图片描述

4. 源码下载

源码下载地址:https://download.csdn.net/download/huangjun0210/10789263

猜你喜欢

转载自blog.csdn.net/huangjun0210/article/details/84140991
今日推荐