1. 程式人生 > >spring boot jpa整合和使用講解

spring boot jpa整合和使用講解

1.pom.xml中新增jpa依賴

  1. <dependency>  
  2.     <groupId>org.springframework.boot</groupId>  
  3.     <artifactId>spring-boot-starter-data-jpa</artifactId>  
  4. </dependency> 

2.application.yml中配置資料庫連線和jpa的相關屬性

  1. spring:  
  2.   datasource:  
  3.     driver-class-name: com.mysql.jdbc.Driver  
  4.     url: jdbc:mysql://192.168.1.18/db_sell?charactorEncoding=utf-8&useSSL=false  
  5.     username: root  
  6.     password: 123456  
  7.   jpa:  
  8.     show-sql: true 

     如果用這中application.properties配置也差不多

  1. spring.jpa.show-sql = true
  2. #spring.jpa.hibernate.ddl-auto = update
  3. spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
  4. #spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultNamingStrategy
  5. spring.jpa.database = SQLSERVER
  6. spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.HSQLDialect

3.jpa的使用

   spring data jpa 預設預先生成了一些基本的CURD的方法,例如:增、刪、改等等

繼承JpaRepository用的時候注入就可以直接使用了



spring data jpa方法命名規則

關鍵字方法命名sql where字句
AndfindByNameAndPwdwhere name= ? and pwd =?
OrfindByNameOrSexwhere name= ? or sex=?
Is,EqualsfindById,findByIdEqualswhere id= ?
BetweenfindByIdBetweenwhere id between ? and ?
LessThanfindByIdLessThanwhere id < ?
LessThanEqualsfindByIdLessThanEqualswhere id <= ?
GreaterThanfindByIdGreaterThanwhere id > ?
GreaterThanEqualsfindByIdGreaterThanEqualswhere id > = ?
AfterfindByIdAfterwhere id > ?
BeforefindByIdBeforewhere id < ?
IsNullfindByNameIsNullwhere name is null
isNotNull,NotNullfindByNameNotNullwhere name is not null
LikefindByNameLikewhere name like ?
NotLikefindByNameNotLikewhere name not like ?

StartingWith

findByNameStartingWithwhere name like '?%'
EndingWithfindByNameEndingWithwhere name like '%?'
ContainingfindByNameContainingwhere name like '%?%'
OrderByfindByIdOrderByXDescwhere id=? order by x desc
NotfindByNameNotwhere name <> ?
InfindByIdIn(Collection<?> c)where id in (?)
NotInfindByIdNotIn(Collection<?> c)where id not  in (?)
True

findByAaaTue

where aaa = true
FalsefindByAaaFalsewhere aaa = false
IgnoreCasefindByNameIgnoreCasewhere UPPER(name)=UPPER(?)

自定義SQL查詢

其實Spring data 覺大部分的SQL都可以根據方法名定義的方式來實現,但是由於某些原因我們想使用自定義的SQL來查詢,

spring data也是完美支援的;在SQL的查詢方法上面使用@Query註解,如涉及到刪除和修改在需要加上@Modifying.

也可以根據需要新增 @Transactional 對事物的支援,查詢超時的設定等



如果你是湖南的 歡迎加入 湖南人在深圳-Java群:557651502

相關推薦

spring boot jpa整合使用講解

1.pom.xml中新增jpa依賴<dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starte

Spring Boot Dubbo 整合入門理解

Spring Boot Dubbo 整合和入門理解    1、搭建Dubbo 框架需要用到幾個元件來看一下元件理解以下dubbo 的整個過程 這樣畫大家應該可以看懂吧,下面就來介紹以下各個模組的安裝和使用。 2、zookeeper   

RabbitMq在spring boot整合應用

具體如果安裝rabbitmq在本例子中不涉及,主要講解在springboot中如果傳送和接收rabbitmq訊息。 1.新增依賴 <dependency> <groupId>org.springframewor

Spring Boot + jpa + oracle 整合及遇到的坑

組裡大佬擼的程式碼,借鑑簡單的部分 一、maven依賴 只需要三個依賴:springboot , springboot jpa , ojdc。lombok是@slf4j用的,可以簡化日誌程式碼;其中ojdc 如果maven不能下載的話,可以到csdn下載:https://

Spring boot專案整合thymeleafshiro

專案使用了spring boot 框架和orm框架用了spring data jpa和前臺是thymeleaf 第一步:先在pom.xml加入thymeleaf和shiro的依賴 <dependency> <groupId>com.

