SpringBoot入门(三)--数据库操作&&Spring-data-jpa的使用

一、添加依赖

项目代码:https://github.com/ffzhihua/springbootdemo 

数据库框架主要使用了sping-data-jpa

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

    <!--mysql驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

二、springboot的配置

1、application.yml文件的配置

spring:
  profiles:
    active: dev

  #数据库驱动等一些信息
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springboot
    username: root
    password: root

  #spring-data-jpa的配置
  jpa:
    hibernate:
      #设置自动创建数据库
      #create:先删除,再创建表,以前表的数据清除
      #update:不删除以前的数据
      #create-drop:当应用停下来的时候删除
      #none:默认什么也不做
      #validate:先验证实体类和表结构是否一致,不一致就会报错
      ddl-auto: create
    #设置显示执行的sql语句
    show-sql: true

2、设置实体类

User.java

package cn.buildworld.springbootdemo;

import org.springframework.web.bind.annotation.PathVariable;

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

/**
 * @Description:用户类
 * @Author: zhihua
 * @Date: 2019/1/9
 */
@Entity
public class User {

    @Id
    @GeneratedValue
    private Long id;
    private String username;
    private int userage;


    public Long getId(){
        return id;
    }

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

    public String getUsername(){
        return username;
    }

    public void setUsername(String username){
        this.username = username;
    }

    public int getUserage(){
        return userage;
    }

    public void setUserage(int userage){
        this.userage = userage;
    }


}

3、设置dao层接口

UserRespository.java

package cn.buildworld.springbootdemo;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

/**
 * @Description:dao层接口
 * @Author: zhihua
 * @Date: 2019/1/9
 */
public interface UserRespository extends JpaRepository<User,Long> {
    /**
     *
     * @param age
     * @return
     */
    public List<User> findByUserage(Integer age);
}

4、web层的控制器实现我们所需的功能(增删改查)

UserController.java

package cn.buildworld.springbootdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:
 * @Author: zhihua
 * @Date: 2019/1/9
 */
@RestController
public class UserController {

    @Autowired
    private UserRespository userRespository;

    @GetMapping("getInfo")
    public List<User> getInfo() {

        List<User> all = userRespository.findAll();
        return all;
    }

    /**
     * POST方式
     *
     * @return
     */
    @PostMapping(value = "getInfo")
    public String getInfoByPost() {
        return "请使用get方式!";
    }

    @PostMapping(value = "adduser")
    public User addUser(@ModelAttribute User user) {
        userRespository.save(user);
        return user;
    }

    @GetMapping(value = "user/{id}")
    public User checkUser(@PathVariable("id") Long id) {
        return userRespository.findById(id).orElse(null);
    }

    @PutMapping("user/{id}")
    public User updateUser(@PathVariable("id") Long id, User user) {
        user.setId(id);
        userRespository.save(user);
        return userRespository.findById(id).orElse(null);

    }

    /**
     * DELETE方式删除一个用户
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "user/{id}")
    public List<User> deleteUser(@PathVariable("id") Long id) {
        userRespository.deleteById(id);
        return userRespository.findAll();
    }
    /**
     * 通过年龄来查询用户信息
     * @param age
     * @return
     */
    @GetMapping(value = "user/age/{age}")
    public List<User> findUserByAge(@PathVariable("age")int age){
        return userRespository.findByUserage(age);
    }


}

5、事务

UserService.java

package cn.buildworld.springbootdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description:
 * @Author: zhihua
 * @Date: 2019/1/10
 */
@Service
public class UserService {
    @Autowired
    private UserRespository userRespository;

    @Transactional(rollbackFor = Exception.class)
    public void insertTwo(){


    }
}

猜你喜欢

转载自blog.csdn.net/ffzhihua/article/details/86233851