Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
SpringTest
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Jira
Jira
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
測試專案
SpringTest
Commits
2260f545
Commit
2260f545
authored
Oct 04, 2018
by
楊慶堂
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
處理查詢結果,如果是 Collection 的話,第一筆沒處理就不處理了
parent
32d321b4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
16 deletions
+28
-16
src/main/java/org/ylhealth/ym/springtest/hibernate/I18NPostProcessor.java
...g/ylhealth/ym/springtest/hibernate/I18NPostProcessor.java
+28
-16
No files found.
src/main/java/org/ylhealth/ym/springtest/hibernate/I18NPostProcessor.java
View file @
2260f545
...
...
@@ -23,10 +23,7 @@ import org.springframework.context.i18n.LocaleContextHolder;
import
com.google.common.cache.Cache
;
import
com.google.common.cache.CacheBuilder
;
/**
* Spring data 取出後進行多國語系的替換
* 快取多語資料 30 分鐘
*/
/** Spring data 取出後進行多國語系的替換 快取多語資料 30 分鐘 */
public
class
I18NPostProcessor
implements
MethodInterceptor
{
private
Logger
logger
=
LoggerFactory
.
getLogger
(
getClass
());
private
EntityManager
em
;
...
...
@@ -45,19 +42,32 @@ public class I18NPostProcessor implements MethodInterceptor {
process
(
result
);
return
result
;
}
/**
* 處理查詢結果,如果是 Collection 的話,第一筆沒處理就不處理了
* @param result
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private
void
process
(
Object
result
)
throws
IllegalAccessException
,
InvocationTargetException
{
if
(
result
instanceof
Collection
)
{
for
(
Object
entity
:
(
Collection
)
result
)
{
processEntity
(
entity
)
;
if
(
result
instanceof
Collection
)
{
for
(
Object
entity
:
(
Collection
)
result
)
{
if
(!
processEntity
(
entity
))
break
;
}
}
else
{
processEntity
(
result
);
}
}
private
void
processEntity
(
Object
entity
)
/**
* @param entity
* @return 是否有處理
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private
boolean
processEntity
(
Object
entity
)
throws
IllegalAccessException
,
InvocationTargetException
{
boolean
result
=
false
;
I18NTranslates
i18n
=
entity
.
getClass
().
getAnnotation
(
I18NTranslates
.
class
);
if
(
i18n
!=
null
)
{
logger
.
trace
(
"I18n process: {}"
,
entity
);
...
...
@@ -75,11 +85,12 @@ public class I18NPostProcessor implements MethodInterceptor {
}
}
}
result
=
true
;
}
return
result
;
}
/**
*
* @param i18n
* @param entity
* @param clazz
...
...
@@ -93,19 +104,20 @@ public class I18NPostProcessor implements MethodInterceptor {
String
language
=
LocaleContextHolder
.
getLocale
().
toString
();
String
table
=
i18n
.
table
();
String
idColumn
=
i18n
.
id
();
Map
<
String
,
Map
<
String
,
String
>>
translateTable
=
translateTable
(
table
,
language
,
i18n
.
mapping
(),
idColumn
);
Map
<
String
,
Map
<
String
,
String
>>
translateTable
=
translateTable
(
table
,
language
,
i18n
.
mapping
(),
idColumn
);
return
translateTable
.
get
(
id
);
}
private
Map
<
String
,
Map
<
String
,
String
>>
translateTable
(
String
table
,
String
language
,
I18NTranslate
[]
mapping
,
String
idColumn
)
{
private
Map
<
String
,
Map
<
String
,
String
>>
translateTable
(
String
table
,
String
language
,
I18NTranslate
[]
mapping
,
String
idColumn
)
{
String
key
=
"Table:"
+
table
+
"_"
+
language
;
Map
<
String
,
Map
<
String
,
String
>>
result
=
cache
.
getIfPresent
(
key
);
if
(
result
==
null
)
{
logger
.
trace
(
"translate query, table: {}"
,
table
);
String
sql
=
String
.
format
(
"select * from %s where languageCode = ?"
,
table
);
String
sql
=
String
.
format
(
"select * from %s where languageCode = ?"
,
table
);
Query
query
=
em
.
createNativeQuery
(
sql
);
SQLQuery
query1
=
query
.
unwrap
(
org
.
hibernate
.
SQLQuery
.
class
);
query1
.
setResultTransformer
(
Transformers
.
ALIAS_TO_ENTITY_MAP
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment