转载请标明出处:

原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot4-jpaJ/

本文出自方志朋的博客

JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。

JPA 的目标之一是制定一个可以由很多供应商实现的API,并且开发人员可以编码来实现该API,而不是使用私有供应商特有的API。

JPA是需要Provider来实现其功能的,Hibernate就是JPA Provider中很强的一个,应该说无人能出其右。从功能上来说,JPA就是Hibernate功能的一个子集。

添加相关依赖

添加spring-boot-starter-jdbc依赖:


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

添加mysql连接类和连接池类:

	<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

配置数据源,在application.properties文件配置:

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
username: root
password: 123456 jpa:
hibernate:
ddl-auto: update # 第一次简表create 后面用update
show-sql: true

注意,如果通过jpa在数据库中建表,将jpa.hibernate,ddl-auto改为create,建完表之后,要改为update,要不然每次重启工程会删除表并新建。

创建实体类

通过@Entity 表明是一个映射的实体类, @Id表明id, @GeneratedValue 字段自动生成

@Entity
public class Account {
@Id
@GeneratedValue
private int id ;
private String name ;
private double money; ... 省略getter setter
}

Dao层

数据访问层,通过编写一个继承自 JpaRepository 的接口就能完成数据访问,其中包含了几本的单表查询的方法,非常的方便。值得注意的是,这个Account 对象名,而不是具体的表名,另外Interger是主键的类型,一般为Integer或者Long

public interface AccountDao  extends JpaRepository<Account,Integer> {
}

Web层

在这个栗子中我简略了service层的书写,在实际开发中,不可省略。新写一个controller,写几个restful api来测试数据的访问。

@RestController
@RequestMapping("/account")
public class AccountController { @Autowired
AccountDao accountDao; @RequestMapping(value = "/list", method = RequestMethod.GET)
public List<Account> getAccounts() {
return accountDao.findAll();
} @RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Account getAccountById(@PathVariable("id") int id) {
return accountDao.findOne(id);
} @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public String updateAccount(@PathVariable("id") int id, @RequestParam(value = "name", required = true) String name,
@RequestParam(value = "money", required = true) double money) {
Account account = new Account();
account.setMoney(money);
account.setName(name);
account.setId(id);
Account account1 = accountDao.saveAndFlush(account); return account1.toString(); } @RequestMapping(value = "", method = RequestMethod.POST)
public String postAccount(@RequestParam(value = "name") String name,
@RequestParam(value = "money") double money) {
Account account = new Account();
account.setMoney(money);
account.setName(name);
Account account1 = accountDao.save(account);
return account1.toString(); } }

通过postman请求测试,代码已经全部通过测试。

源码下载:https://github.com/forezp/SpringBootLearning

参考资料

accessing-data-jpa




扫码关注公众号有惊喜

(转载本站文章请注明作者和出处 方志朋的博客

SpringBoot非官方教程 | 第四篇:SpringBoot 整合JPA的更多相关文章

  1. SpringBoot进阶教程 | 第四篇:整合Mybatis实现多数据源

    这篇文章主要介绍,通过Spring Boot整合Mybatis后如何实现在一个工程中实现多数据源.同时可实现读写分离. 准备工作 环境: windows jdk 8 maven 3.0 IDEA 创建 ...

  2. (转)SpringBoot非官方教程 | 第七篇:springboot开启声明式事务

    springboot开启事务很简单,只需要一个注解@Transactional 就可以了.因为在springboot中已经默认对jpa.jdbc.mybatis开启了事事务,引入它们依赖的时候,事物就 ...

  3. SpringBoot非官方教程 | 第二十三篇: 异步方法

    转载请标明出处: 原文首发于https://www.fangzhipeng.com/springboot/2017/07/11/springboot-ansy/ 本文出自方志朋的博客 这篇文章主要介绍 ...

  4. (转)SpringBoot非官方教程 | 第三篇:SpringBoot用JdbcTemplates访问Mysql

    本文介绍springboot通过jdbc访问关系型MySQL,通过spring的JdbcTemplate去访问. 准备工作 jdk 1.8 maven 3.0 idea mysql 初始化mysql: ...

  5. SpringBoot非官方教程 | 第八篇:springboot整合mongodb

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot8-mongodb/ 本文出自方志朋的博客 这篇文 ...

  6. SpringBoot非官方教程 | 第六篇:springboot整合mybatis

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-mybatis/ 本文出自方志朋的博客 本文主要 ...

  7. SpringBoot非官方教程 | 第五篇:springboot整合 beatlsql

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot5-beatlsql/ 本文出自方志朋的博客 Be ...

  8. SpringBoot非官方教程 | 第七篇:springboot开启声明式事务

    转载请标明出处: http://blog.csdn.net/forezp/article/details/70833629 本文出自方志朋的博客 springboot开启事务很简单,只需要一个注解@T ...

  9. (转) SpringBoot非官方教程 | 第十一篇:springboot集成swagger2,构建优雅的Restful API

    swagger,中文“拽”的意思.它是一个功能强大的api框架,它的集成非常简单,不仅提供了在线文档的查阅,而且还提供了在线文档的测试.另外swagger很容易构建restful风格的api,简单优雅 ...

随机推荐

  1. SQL脚本整理系列一 表分区

    表分区的目的: 1.把历史数据放到另外一个表里面 可以提高查询效率 当然如果经常查询历史数据和新数据的合并结果集这样做就大大的不好了 2.通过把一个表放到不同的文件,不同的文件再存储到不同的磁盘列阵中 ...

  2. mysql 查两个表相同的值

    比如一个数据库 表A和表B 都有一个username字段, 现查出与表A中username值相同的表B的username和password数据 select B.username,B.password ...

  3. 远程SQL Server连接不上

    运行 cmd -> 输入 netsh winsock reset重启后 应该可以连接sql了

  4. H5插入视频兼容主浏览器

    插入视频的方法有很多种,但是有一些方法不兼容. 方法1:DW插入视频利用DW插入的视频为flv格式,操作较简单,但是代码复杂,需要浏览器支持flash插件:火狐浏览器需要手动下载flash插件,比较麻 ...

  5. node的版本控制之nvm的安装与使用

    NVM的安装 windows下的安装: windows下的离线安装: nvm 的windows下载地址:https://github.com/coreybutler/nvm-windows/relea ...

  6. js如何设置一个倒计时

    //申明一个定时器 let endInterval; //结束时间(毫秒数,这里是距离 1970 年 1 月 1 日至今的毫秒数) let endSeconds; //结束时间差 const ENDT ...

  7. @PathVariable @RequestParam @RequestBody等参数绑定注解详解

    一.分类 handler method 参数绑定常用的注解,我们根据他们处理的Request的内容不同分为四类: 处理request uri 部分的注解:   @PathVariable;(这里指ur ...

  8. 从软件工程师的角度看MacBook Air的几个设计亮点

    我多年从事软件开发和运营工作,从未跟“设计”间断过.现在在设计一个全新saas产品:超级表格(www.domypp.com).最近买了台苹果最新款的笔记本电脑MacBook Air,从该产品功能设计和 ...

  9. attention

    attention: 时序的刻画 attention 在recommendation 中的应用: 年龄的增长, 对于商品的喜好 Dynamic attention deeo model:

  10. Vbox安装CentOS7及网络配置

    安装CentOS7及网络配置 Vbox和其他虚拟机一样,安装完成一个虚拟机,需要配置网络才能实现物理主机和虚拟机之间的访问.虚拟主机和Internet(外网)的访问 1.设置Vbox全局网络 单击主界 ...