1. 程式人生 > >Dorado 支援多資料來源(QyjHibernateDao)

Dorado 支援多資料來源(QyjHibernateDao)

DBConfig.properties

driver = oracle.jdbc.driver.OracleDriver

url = jdbc:oracle:thin:@127.0.0.1:1521:ORCL

username = qyj

password = qyj

 

QyjHibernateDao.java

package com.qyj.framework.dao;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.util.List;

import java.util.Map;

import java.util.Properties;

 

import org.apache.commons.lang.StringUtils;

import org.hibernate.Query;

import org.hibernate.Session;

import org.springframework.stereotype.Repository;

 

import com.bstek.dorado.data.provider.Page;

import com.gs.mcf.common.McfBaseDAO;

 

/***

 * QyjHibernateDao 多資料來源支援

 * 建立日期: 20151125

 * @author like12

 * @param <T>

 * 

 */

@SuppressWarnings("rawtypes")

@Repository

public class QyjHibernateDao<T> extends McfBaseDAO {

//like12 add,20151224,全域性Session

private static Session sessionGlobal = null;

/**

 * 儲存

 * @param entity

 */

public void saveQyj(T entity) {

String entityName = this.getEntityName(entity);

Session session = this.getSessionQyj();

session.saveOrUpdate(entityName, entity);

session.flush();

session.clear();

}

 

/**

 * 修改

 * 

 * @param hql

 * @param params

 */

public void excuteUpdate(final String hql, Object[] params) {

Query query = this.getSessionQyj().createQuery(hql);

if (params != null) {

int i = 0;

for (Object o : params) {

query.setParameter(i, o);

i++;

}

}

query.executeUpdate();

}

/**

 * like12 add,20160414

 * 全部查詢

 * @param hql

 * @param parameters

 * @return

 */

@SuppressWarnings("unchecked")

public <X> List<X> findQyjAll(String hql, Map<String, ?> parameters) {

return createQueryQyj(hql, parameters).list();

}

/**

 * 分頁查詢

 * @param page

 * @param hql

 * @param parameters

 * @return

 */

@SuppressWarnings("unchecked")

public Page<T> findQyj(Page<T> page, String hql, Map<String, ?> parameters) {

notNull(page, "page");

 

Query q = this.createQueryQyj(hql, parameters);

long totalCount = countHqlResultQyj(hql, parameters);

page.setEntityCount((int) totalCount);

setPageParameterToQuery(q, page);

page.setEntities(q.list());

return page;

}

public Query createQueryQyj(String queryString, Map<String, ?> parameters) {

Query query = this.getSessionQyj().createQuery(queryString);

if (parameters != null) {

query.setProperties(parameters);

}

return query;

}

protected long countHqlResultQyj(String hql, Map<String, ?> parameters) {

String countHql = generateCountHqlQyj(hql);

return ((Number) findUniqueQyj(countHql, parameters)).longValue();

}

private String generateCountHqlQyj(String hql) {

hql = "from " + StringUtils.substringAfter(hql, "from");

hql = StringUtils.substringBefore(hql, "order by");

String countHql = "select count(*) " + hql;

return countHql;

}

@SuppressWarnings("unchecked")

public <X> X findUniqueQyj(String hql, Map<String, ?> parameters) {

return (X) createQueryQyj(hql, parameters).uniqueResult();

}

/**

 * 獲取多資料來源Session

 * @return

 */

public Session getSessionQyj() {

//like12 modofied,20151224,每一次 增刪改查 都要產生一次新的Session 資料庫會很快死掉

if(sessionGlobal == null){

Properties prop = new Properties();

String driver = null;

String url = null;

String username = null;

String password = null;

Connection connection = null;

try{

//讀配置檔案

prop.load(this.getClass().getClassLoader().getResourceAsStream(

"DBConfig.properties"));

driver = prop.getProperty("driver");

url = prop.getProperty("url");

username = prop.getProperty("username");

password = prop.getProperty("password");

Class.forName(driver);//1.載入驅動

connection = DriverManager.getConnection(url,username,password);//2.得到連線

}catch (Exception e){

e.printStackTrace();

}

//中途攔截,正規預設為:this.sessionFactory.openSession()

sessionGlobal = this.sessionFactory.openSession(connection);

}

//like12 add,20151228,bug,需清除上一次操作的遺留資訊,否則編輯不了

sessionGlobal.clear();

return sessionGlobal;

}

}

 

AppAdsDAO.java

package com.qyj.framework.dao;

 

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

 

import javax.annotation.Resource;

 

import org.springframework.stereotype.Repository;

 

import com.bstek.dorado.data.provider.Page;

import com.gs.mcf.common.Constants;

import com.gs.mcf.common.McfBaseDAO;

import com.qyj.framework.entity.AppAds;

import com.qyj.framework.service.AppAdsDS;

 

/***

 * AppAds 資料庫持久類

 * 建立日期: 20151124

 * @author like12

 * 

 */

@Repository

public class AppAdsDAO extends McfBaseDAO<AppAds, String> {

//like12 add,20151124,獲取this.getCurrentEmployee()

@Resource

AppAdsDS appAdsDS;

@Resource

PlatformDAO platformDAO;

@SuppressWarnings("rawtypes")

@Resource

QyjHibernateDao qyjHibernateDao;

/**

 * 分頁查詢

 * @param page

 * @param parameter

 */

@SuppressWarnings({ "rawtypes", "unchecked" })

public void findAppAds(Page<AppAds> page, Map<String, Object> parameter) {

StringBuilder hql = new StringBuilder(" from AppAds where enabled=1 " +

" order by adId desc ");

Map params = new HashMap();

qyjHibernateDao.findQyj(page, hql.toString(), params);

}

/**

 * 查詢ById

 * @param id

 * @return

 */

@SuppressWarnings("unchecked")

public AppAds findAppAdsById(int id) {

StringBuilder hql = new StringBuilder(" from AppAds where enabled=1 and adId=" + id + " ");

return (AppAds)qyjHibernateDao.findUniqueQyj(hql.toString(), null);

}

/**

 * 儲存

 * @param appAds

 * @param parameter

 * @return

 */

@SuppressWarnings("unchecked")

public String saveAppAds(AppAds appAds, Map<String, Object> parameter) {

//xjn add,"蘋果客戶端,安卓客戶端"轉為"2,1"

String platformname = appAds.getPlatform();

String[] platformArray = null;   

String platformValue = null;

StringBuffer buf = new StringBuffer();  

platformArray = platformname.split(",");

for(int i=0;i<platformArray.length;i++){

int platValue = (platformDAO.findPlatformByName(platformArray[i])).getPlatformValue();

if(platValue != 0){

buf.append(platValue);

buf.append(",");  

}

}

platformValue = buf.substring(0,buf.length()-1).toString();

appAds.setPlatform(platformValue);

appAds.setEnabled(1);

appAds.setCreatedBy(appAdsDS.getCurrentEmployee().getEmployeeName());

appAds.setCreatedDate(new Date());

qyjHibernateDao.saveQyj(appAds);

return "success";

}

/**

 * 刪除

 * @param id

 * @return

 */

public String deleteAppAdsById(int id) {

qyjHibernateDao.excuteUpdate(" update AppAds set enabled=0 where adId = ? ", new Object[]{ id });

return Constants.success;

}

}