在hibernate 中比 SimplePropertyPreFilter 還要好用的過濾類 ComplexPropertyPreFilter(級聯過濾,關係對映過濾類,複雜的屬性預過濾器)!!!!
在hibernate 中比 SimplePropertyPreFilter 還要好用的過濾類
比官方自帶的過濾類(SimplePropertyPreFilter )還好用,那肯定是自定義的啦!
先講下結果吧,看是不是諸位要的:
- 能過濾類中的屬性類,無論是Set集合,List集合,還是EJB都能過濾
- 能解決多對多,或者是一對多,一對一等關係對映死迴圈,資料返回資料多,雜,亂等特點
- 做json的主宰,想要返回的資料是哪些欄位就返回哪些欄位
- 簡單上手
好吧上程式碼:
package intercepter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.PropertyPreFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import model.wudun.Articles;
import model.wudun.Comment;
import model.wudun.Member;
import java.util.*;
/**
* @Author:wj
* @Date:Created in 15:16 2018/1/5
* @Description :用 fasjson 對 hibernate 級聯關係進行過濾
*/
public class ComplexPropertyPreFilter implements PropertyPreFilter {
private Map<Class<?>, String[]> includes = new HashMap<>();
private Map<Class<?>, String[]> excludes = new HashMap<>();
static {
JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.DisableCircularReferenceDetect.getMask();
}
public ComplexPropertyPreFilter() {
}
public ComplexPropertyPreFilter(Map<Class<?>, String[]> includes) {
super();
this.includes = includes;
}
public boolean apply(JSONSerializer serializer, Object source, String name) {
//物件為空。直接放行
if (source == null) {
return true;
}
// 獲取當前需要序列化的物件的類物件
Class<?> clazz = source.getClass();
// 無需序列的物件、尋找需要過濾的物件,可以提高查詢層級
// 找到不需要的序列化的型別
for (Map.Entry<Class<?>, String[]> item : this.excludes.entrySet()) {
// isAssignableFrom(),用來判斷型別間是否有繼承關係
if (item.getKey().isAssignableFrom(clazz)) {
String[] strs = item.getValue();
// 該型別下 此 name 值無需序列化
if (isHave(strs, name)) {
return false;
}
}
}
// 需要序列的物件集合為空 表示 全部需要序列化
if (this.includes.isEmpty()) {
return true;
}
// 需要序列的物件
// 找到不需要的序列化的型別
for (Map.Entry<Class<?>, String[]> item : this.includes.entrySet()) {
// isAssignableFrom(),用來判斷型別間是否有繼承關係
if (item.getKey().isAssignableFrom(clazz)) {
String[] strs = item.getValue();
// 該型別下 此 name 值無需序列化
if (isHave(strs, name)) {
return true;
}
}
}
return false;
}
/*
* 此方法有兩個引數,第一個是要查詢的字串陣列,第二個是要查詢的字元或字串
*/
public static boolean isHave(String[] strs, String s) {
for (int i = 0; i < strs.length; i++) {
// 迴圈查詢字串陣列中的每個字串中是否包含所有查詢的內容
if (strs[i].equals(s)) {
// 查詢到了就返回真,不在繼續查詢
return true;
}
}
// 沒找到返回false
return false;
}
public Map<Class<?>, String[]> getIncludes() {
return includes;
}
public void setIncludes(Map<Class<?>, String[]> includes) {
this.includes = includes;
}
public Map<Class<?>, String[]> getExcludes() {
return excludes;
}
public void setExcludes(Map<Class<?>, String[]> excludes) {
this.excludes = excludes;
}
public static void main(String[] args) {
// use instanceOf,用來判斷物件是否是類的例項
// use isAssignableFrom(),用來判斷型別間是否有繼承關係
// use isInstance(),用來判斷物件是否是類的例項
//setExcludes(),進行過濾的類!!!!
Class<?> intClass = Integer.class;
// Create various objects.
String str = "Hello";
Date date = new Date();
Integer i = new Integer(10);
// Is str an instance of class Integer?
boolean check1 = intClass.isInstance(str);
System.out.println("str is an Integer? " + check1);
// Is date an instance of class Integer?
boolean check2 = intClass.isInstance(date);
System.out.println("date is an Integer? " + check2);
// Is i an instance of class Integer?
boolean check3 = intClass.isInstance(i);
System.out.println("i is an Integer? " + check3);
System.out.println(Articles.class.isInstance(new Comment()));
System.out.println(Comment.class.isInstance(new Articles()));
System.out.println(Articles.class.isAssignableFrom(Member.class));
//這裡是測試程式碼!! 博主一些EJB類 不要糾結導不出來!!!! 不要糾結導不出來!!!! 不要糾結導不出來!!!!
List<Articles> articlesList = new ArrayList<>(0);
Articles articles = new Articles();
articles.setId("articles_1");
Comment comment = new Comment();
comment.setCommentId("comment_1");
Member member = new Member();
member.setId("member_1");
articles.setMember(member);
Set<Articles> articlesSet = new LinkedHashSet<>(0);
articlesSet.add(articles);
member.setArticlesCollect(articlesSet);
Set<Member> memberSet = new LinkedHashSet<>(0);
memberSet.add(member);
articles.setMemberSet(memberSet);
Set<Comment> commentSet = new LinkedHashSet<>(0);
commentSet.add(comment);
articles.setComments(commentSet);
member.setCommentSet(commentSet);
comment.setMember(member);
articlesList.add(articles);
ComplexPropertyPreFilter filter = new ComplexPropertyPreFilter();
filter.setExcludes(new HashMap<Class<?>, String[]>() {
private static final long serialVersionUID = -8411128674046835592L;//我也不知道這是幹嘛的 刪除或者不寫不會影響其功能和效果
{ //物件 Articles中 : 多對多屬性:giveRewardSet(Set集合) memberSet(Set集合)
put(Articles.class, new String[] { "giveRewardSet","memberSet"});//放入過濾的物件,然後是需要過濾的物件裡的屬性
//物件Comment :commentId(Comment物件中普通的屬性), member(Comment物件中物件屬性)
put(Comment.class, new String[] { "commentId", "member" });
//物件Member :nickname(Member物件中普通的屬性) 多對多屬性: articlesCollect(Set集合)
put(Member.class, new String[] { "nickname", "articlesCollect" });
//當然 多對一 一對一也是可以過濾的啦!!! 如果還有不清楚的歡迎詢問
}
});
System.out.println(JSON.toJSONString(articlesList, filter));
}
}
謝謝!若有問題歡迎踩………
由於被博主遇到這個問題深感頭疼 然後機緣巧合的狀況下刷到部落格,覺得該博主部落格不是很詳細沒有很好的說明這個實體類的強大效能,然後博主覺得這篇部落格埋沒於碼海中是在可以故此轉發 , 脫茂!!:
https://www.cnblogs.com/sandyfog/articles/3679804.html
向老前輩致敬
相關推薦
在hibernate 中比 SimplePropertyPreFilter 還要好用的過濾類 ComplexPropertyPreFilter(級聯過濾,關係對映過濾類,複雜的屬性預過濾器)!!!!
在hibernate 中比 SimplePropertyPreFilter 還要好用的過濾類 比官方自帶的過濾類(SimplePropertyPreFilter )還好用,那肯定是自定義的啦! 先講下結果吧,看是不是諸位要的: 能過濾
centos安裝tldr神器(比man還好用的工具)
linux學習歷程先從github上把tldr克隆下來:git clone https://github.com/tldr-pages/tldr.git安裝需求:1、pip(需要Python2.7+或3.3+環境)我用的是centos6.5 Python默認版本2.6.6,(版本太低安裝不了pip)先更新下P
比freemarker還好用的模板引擎,poi-tl,動態生成資料
在之前專案中有這樣一個需求,就是本地儲存合同模板,然後要動態生成合同的內容,之後呼叫第三方(上上籤)去進行簽署合同和蓋章,當說動態生成合同中內容是,我第一個想到的是freemarker,後來跟同事商量了一下最後決定用poi-tl,這是一個純java的模板引擎,是基於word的
Hibernate 級聯操作,對映檔案中的預設的Lazy屬性導致的異常
hibernate中的延遲載入策略一定程度上降低了記憶體開銷,但是有時候使用會出現不想要的異常。 首先,hibernate hbm 中lazy屬性(true|false),在hibernate中預設lazy是true。 Hibernate中允許使用延遲載入的地方主要有以下
@程式設計師,你們還在用網上亂找的方法匯入匯出Excel麼,我們給你造了個輪子!!!!!
程式設計師的顯著特點 有一天跟一位同事跟我閒聊,討論起過去若干年軟體行業的感受,他問了個問題:你覺得一個好的軟體工程師最顯著的特點是什麼? 我想了一會,說:大概是坐得住吧。 某種意義上來說,在網際網路技術飛速發展的今天,資訊的洪流總是無聲無息間把我們掩埋,一不小心,可能就感覺自己似乎已經out了。 當然,資訊
hibernate多對多雙向關系映射的級聯配置
hibernate多對多雙向關系映級配置多對多關系映射 set元素的屬性:cascade:級聯操作。取值:save-update:級聯保存更新delete:級聯刪除。 註意:在多對多雙向關系映射中,不能配置雙向級聯刪除。但是可以配置雙向級聯保存更新。 <set name=&q
用Darwin開發RTSP級聯伺服器(拉模式轉發)(附原始碼)
QTSS_Error DoDescribe(QTSS_StandardRTSP_Params* inParams) { char* theUriStr = NULL; QTSS_Error err = QTSS_GetValueAsString(inParams->inRTSPRequest,
mysql 在表中新增多個外來鍵/增加外來鍵/級聯約束
CREATE TABLE`xh` ( `id` int(100) unsigned NOT NULL AUTO_INCREMENT COMMENT , `cl_id` smallint(3) unsigned NOT NULL COMMENT, `title` varchar(100) COLLAT
mysql在表中新增多個外來鍵/增加外來鍵/級聯約束
CREATE TABLE`xh` ( `id` int(100) unsigned NOT NULL AUTO_INCREMENT COMMENT , `cl_id` smallint(3) unsigned NOT NULL COMMENT, `title` varchar(100) COLLATE
Extjs4中tree元件子節點和父節點的級聯操作
方式一: checkchange:function(node,checked,options){ //遍歷孩子,遞迴實現找到所有孩子節點 var allChild=function(node,flag){ node.eachChild(
除了創造比特幣,中本聰還教了我們如何保護賬號安全
作者 | Jack Dossman 譯者 | 李曉泉 編輯 | 波波 "In Satoshi We Trust" 區塊鏈時代最大的祕密,就是比特幣之父中本聰。坐擁100萬比特幣(峰值時接近200億美元)的中本聰,自從2011年離場後,竟再也沒有動過
Python居然還能用到財務當中?資料清洗中的運用!無所不能的Py
目前,江北區審計局資料分析小組運用Python語言對區級20個部門預算執行審計專案的多個部門財務資料進行了清理,相對其他資料清理工具,Python更加靈活、簡潔、高效和準確。 由於我區各部門財務核算軟體未統一,各單位財務軟體型別和版本各異,會計科目設定及會計處理方式不一致
為什麼使用懶載入?為什麼hibernate中的實體不用private?懶載入用到那些地方,為什麼用?
1、懶載入又稱延遲載入,就是當你需要載入一個數據的時候,他只返回這個物件的代理物件, 這個過程是通過CGLB實現的,CGLB代理是面向物件的代理,如果物件這個實體的屬性使用了private
中超新開始!告別壟斷性統治 有一點比投入還重要
11月29日訊息,@北京商報從度小滿金融人士處獲悉,百度正式拿到准許經營證券期貨的許可證。據許可證顯示,機構名稱為北京百度百盈科技有限公司(下稱“百度百盈”),證券期貨業務經營範圍為基金銷售。而今年8月22日,根據北京證監局官網顯示,證監局已核准百度百盈證券投資基金銷售業務資格。 企查查資訊顯示,百度百盈成
【推薦系統】2017年,你還在用使用者畫像和協同過濾做推薦系統嗎?
本文是大資料雜談 7 月 13 日社群公開課分享整理,也是第四正規化主題月的第二堂公開課內容。 今天想和大家分享,如何使用大規模機器學習解決真實的業務問題。我們今天會以機器學習中的一個典型場景為例來講解,即基於大規模機器學習模型的推薦系統。 推薦系統的本質是什麼? 比如說我們看到手機淘寶首頁,往下一
2017年,你還在用使用者畫像和協同過濾做推薦系統嗎?
本文是大資料雜談 7 月 13 日社群公開課分享整理,也是第四正規化主題月的第二堂公開課內容。 今天想和大家分享,如何使用大規模機器學習解決真實的業務問題。我們今天會以機器學習中的一個典型場景為例來講解,即基於大規模機器學習模型的推薦系統。 推薦系統的本質是什麼? 比
H5時代leaflet中還在用DivIcon?
前段時間寫了篇《[leaflet如何載入10萬資料](http://gisarmory.xyz/blog/index.html?blog=leaflet100ThousandData)》的文章,有同學反應其中的Canvas-Markers外掛不支援DivIcon。我們今天就來聊一聊,為什麼這個外掛不支援Div
Hibernate中的五大核心類和接口
session 回滾 開啟事務 核心 避免 事務 提交 jdbc 一級緩存 Hibernate中的五大核心類和接口 Configuration(類) : 加載配置文件hibernate.cfg.xml文件中的配置信息,從而得到: 1).hibernate的
Hibernate中的條件查詢完畢類
hiberna 產生 content div ber ont size 查詢 criteria Hibernate中的條件查詢有下面三個類完畢: 1、Criteria:代表一次查詢 2、Criterion:代表一個查詢條件 3、Restrictions:產生
(七)Hibernate中使用JDBC
alt work 方法 代碼 cep cti 一個 sdm admin 在hibernate中獲取connection數據庫連接有兩種方法:(操作數據庫常用這種方法) 1. session.doReturningWork 返回一個對象,適用於查詢方法 2. ses