spring boot+jpa+druid多資料來源整合示例

新增pom依賴 <dependencies> <dependency> <groupId>org.springframework.boot</groupId>

Spring Boot整合Spring Data JPA

綜合概述 JPA是Java Persistence API的簡稱,是一套Sun官方提出的Java持久化規範。其設計目標主要是為了簡化現有的持久化開發工作和整合ORM技術,它為Java開發人員提供了一種ORM工具來管理Java應用中的關係資料。 簡而言之,JPA提供了使用面向物件的方式操作資料庫的功能。JPA

spring-boot-plus1.1.0.釋出-整合Spring Boot Admin管理監控應用

Spring Boot Admin Spring Boot Admin用來管理和監控Spring Boot應用程式 應用程式向我們的Spring Boot Admin Client註冊(通過HTTP)或使用SpringCloud®(例如Eureka,Consul)發現 UI是Spring Boot Actu

Spring Boot 屬性配置使用

系統環境 itl class location web ron hashmap 驗證 ssp spring Boot 允許通過外部配置讓你在不同的環境使用同一應用程序的代碼,簡單說就是可以通過配置文件來註入屬性或者修改默認的配置。 Spring Boot 系列

Spring Boot-------JPA——EntityManager構建通用DAO

增刪改查 throw date print ase pre persist images lag  EntityManager   EntityManager 是用來對實體Bean 進行操作的輔助類。他可以用來產生/刪除持久化的實體Bean,通過主鍵查找實體bean

Spring Boot使用HandlerInterceptorAdapterWebMvcConfigurerAdapter實現原始的登錄驗證

onf ota throws 介紹 fig config sta apt 進行 HandlerInterceptorAdapter的介紹:http://www.cnblogs.com/EasonJim/p/7704740.html,相當於一個Filter攔截器,但是這個顆粒

Spring Boot + JPA(hibernate 5) 開發時,數據庫表名大小寫問題

hibernate mysql spring boot 大小寫 這幾天在用spring boot開發項目, 在開發的過程中遇到一個問題hibernate在執行sql時,總會提示表不存在。尋找之後發現, 建表時,表統一采用了大寫。hibernate會把大寫統一轉換成小寫。且 mysql在 lin

spring boot配置mybatis事務管理

生成 很多 -s 順序 south 位置 ron 技術 username spring boot配置mybatis和事務管理 一、spring boot與mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依賴如下: <!-- Spri

spring boot入門 -- 介紹第一個例子

image 目錄結構 scope for odi 服務架構 引用 ima 要求 轉載:https://www.cnblogs.com/junyang/p/8151802.html “越來越多的企業選擇使用spring boot 開發系統,spring boot牛在什麽地方?

spring boot jpa】hql語句報錯 :antlr.NoViableAltException: unexpected token: roleName

圖片 set 技術 body except exp 所在 antlr class 使用場景:在spring data jpa下使用@Query("hql語句") 然後在項目啟動的時候報錯 hql語句報錯:antlr.NoViableAltException: unexpec

015-Spring Boot 定制優化內嵌的Tomcat

web容器 ffi add nec app ase class new ont 一、內嵌web容器   參看http://www.cnblogs.com/bjlhx/p/8372584.html   查看源碼可知提供以下三種:      二、定制優化tomcat

Spring boot + Freemarker 整合

ext temp har 默認 htm star HA artifact AR 1.首先要添加freemarker依賴包 <dependency> <groupId>org.springframework.boot</groupId>

spring boot的@RequestParam@RequestBody的區別

ble rest val LV 請求頭 ade head 暴露 設計 尊重原創:https://blog.csdn.net/u013306545/article/details/79071683 一、問題描述 由於項目是前後端分離,因此後臺使用的是spring

Spring Boot 1.X2.X優雅彩38平臺出租重啟實戰

main pau ble eas ren 超過 kill -9 tst 兼容 項目在彩38平臺出租 haozbbs.com Q1446595067 重新發布的過程中,如果有的請求時間比較長,還沒執行完成,此時重啟的話就會導致請求中斷,影響業務功能,優雅重啟可以保證在停止的時

Spring Boot實踐——基礎常用配置

develop google art 容器 .sql pem -i 未定義 eve 借鑒:https://blog.csdn.net/j903829182/article/details/74906948 一、Spring Boot 啟動註解說明 @SpringBoot