Springboot03整合SpringDataJPA访问MySQL数据库

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

使用SpringBoot访问MySQL数据库,并且结合SpringDataJPA完成CRUD(Create,Read,Update,Delete

结合 Springboot01 中的demo案例 Springboot01创建第一个程序

1 添加 pom.xml 依赖

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

2  配制数据源 


spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring-test
spring.datasource.username=root
spring.datasource.password=123456
#com.mysql.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##是hibernate的配置属性,其主要作用是:自动创建、更新、验证数据库表结构
#create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。
#create-drop:每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。
#update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等应用第一次运行起来后才会。
#validate:每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。
spring.jpa.properties.hibernate.hbm2ddl.auto=update

3 创建 UserController 


import com.springboot.demo.pojo.UserModel;
import com.springboot.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/users")
public class UserController {


    @Autowired
    private UserRepository userRepository;


    /**
     * 查询所有的用户
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<UserModel> getAllUserList(){
        return userRepository.findAll();
    }

    /**
     * 添加新用户
     * 更新 这里主键为 id 如果数据参数中有对应的id 则为更新
     * @param userModel
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public UserModel addUser(UserModel userModel){
        return userRepository.save(userModel);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public  List<UserModel>  deleteUser(Long id){
         userRepository.deleteById(id);
        return userRepository.findAll();
    }
}

4 创建 对应的 JPA

import com.springboot.demo.pojo.UserModel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import java.io.Serializable;


/**
 * JpaRepository接口(SpringDataJPA提供的简单数据操作接口)
 * JpaSpecificationExecutor(SpringDataJPA提供的复杂查询接口)
 * Serializable(序列化接口)。
 */
public interface UserRepository extends JpaRepository<UserModel,Long> , JpaSpecificationExecutor<UserModel>, Serializable {
}

5 对应的映射实体 UserModel 



import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

@Entity
@Table(name = "t_user")
public class UserModel implements Serializable {

    @Id
    @GeneratedValue
    @Column(name = "u_id")
    private Long id;

    @Column(name = "u_name")
    @NotNull(message = "姓名不可为空")
    private String name;


    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

6 启动程序测试 

测试添加 

测试查询 

猜你喜欢

转载自blog.csdn.net/zl18603543572/article/details/85962256