cn.sylinx.hbatis.db.common.DefaultHbatisService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hbatis-core Show documentation
Show all versions of hbatis-core Show documentation
hbatis is a simple orm framework
package cn.sylinx.hbatis.db.common;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import cn.sylinx.hbatis.db.cache.CacheQuery;
import cn.sylinx.hbatis.db.dialect.Dialect;
import cn.sylinx.hbatis.db.mapper.QueryMapper;
import cn.sylinx.hbatis.kit.Ret;
import cn.sylinx.hbatis.plugin.datasource.JdbcResourcePlugin;
/**
* 默认HbatisService实现
*
* @author han
*
*/
public class DefaultHbatisService implements HbatisService, BaseDbOper {
/**
* DbPro对象
*/
private final DbPro dbPro;
/**
* 缓存查询对象
*/
private final CacheQuery cacheQuery;
/**
* 缓存HbatisService对象
*/
private final Map map = new WeakHashMap();
/**
* 构造器
*/
public DefaultHbatisService() {
this(JdbcResourcePlugin.DEFAULT_JDBC_RESOURCE_NAME);
}
public DefaultHbatisService(String datasourceName) {
this(DbPro.use(datasourceName));
}
/**
* 构造器
*
* @param dbPro
* DbPro对象
*/
private DefaultHbatisService(DbPro dbPro) {
this.dbPro = dbPro;
cacheQuery = createCacheQuery();
map.put(dbPro.getDataSourceName(), this);
}
@Override
public String getDatasourceName() {
return dbPro.getDataSourceName();
}
@Override
public Dialect getDialect() {
return dbPro.getDialect();
}
@Override
public T call(Callable callable) {
return dbPro.call(callable);
}
/**
* 创建缓存查询对象
*
* @param params
* @return
*/
private CacheQuery createCacheQuery(Object... params) {
return new DefaultCacheQuery(this.dbPro);
}
public HbatisService use(String jdbcResourceName) {
HbatisService dbService = map.get(jdbcResourceName);
if (dbService == null) {
synchronized (jdbcResourceName.intern()) {
createInstanceInner(jdbcResourceName);
}
dbService = map.get(jdbcResourceName);
}
return dbService;
}
private void createInstanceInner(String jdbcResourceName) {
HbatisService dbService = map.get(jdbcResourceName);
if (dbService == null) {
dbService = new DefaultHbatisService(DbPro.use(jdbcResourceName));
map.put(jdbcResourceName, dbService);
}
}
@Override
public CacheQuery withCache() {
if (cacheQuery == null) {
throw new UnsupportedOperationException("不支持缓存查询");
}
return cacheQuery;
}
protected CacheQuery getCacheQuery() {
return cacheQuery;
}
public boolean transaction(ITransaction transaction) {
return dbPro.transaction(transaction);
}
@Override
public Ret transactionWithReturn(ITransaction transaction) {
return dbPro.transactionWithReturn(transaction);
}
public List query(final String sql, final QueryMapper mapper, final Object... params) {
return dbPro.query(sql, mapper, params);
}
public T queryFirst(final String sql, final QueryMapper mapper, final Object... params) {
return dbPro.queryFirst(sql, mapper, params);
}
public List