SpringBoot在IDEA下使用JPA
1依赖
使用IDEA构建基于JPA的项目需要引用JPA、MYSQL依赖

2配置文件修改
2.1连接库
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=Aa123123
2.2开启jpa日志
spring.jpa.show-sql=true
3编写实体连接表
实体类增加注解
@Entity
@Table(name = "car")
主键增加注解
@Id()
字段增加注解
@Column(name = "car_id")
4定义JPA接口类
JPA接口默认提供保存、delByID,如果额外需要查询和更新需要手动定义接口,其中使用update接口有两点需要注意:
1用户手动编写SQL放在@Query注解中;
2增加注解@Transactional、@Modifying用于update方法。
package com.cloud.jpa.repository; import com.cloud.jpa.entity.Car;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import javax.transaction.Transactional; @Repository
public interface FirstJpaRepository extends JpaRepository<Car, Integer> { Car findByCarId(Integer carid); Car findByCarName(String carName); @Transactional
@Modifying
@Query(value = "update car c set c.car_name=? where car_id=?", nativeQuery = true)
int updateCarNameByCarId(String carName, Integer carId);
}
5开始使用
搭建好上述环境后就可以实现基本的基于JPA的CRUD了
项目地址 https://github.com/MingHaiTian/springboot-jpa.git
SpringBoot在IDEA下使用JPA的更多相关文章
- SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法
软件152 尹以操 首先谢谢大佬的简书文章:http://www.jianshu.com/p/45ad65690e33# 这篇文章中讲的是spring中使用spring data jpa,使用了xml ...
- alibaba druid 在springboot start autoconfig 下的bug
alibaba druid 在springboot start autoconfig下的bug 标签(空格分隔):druid springboot start autoconfig 背景 发现.分析过 ...
- SpringBoot系列之Spring Data Jpa集成教程
SpringBoot系列之Spring Data Jpa集成教程 Spring Data Jpa是属于Spring Data的一个子项目,Spring data项目是一款集成了很多数据操作的项目,其下 ...
- Springboot的resources下资源访问的问题
对于路径问题,是让我一直感到痛苦的事情,首先是因为我的眼高手低,感觉路径这么简单根本没必要去看,但是昨天项目组长的冷嘲热讽让我无地自容:“你竟然连linux和window的路径的区别都不知道,呵呵”. ...
- SpringBoot图文教程12—SpringData Jpa的基本使用
有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+ ...
- SpringBoot入门:Spring Data JPA 和 JPA(理论)
参考链接: Spring Data JPA - Reference Documentation Spring Data JPA--参考文档 中文版 纯洁的微笑:http://www.ityouknow ...
- SpringBoot(三)thymeleaf+JPA+MySql
接着上一节的 第一步:在pom文件中加入以下代码: <!--JPA--> <dependency> <groupId>org.springframework.boo ...
- springboot(五):spring data jpa的使用
在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项 使用spr ...
- SpringBoot(五) :spring data jpa 的使用
原文出处: 纯洁的微笑 在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法 ...
随机推荐
- iOS之estimatedHeightForRowAtIndexPath避免程序EXC_BAD_ACCESS
在你的项目中把estimatedHeightForRowAtIndexPath方法写实现以下,返回一个估计高度(随便估,笔者建议还是按照正常思路来写,大概高度是多少就返回多少),这样就不会报EXC_B ...
- js中哪些值会被认为false?
在javascript中,只有 false null undefined 空字符串 即 “” 数字 0 数字 NaN 会被当作false,其余都是真. 注:字符串 “false” 也会被当作真.
- jQuery 遍历 - children() 方法 获取指定id下子元素的值
<a id="Aobj_2_2" class="" specid="2" specvid="2" href=&qu ...
- webpack报错Cannot read property 'presetToOptions' of undefined
在学习react全家桶时,webpack首先报错,报错内容如下,最后我是因为没有全局安装webpack导致的报错,使用npm install webpack -g安装解决了这个问题.
- Linux 运维工程师学习成长路线上要经历哪四个阶段?
之前曾看到一篇新闻,Linux之父建议大家找一份基于Linux和开源环境的工作.今天就来聊一聊我的想法,本人8年Linux运维一线经验,呆过很多互联网公司,从一线运维做到运维架构师一职,也见证了中国运 ...
- js判断当前浏览器是否是源生app的webview
有些时候,我们在开发过程中需要判断,当前页面被打开是否是处于源生的webview里面,或者NODEJS做服务器后端支持的时候,判断请求来源是否来至于源生webview里面被打开的页面请求GET/POS ...
- vue使用axios调用豆瓣API跨域问题
最近做了一个vue小demo,使用了豆瓣开源的API,通过ajax请求时需要跨域才能使用. 封面.jpg 一.以下是豆瓣常用的开源接口: 正在热映 :https://api.douban.com/ ...
- kafka zk常用命令
1 创建topic: kafka-topics.sh --create --zookeeper 3.3.3.3:2181 --replication-factor 1 --partitions 3 ...
- go字符串操作
在Go语言标准库中的strings和strconv两个包可以对字符串做快速处理 string包 func Contains(s, substr string) bool 字符串s中是否包含substr ...
- c. 求阶乘和的方法(N的值不能太大)初学者
#include <stdio.h> int main() { int n,i; int a=1; //a设置为一个数的阶乘 int b; // b 设置为阶乘的和 for(i ...