/**
* Created by hdwang on 2017/8/7.
*
* hibernate拦截器:表名替换
*/
public class AutoTableNameInterceptor extends EmptyInterceptor {
private String srcName = StringUtils.EMPTY; //源表名
private String destName = StringUtils.EMPTY; // 目标表名
public AutoTableNameInterceptor() {}
public AutoTableNameInterceptor(String srcName,String destName){
this.srcName = srcName;
this.destName = destName;
}
@Override
public String onPrepareStatement(String sql) {
if(srcName.equals(StringUtils.EMPTY) || destName.equals(StringUtils.EMPTY)){
return sql;
}
sql = sql.replaceAll(srcName, destName);
return sql;
}
}
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" ref="defaultDataSource"></property>
<property name="packagesToScan">
<list>
<value>com.my.pay.task.entity</value>
<value>com.my.pay.paycms.entity</value>
<value>com.my.pay.data.entity.payincome</value>
</list>
</property>
<property name="mappingLocations">
<list>
<value>classpath*:/hibernate/hibernate-sql.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<!-- 开启查询缓存 -->
<prop key="hibernate.cache.use_query_cache">false</prop>
<!-- 配置二级缓存 -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<!-- 强制Hibernate以更人性化的格式将数据存入二级缓存 -->
<prop key="hibernate.cache.use_structured_entries">true</prop>
<!-- Hibernate将收集有助于性能调节的统计数据 -->
<prop key="hibernate.generate_statistics">false</prop>
<!-- 指定缓存配置文件位置 -->
<prop key="hibernate.cache.provider_configuration_file_resource_path">/spring/ehcache.xml</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.current_session_context_class">jta</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory</prop>
<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
</props>
</property>
</bean>
public class LocalSessionFactoryBean extends HibernateExceptionTranslator
implements FactoryBean<SessionFactory>, ResourceLoaderAware, InitializingBean, DisposableBean {
private DataSource dataSource;
private Resource[] configLocations;
private String[] mappingResources;
private Resource[] mappingLocations;
private Resource[] cacheableMappingLocations;
private Resource[] mappingJarLocations;
private Resource[] mappingDirectoryLocations;
private Interceptor entityInterceptor;
private NamingStrategy namingStrategy;
private Object jtaTransactionManager;
private Object multiTenantConnectionProvider;
private Object currentTenantIdentifierResolver;
private RegionFactory cacheRegionFactory;
private Properties hibernateProperties;
private Class<?>[] annotatedClasses;
private String[] annotatedPackages;
private String[] packagesToScan;
private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
private Configuration configuration;
private SessionFactory sessionFactory;
/**
* Open a {@link Session}.
* <p/>
* JDBC {@link Connection connection(s} will be obtained from the
* configured {@link org.hibernate.service.jdbc.connections.spi.ConnectionProvider} as needed
* to perform requested work.
*
* @return The created session.
*
* @throws HibernateException Indicates a problem opening the session; pretty rare here.
*/
public Session openSession() throws HibernateException;
/**
* Obtains the current session. The definition of what exactly "current"
* means controlled by the {@link org.hibernate.context.spi.CurrentSessionContext} impl configured
* for use.
* <p/>
* Note that for backwards compatibility, if a {@link org.hibernate.context.spi.CurrentSessionContext}
* is not configured but JTA is configured this will default to the {@link org.hibernate.context.internal.JTASessionContext}
* impl.
*
* @return The current session.
*
* @throws HibernateException Indicates an issue locating a suitable current session.
*/
public Session getCurrentSession() throws HibernateException;
<property name="entityInterceptor"> <bean class="com.my.pay.common.AutoTableNameInterceptor"/> </property>
@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;
protected Session getSession(){
if(autoTableNameInterceptorThreadLocal.get() == null){
return this.sessionFactory.getCurrentSession();
}else{
SessionBuilder builder = this.sessionFactory.withOptions().interceptor(autoTableNameInterceptorThreadLocal.get());
Session session = builder.openSession();
return session;
}
}
/**
* 线程域变量,高效实现线程安全(一个请求对应一个thread)
*/
private ThreadLocal<AutoTableNameInterceptor> autoTableNameInterceptorThreadLocal = new ThreadLocal<>();
public List<WfPayLog> find(Long merchantId, Long poolId,String sdk, Long appId,String province,
Integer price,
String serverOrder, String imsi,Integer iscallback,String state,
Date start, Date end, Paging paging) {
。。。。
//定制表名拦截器,设置到线程域
autoTableNameInterceptorThreadLocal.set(new AutoTableNameInterceptor("wf_pay_log","wf_pay_log_"+ DateUtil.formatDate(start,DateUtil.YEARMONTH_PATTERN)));
List<WfPayLog> wfPayLogs;
if (paging == null) {
wfPayLogs = (List<WfPayLog>) find(hql.toString(), params); //find方法里面有 this.getSession().createQuery("hql") 等方法
} else {
wfPayLogs = (List<WfPayLog>) findPaging(hql.toString(), "select count(*) " + hql.toString(), params, paging);
}
return wfPayLogs;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有