什么是JPA

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

pom.xml

    <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

apllication.yml

server:
port: 8080 spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT%2B8 jpa:
hibernate:
ddl-auto: update
show-sql: true

Dao

import com.vast.entity.Account;
import org.springframework.data.jpa.repository.JpaRepository;
/** JPA中提供的crud的方法,可以直接进行调用。泛型中Account指要操作的数据库对应的实体类;Integer指的是该实体类对应的主键的类型 */
public interface IAccountDao extends JpaRepository<Account,Integer> {
}

Controller(请求方式是Restfull风格)

package com.vast.controller;

import com.vast.dao.IAccountDao;
import com.vast.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*; import java.util.List; @RestController
@RequestMapping("/account")
public class HomeController {
@Autowired
private IAccountDao accountDao; @Value("${my.name}")
private String name;
@Value("${my.age}")
private int age; @GetMapping("/home")
public String homePage(){
return name.concat("13__" + age);
} @GetMapping("/{id}")
public Account getAccountById(@PathVariable(value = "id") int id){ return accountDao.findOne(id);
} @GetMapping("/list")
public List<Account> getAccounts(){
return accountDao.findAll();
} @RequestMapping(value = "",method = RequestMethod.POST)
public String postAccount(@RequestParam(value = "name") String name,
@RequestParam(value = "money") double money) {
Account account = new Account();
account.setName(name);
account.setMoney(money); Account saveAccountResult = accountDao.save(account); return saveAccountResult.toString();
} @PutMapping("/{id}")
public String updateAccountById(@PathVariable (value = "id") int id,
@RequestParam(value = "name",required=false) String name,
@RequestParam(value = "money",required=false) double money){ Account account = new Account();
account.setName(name);
account.setMoney(money);
account.setId(id); Account account1 = accountDao.saveAndFlush(account); return account1.toString();
} }

SpringBoot入门-JPA(三)的更多相关文章

  1. SpringBoot入门(三)——入口类解析

    本文来自网易云社区 上一篇介绍了起步依赖,这篇我们先来看下SpringBoot项目是如何启动的. 入口类 再次观察工程的Maven配置文件,可以看到工程的默认打包方式是jar格式的. <pack ...

  2. SpringBoot入门教程(三)通过properties实现多个数据库环境自动切换配置

    前面的文章已经介绍了CentOS部署SpringBoot项目从0到1的详细过程,包括Linux安装ftp.Tomcat以及Java jdk的全部过程.这篇文章主要介绍关于springboot如何通过多 ...

  3. springboot入门(三)-- springboot集成mybatis及mybatis generator工具使用

    前言 mybatis是一个半自动化的orm框架,所谓半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernate这种完 ...

  4. SpringBoot入门笔记(三)、热加载

    1.配置热加载环境,在pom.xml添加如下代码 <build> <!--springloader plugin --> <plugins> <plugin& ...

  5. Springboot+MyBatis+JPA集成

      1.前言 Springboot最近可谓是非常的火,本人也在项目中尝到了甜头.之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Sp ...

  6. SpringBoot入门(五)——自定义配置

    本文来自网易云社区 大部分比萨店也提供某种形式的自动配置.你可以点荤比萨.素比萨.香辣意大利比萨,或者是自动配置比萨中的极品--至尊比萨.在下单时,你并没有指定具体的辅料,你所点的比萨种类决定了所用的 ...

  7. SpringBoot入门(四)——自动配置

    本文来自网易云社区 SpringBoot之所以能够快速构建项目,得益于它的2个新特性,一个是起步依赖前面已经介绍过,另外一个则是自动配置.起步依赖用于降低项目依赖的复杂度,自动配置负责减少人工配置的工 ...

  8. SpringBoot入门(二)——起步依赖

    本文来自网易云社区 在前一篇我们通过简单几步操作就生成了一个可以直接运行的Web程序,这是因为SpringBoot代替我们做了许多工作,概括来讲可以分为起步依赖和自动配置.这一篇先来看看起步依赖. 项 ...

  9. SpringBoot入门(一)——开箱即用

    本文来自网易云社区 Spring Boot是什么 从根本上来讲Spring Boot就是一些库的集合,是一个基于"约定优于配置"的原则,快速搭建应用的框架.本质上依然Spring, ...

随机推荐

  1. Linux文本处理sed、软件包管理、磁盘存储、文件系统和挂载

    Linux文本处理工具sed.软件包管理.磁盘存储及文件系统 文本处理工具sed巧妙用法 1.通过sed获取文件路径的基名和目录名 思路:采用正则表达式将文本字符串分组,取对应的分组后向引用即可. 获 ...

  2. tomcat日志分割

    1.下载(最新版本)并解压,cd进入安装目录 #  wget http://cronolog.org/download/cronolog-1.6.2.tar.gz # tar zxvf cronolo ...

  3. LGOJP3193 [HNOI2008]GT考试

    \(f[i][j]\)表示当前摆放到第\(i\)位,然后当前的匹配长度为\(j\) \(f[i][j]=\sum {f[i][k]*g[k][j]}\) \(g[i][j]\)表示将长度为\(i\)的 ...

  4. 原生js的常用方法总结

    =============== 通知: 博主已迁至<掘金>码字,博客园可能以后不再更新,掘金地址:https://juejin.im/post/5a1a6a6551882534af25a8 ...

  5. 利用Java反射机制优化简单工厂设计模式

    之前项目有个需求,审批流程的时候要根据配置发送信息:发送短信.发送邮件.当时看到这个就想到要用工厂模式,为什么要用工厂模式呢?用工厂模式进行大型项目的开发,可以很好的进行项目并行开发.就是一个程序员和 ...

  6. js事件绑定方法

    最近收集了一些关于JavaScript绑定事件的方法,汇总了一下,不全面,但是,希望便于以后自己查看. JavaScript中绑定事件的方法主要有三种: 1 在DOM元素中直接绑定 2 JavaScr ...

  7. js spread object

    What’s is the benefit / drawback of these two alternatives? Using object spread options = {...option ...

  8. my OD

    1 复习c文件处理内容 2 编写myod.c 用myod XXX实现Linux下od -tx -tc XXX的功能 main与其他分开,制作静态库和动态库 编写Makefile 5 提交测试代码和运行 ...

  9. 文件夹上传组件webupload插件

    javaweb上传文件 上传文件的jsp中的部分 上传文件同样可以使用form表单向后端发请求,也可以使用 ajax向后端发请求 1.通过form表单向后端发送请求 <form id=" ...

  10. Centrifugo  语言无关的实时消息服务

    Centrifugo 语言无关的实时消息服务,基于golang编写,提供了websocket 以及sockjs 的兼容处理,使用上很简单 同时也支持基于redis的扩展,以下是一个简单的运行测试 环境 ...