Java9中使用jpa_第三篇:SpringBoot中使用JPA

上一篇使用了JdbcTemplate去访问数据库,毕竟使用的是原生的SQL形式,像我这种懒人是肯定不会考虑的了。。

这次记录下使用JPA来极大的减少我们的代码量

首先,还是准备好SQL文件

DROP TABLE IF EXISTS users;

CREATE TABLE users (

id INT ( 11 ) PRIMARY KEY AUTO_INCREMENT,

username VARCHAR ( 255 ) NOT NULL,

passwd VARCHAR ( 255 )

) ENGINE = INNODB DEFAULT CHARSET = utf8;

INSERT users VALUES ( NULL, '翠花', '123' );

INSERT users VALUES ( NULL, '王卫国', '123' );

INSERT users VALUES ( NULL, '李小花', '123' );

INSERT users VALUES ( NULL, '王二柱', '123' );

INSERT users VALUES ( NULL, '赵铁蛋', '123' );

这次需要用到的依赖

org.springframework.boot

spring-boot-starter-data-jpa

org.springframework.boot

spring-boot-starter-web

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-starter-test

test

User.java

package com.priv.gabriel.entity;

import javax.persistence.*;

/**

* Created with Intellij IDEA.

*

* @Author: Gabriel

* @Date: 2018-10-08

* @Desciption:

*/

@Entity

@Table(name = "users")

public class User {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private long id;

@Column(nullable = false)

private String username;

@Column(nullable = false)

private String passwd;

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 String getPasswd() {

return passwd;

}

public void setPasswd(String passwd) {

this.passwd = passwd;

}

}

UserController.java

package com.priv.gabriel.controller;

import com.priv.gabriel.entity.User;

import com.priv.gabriel.repository.UserRepository;

import org.springframework.beans.factory.annotation.Autowired;

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

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;

/**

* Created by Administrator on 2018/10/9.

*/

@RestController

@RequestMapping("/users")

public class UserController {

@Autowired

private UserRepository userRepository;

@RequestMapping(value = "/",method = RequestMethod.GET)

public List usersList(){

return userRepository.findAll();

}

@RequestMapping(value = "/" ,method = RequestMethod.PUT)

public String updateUser(User user){

if(userRepository.save(user) != null){

return "修改成功";

}else{

return "修改失败";

}

}

@RequestMapping(value = "/{id}",method = RequestMethod.GET)

public User selectUserById(@PathVariable long id){

return userRepository.findById(id).get();

}

@RequestMapping(value ="/{id}",method = RequestMethod.DELETE)

public String deleteUser(@PathVariable long id){

userRepository.deleteById(id);

return "删除成功";

}

@RequestMapping(value = "/",method = RequestMethod.POST)

public String saveUser(User user){

System.out.println(userRepository);

if(userRepository.save(user) != null){

return "新增成功";

}else{

return "新增失败";

}

}

}

此处就偷个懒不写service层了,要研究的小朋友还是不要学我哈

UserRepository.java

package com.priv.gabriel.repository;

import com.priv.gabriel.entity.User;

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

import org.springframework.stereotype.Repository;

/**

* Created with Intellij IDEA.

*

* @Author: Gabriel

* @Date: 2018-10-08

* @Desciption:

*/

@Repository

public interface UserRepository extends JpaRepository{

}

使用jpa最大的好处就是你只需要基础一个JpaRepository接口,其余的都交给jpa自己去处理,我们只负责调用就好了,回到springboot的主题就是

just run

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值