SpringBoot2 - CRUD

构建项目

访问start.spring.io/

SpringBoot2 - CRUD_第1张图片

项目目录结构

SpringBoot2 - CRUD_第2张图片

导入开发工具

SpringBoot2 - CRUD_第3张图片
SpringBoot2 - CRUD_第4张图片

配置数据源以及JPA

  1. 新建application.yml配置文件


    SpringBoot2 - CRUD_第5张图片

配置文件中不要使用TAB键

spring:
 datasource:
  url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8
  driverClassName: com.mysql.jdbc.Driver
  username: root
  password: 123456
 jpa:
  database: MySQL
  show-sql: true
  hibernate:
   nameing_strategy: org.hibernate.cfg.ImprovedNamingStrategy
  1. 创建用户表
DROP TABLE IF EXISTS `t_user`;

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` varchar(50) DEFAULT NULL COMMENT '名字',
  `age` int DEFAULT '0' COMMENT '年龄',
  `address` varchar(200) DEFAULT NULL COMMENT '地址',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

创建实体

SpringBoot2 - CRUD_第6张图片
package com.gala.jpa.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 {

    private Long id;

    private String name;

    private Integer age;

    private String address;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    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;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}

创建JPA

SpringBoot2 - CRUD_第7张图片
package com.gala.jpa.repository.jpa;

import java.io.Serializable;

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

import com.gala.jpa.entity.User;

public interface UserDao extends JpaRepository, JpaSpecificationExecutor, Serializable {

}

创建控制器

SpringBoot2 - CRUD_第8张图片
package com.gala.jpa.controller;

import java.util.List;

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 com.gala.jpa.entity.User;
import com.gala.jpa.repository.jpa.UserDao;

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

    @Autowired
    private UserDao userDao;

    /**
     * 查询
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List list() {
        return userDao.findAll();
    }

    /**
     * 新增或修改
     */
    @RequestMapping(value = "/save", method = RequestMethod.GET)
    public User save(User User) {
        return userDao.save(User);
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public List delete(Long id) {
        userDao.deleteById(id);
        return userDao.findAll();
    }

}

访问项目

启动项目

  1. 添加更新用户,请求地址:http://127.0.0.1:8080/user/save?name=admin&age=30&address=beijing

  2. 查询用户,请求地址:http://127.0.0.1:8080/user/list

    SpringBoot2 - CRUD_第9张图片

SpringBoot2 - CRUD_第10张图片
  1. 删除用户,请求地址:http://127.0.0.1:8080/user/delete?id=1

    SpringBoot2 - CRUD_第11张图片

你可能感兴趣的:(SpringBoot2 - CRUD)