Commit 32d321b4 authored by 楊慶堂's avatar 楊慶堂

多語檔一次讀出來

parent 7c1f5587
...@@ -25,7 +25,7 @@ import com.google.common.cache.CacheBuilder; ...@@ -25,7 +25,7 @@ import com.google.common.cache.CacheBuilder;
/** /**
* Spring data 取出後進行多國語系的替換 * Spring data 取出後進行多國語系的替換
* * 快取多語資料 30 分鐘
*/ */
public class I18NPostProcessor implements MethodInterceptor { public class I18NPostProcessor implements MethodInterceptor {
private Logger logger = LoggerFactory.getLogger(getClass()); private Logger logger = LoggerFactory.getLogger(getClass());
...@@ -93,29 +93,34 @@ public class I18NPostProcessor implements MethodInterceptor { ...@@ -93,29 +93,34 @@ public class I18NPostProcessor implements MethodInterceptor {
String language = LocaleContextHolder.getLocale().toString(); String language = LocaleContextHolder.getLocale().toString();
String table = i18n.table(); String table = i18n.table();
String idColumn = i18n.id(); String idColumn = i18n.id();
Map<String, Map<String, String>> translateTable = translateTable(table, language, i18n.mapping(), idColumn);
String key = table + "_" + id + "_" + language; return translateTable.get(id);
Map<String, String> result = cache.getIfPresent(key); }
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) { if (result == null) {
logger.trace("translate query, table: {}", table); logger.trace("translate query, table: {}", table);
String sql = String sql =
String.format("select * from %s where %s = ? and languageCode = ?", table, idColumn); String.format("select * from %s where languageCode = ?", table);
Query query = em.createNativeQuery(sql); Query query = em.createNativeQuery(sql);
SQLQuery query1 = query.unwrap(org.hibernate.SQLQuery.class); SQLQuery query1 = query.unwrap(org.hibernate.SQLQuery.class);
query1.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); query1.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
for (I18NTranslate i18nTranslate : i18n.mapping()) { query1.addScalar(idColumn);
for (I18NTranslate i18nTranslate : mapping) {
query1.addScalar(i18nTranslate.column()); query1.addScalar(i18nTranslate.column());
} }
query.setParameter(1, id); query.setParameter(1, language);
query.setParameter(2, language);
List<Map<String, String>> result1 = query.getResultList(); List<Map<String, String>> result1 = query.getResultList();
if (!result1.isEmpty()) { result = new HashMap<>();
result = result1.get(0); for (Map<String, String> map : result1) {
cache.put(key, result); result.put(map.get(idColumn), map);
} else }
cache.put(key, new HashMap<>()); cache.put(key, result);
} }
return result; return result;
} }
......
...@@ -9,6 +9,14 @@ import org.springframework.data.repository.core.RepositoryInformation; ...@@ -9,6 +9,14 @@ import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.support.RepositoryFactorySupport; import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.data.repository.core.support.RepositoryProxyPostProcessor; import org.springframework.data.repository.core.support.RepositoryProxyPostProcessor;
/**
* 將 Spring JPA 的讀取套上 i18n 轉換
* @author scott.yang
*
* @param <R>
* @param <T>
* @param <I>
*/
public class I18NRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable> public class I18NRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable>
extends JpaRepositoryFactoryBean<R, T, I> { extends JpaRepositoryFactoryBean<R, T, I> {
......
...@@ -9,7 +9,7 @@ import java.lang.annotation.Target; ...@@ -9,7 +9,7 @@ import java.lang.annotation.Target;
@Target({ElementType.TYPE}) @Target({ElementType.TYPE})
public @interface I18NTranslates { public @interface I18NTranslates {
/** /**
* 對應的 id * 翻譯表格對應的資料 id 欄位
* *
* @return * @return
*/ */
...@@ -23,7 +23,7 @@ public @interface I18NTranslates { ...@@ -23,7 +23,7 @@ public @interface I18NTranslates {
String table() default ""; String table() default "";
/** /**
* 資料對應 * 要翻譯的資料對應
*/ */
I18NTranslate[] mapping(); I18NTranslate[] mapping();
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment