热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

第三篇:SpringBoot中使用JPA

第三,篇,springboo

上一篇使用了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


推荐阅读
author-avatar
手机用户2502890621
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有