package com.newvideo.service.imp;
|
|
import java.io.Serializable;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import javax.annotation.Resource;
|
|
import org.hibernate.HibernateException;
|
import org.hibernate.Session;
|
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.orm.hibernate4.HibernateCallback;
|
import org.springframework.stereotype.Service;
|
|
import com.newvideo.dao.ConfigDao;
|
import com.newvideo.domain.Config;
|
|
@Service
|
public class ConfigService {
|
@Resource
|
private ConfigDao configDao;
|
|
public ConfigDao getConfigDao() {
|
return configDao;
|
}
|
|
public void setConfigDao(ConfigDao configDao) {
|
this.configDao = configDao;
|
}
|
|
// 获取配置文件
|
public List<Config> getConfig() {
|
return configDao.list("from Config");
|
}
|
|
@Cacheable(value = "homeCache", key = "'getConfigAsMap'")
|
public Map<String, String> getConfigAsMap() {
|
Map<String, String> map = new HashMap<String, String>();
|
List<Config> list = getConfig();
|
for (Config cg : list) {
|
map.put(cg.getKey(), cg.getValue());
|
}
|
return map;
|
}
|
|
public Config getConfigByKey(String key) {
|
List<Config> list = configDao.list("from Config cf where cf.key=?", new Serializable[] { key });
|
if (list != null && list.size() > 0)
|
return list.get(0);
|
else
|
return null;
|
}
|
|
// 更新某个配置文件
|
public void updateConfig(final Config cg) {
|
configDao.excute(new HibernateCallback() {
|
public Object doInHibernate(Session session) throws HibernateException {
|
try {
|
session.getTransaction().begin();
|
session.createSQLQuery("update wk_video_config set `value`=? where id=?")
|
.setParameter(0, cg.getValue()).setParameter(1, cg.getId()).executeUpdate();
|
session.flush();
|
session.getTransaction().commit();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
});
|
}
|
|
public boolean updateConfigList(final List<Config> cgs) {
|
return (Boolean) configDao.excute(new HibernateCallback() {
|
public Object doInHibernate(Session session) throws HibernateException {
|
try {
|
session.getTransaction().begin();
|
for (Config cg : cgs) {
|
session.update(cg);
|
}
|
session.flush();
|
session.clear();
|
session.getTransaction().commit();
|
} catch (Exception e) {
|
e.printStackTrace();
|
session.getTransaction().rollback();
|
return false;
|
}
|
return true;
|
}
|
});
|
}
|
}
|