欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot下如何自定義Repository中的DAO方法

 更新時間:2017年06月01日 17:06:25   作者:bladestone  
這篇文章主要介紹了Spring Boot下如何自定義Repository中的DAO方法,需要的朋友可以參考下

 環(huán)境配置介紹

jdk 1.8, spring Boot 1.5.3.RELEASE, MySQL, Spring Data, JPA

問題描述

Spring Data提供了一套簡單易用的DAO層抽象與封裝,覆蓋的CURD的基本功能,但是在諸多的情況下,需要用戶自定義DAO的實現(xiàn)方法,來實現(xiàn)更為復(fù)雜和精細(xì)的數(shù)據(jù)庫訪問操作,該如何來解決這個問題?

目標(biāo)描述

這里我們以自定義testAA的方法為例,來介紹如何實現(xiàn)自定義的DAO方法擴(kuò)展。

數(shù)據(jù)庫表的定義

我們這里定義了一個非常簡單的mycity表,來作為示例的實體類BaseEntity:

數(shù)據(jù)庫表定義:

這里寫圖片描述

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
@MappedSuperclass
public abstract class BaseEntity implements java.io.Serializable {
 private static final long serialVersionUID = -2420979951576787924L;
 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)
 @Column(name = "ID")
 private Long id;
 @Version
 private Long version;
 @Temporal(TemporalType.TIMESTAMP)
 @Column(name = "CREATE_TIME",columnDefinition="timestamp default CURRENT_TIMESTAMP")
 private Date createTime;
 @Temporal(TemporalType.TIMESTAMP)
 @Column(name = "UPDATE_TIME",columnDefinition="timestamp default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
 private Date updateTime;
}

MyCity的定義如下:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import lombok.Data;
@Entity
@Table(name="mycity")
@Data
public class City extends BaseEntity {
 private static final long serialVersionUID = -7510771121759944670L;
 @Column(name="Name")
 private String name;
 @Column(name="country_code")
 private String countryCode;
 @Column
 private String district;
 @Column
 private int population;
}

這里的@Data使用了lombok提供的強(qiáng)大標(biāo)注,來簡化冗余Getter/Setter方法的使用。

定義Repository

標(biāo)準(zhǔn)的CityRepository.Java,這里完全使用缺省提供的方法:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.rose.money.City;
@Repository
public interface CityRepository extends JpaRepository<City, Long>, CityRepositoryCustom{
}

這里的CityRepository繼承了2個父類,包括用戶自定義的接口類,讓用戶自定義的接口可以暴漏出來。
這里的CityRepsoitoryCustom定義了用戶的自定義方法:

public interface CityRepositoryCustom {
 public void testAA();
}

Notice: 這里的Custom后綴是約定的,不能隨意修改。

自定義方法的實現(xiàn)類:

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.beans.factory.annotation.Autowired;
public class CityRepositoryImpl implements CityRepositoryCustom {
 @Autowired
 @PersistenceContext
 private EntityManager entityManager;
 @Override
 public void testAA() {
  List<Object[]> cities = entityManager.createNativeQuery("select id, name, district from mycity").getResultList();
  for (Object[] objs : cities) {
   System.out.print("location 1:" + objs[0]);
   System.out.print("location 2:" + objs[1]);
   System.out.print("location 3:" + objs[2]);
  }
 }
}

這里的實現(xiàn)類就是讀取了幾條記錄,然后打印出來。其實現(xiàn)了Custom的接口類。

配置信息

application.properties:

spring.application.name=custom jpa
spring.jpa.database=MYSQL
spring.datasource.username=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/world?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=true
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy 
spring.jpa.show-sql=true

測試

測試用例:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.rose.money.repository.CityRepository;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomjpaApplicationTests {
 @Autowired
 private CityRepository cityRepo;
 @Test
 public void contextLoads() {
  City city = cityRepo.findOne(1l);
  System.out.println("city=>" + city);
  cityRepo.testAA();
 }
}

測試的結(jié)果圖示:

這里寫圖片描述

總結(jié)

約定大于配置,Custom后綴實現(xiàn)與擴(kuò)展,非常的簡單實用。

以上所述是小編給大家介紹的Spring Boot下如何自定義Repository中的DAO方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • SpringBoot項目實現(xiàn)MyBatis流式查詢的教程詳解

    SpringBoot項目實現(xiàn)MyBatis流式查詢的教程詳解

    這篇文章主要介紹了SpringBoot項目如何實現(xiàn)MyBatis的流式查詢,mybatis的流式查詢,有點冷門,實際用的場景比較少,但是在某些特殊場景下,卻是十分有效的一個方法,感興趣的同學(xué)可以參考一下
    2023-06-06
  • java中extends與implements的區(qū)別淺談

    java中extends與implements的區(qū)別淺談

    java中extends與implements的區(qū)別淺談,需要的朋友可以參考一下
    2013-03-03
  • Java MongoDB實現(xiàn)REST過程解析

    Java MongoDB實現(xiàn)REST過程解析

    這篇文章主要介紹了Java MongoDB實現(xiàn)REST過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • Spring Cloud入門教程之Zuul實現(xiàn)API網(wǎng)關(guān)與請求過濾

    Spring Cloud入門教程之Zuul實現(xiàn)API網(wǎng)關(guān)與請求過濾

    這篇文章主要給大家介紹了關(guān)于Spring Cloud入門教程之Zuul實現(xiàn)API網(wǎng)關(guān)與請求過濾的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-05-05
  • MybatisPlus創(chuàng)建時間不想用默認(rèn)值的問題

    MybatisPlus創(chuàng)建時間不想用默認(rèn)值的問題

    MybatisPlus通過FieldFill注解和MpMetaObjectHandler類支持自動填充字段功能,特別地,可以設(shè)置字段在插入或更新時自動填充創(chuàng)建時間和更新時間,但在特定場景下,如導(dǎo)入數(shù)據(jù)時,可能需要自定義創(chuàng)建時間
    2024-09-09
  • 了解java Struts攔截器的相關(guān)操作

    了解java Struts攔截器的相關(guān)操作

    Struts為我們實現(xiàn)了很多的功能,比如數(shù)據(jù)自動封裝,文件上傳功能阿。Struts為我們提供的這些功能都是通過攔截器完成的。下面我們來詳細(xì)了解一下吧
    2019-06-06
  • SpringMVC跨服務(wù)器上傳文件中出現(xiàn)405錯誤的解決

    SpringMVC跨服務(wù)器上傳文件中出現(xiàn)405錯誤的解決

    這篇文章主要介紹了SpringMVC跨服務(wù)器上傳文件中出現(xiàn)405錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java中常見的6種線程池示例詳解

    java中常見的6種線程池示例詳解

    這篇文章主要介紹了java中常見的6種線程池示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Java利用for循環(huán)打印菱形的實例教程

    Java利用for循環(huán)打印菱形的實例教程

    這篇文章主要給大家介紹了關(guān)于Java利用for循環(huán)打印菱形的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java兩種方式實現(xiàn)動態(tài)代理

    Java兩種方式實現(xiàn)動態(tài)代理

    Java 在 java.lang.reflect 包中有自己的代理支持,該類(Proxy.java)用于動態(tài)生成代理類,只需傳入目標(biāo)接口、目標(biāo)接口的類加載器以及 InvocationHandler 便可為目標(biāo)接口生成代理類及代理對象。我們稱這個Java技術(shù)為:動態(tài)代理
    2020-10-10

最新評論