一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Spring Boot下如何自定義Repository中的DAO方法

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

2020-11-04 16:44bladestone Java教程

這篇文章主要介紹了Spring Boot下如何自定義Repository中的DAO方法,需要的朋友可以參考下

 環(huán)境配置介紹

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

問(wèn)題描述

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

目標(biāo)描述

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

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

我們這里定義了一個(gè)非常簡(jiǎn)單的mycity表,來(lái)作為示例的實(shí)體類BaseEntity:

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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的定義如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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)注,來(lái)簡(jiǎn)化冗余Getter/Setter方法的使用。

定義Repository

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

?
1
2
3
4
5
6
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個(gè)父類,包括用戶自定義的接口類,讓用戶自定義的接口可以暴漏出來(lái)。
這里的CityRepsoitoryCustom定義了用戶的自定義方法:

?
1
2
3
public interface CityRepositoryCustom {
 public void testAA();
}

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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]);
  }
 }
}

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

配置信息

application.properties:

?
1
2
3
4
5
6
7
8
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

測(cè)試

測(cè)試用例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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();
 }
}

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

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

總結(jié)

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

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

原文鏈接:http://blog.csdn.net/blueheart20/article/details/72821384

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 高清毛片一区二区三区 | 日韩一区二区三区免费 | 日韩免费高清专区 | 黑人巨摘花第一次出血 | 国产精品视频免费看 | 奇米成人 | 亚洲视频日韩 | 91欧美秘密入口 | 国内精品九一在线播放 | 天天综合网天天做天天受 | 婷婷色天使在线视频观看 | 亚洲人成毛片线播放 | 女教师被女同学调教成脚奴 | 男人疯狂进女人下部视频动漫 | 无遮18禁在线永久免费观看挡 | 国产免费久久精品44 | 精品在线播放 | 美女机巴| tobu8在线观看免费高清 | 亚洲免费国产 | 日韩亚洲欧美综合一区二区三区 | 国产午夜精品不卡视频 | yy3341殇情影院理论片 | 国产午夜精品一区二区 | 亚洲久操 | 日韩视频免费看 | 婷婷在线综合 | 99精品视频在线观看免费 | 日本69av | 猫咪社区在线播放 | 国产专区日韩精品欧美色 | 日本高清视频在线免费观看 | 欧美色在线 | 粉嫩国产14xxxxx0000 | 日产精品卡一卡2卡三卡乱码工厂 | 日韩不卡一区二区三区 | ysl蜜桃色成人麻豆 youwu在线影院 | 免费高清视频免费观看 | 成人精品第一区二区三区 | 女女同性做爰xxoo亲吻 | 免费久久久久 |