首页 > 编程语言 > 详细

04 Spring Data JPA快速入门

时间:2020-09-17 23:37:31      阅读:74      评论:0      收藏:0      [点我收藏+]

Spring Data JPA快速入门

环境要求

名称 版本
JDK 1.8+
Maven 3.0+
Intellij IDEA

搭建数据库

创建一个数据的新用户并附上权限

mysql> craete database springdatajpa;
mysql> create user ‘springuser‘@‘localhost‘ identified by ‘123456‘;
mysql> grant all on db_example.* to ‘root‘@‘localhost‘;

创建user表

create table ‘user‘{
	‘id‘ int(11) not null auto_increment,
	‘name‘ varchar(50) default null,
	‘email‘ varchar(200) default all,
	primary key (‘id‘)

}

使用Intellij IDEA创建项目

1、创建项目

技术分享图片 技术分享图片 技术分享图片

2、修改application.properties文件。

建议将application.properties修改为application.yaml文件

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/springdatajpa
    username: root
    password: 123456

3、创建一个@Entity

package com.example.springdatajpa01;

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

/**
 * TODO
 *
 * @author: Yizq
 * @data: 2020/8/23 4:38 下午
 */
@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    private String email;

    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 String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

4、创建一个Repository

package com.example.springdatajpa01;

import org.springframework.data.repository.CrudRepository;

/**
 * TODO
 *
 * @author: Yizq
 * @data: 2020/8/23 4:40 下午
 */

public interface UserRepository extends CrudRepository<User,Long> {

}

5、创建一个Controller

package com.example.springdatajpa01;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * TODO
 *
 * @author: Yizq
 * @data: 2020/8/23 4:44 下午
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping(path = "/add")
    public void addNewUser(@RequestParam String name,@RequestParam String email) {
        User user = new User();
        user.setName(name);
        user.setEmail(email);
        userRepository.save(user);
    }


    @GetMapping(path = "/all")
    @ResponseBody
    public Iterable<User> getAllUsers() {
        return userRepository.findAll();
    }

}

5、运行springboot启动类

package com.example.springdatajpa01;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Springdatajpa01Application {

	public static void main(String[] args) {
		SpringApplication.run(Springdatajpa01Application.class, args);
	}

}


6、发送请求

http://localhost:8080/user/add?name=zhangsan&email=johnson1008@163.com
http://localhost:8080/user/all

返回的结果
[
    {
        "id":1,
        "name":"zhangsan",
        "email":"johnson1008@163.com"
    }
]

04 Spring Data JPA快速入门

原文:https://www.cnblogs.com/xianbeier/p/13688396.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!