cn.sylinx.hbatis.ext.mirage.DefaultMirageService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hbatis-mirage Show documentation
Show all versions of hbatis-mirage Show documentation
hbatis mirage is a simple orm framework, extends hbatis-core
package cn.sylinx.hbatis.ext.mirage;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import cn.sylinx.hbatis.db.common.DbOper;
import cn.sylinx.hbatis.db.common.DefaultHbatisService;
import cn.sylinx.hbatis.db.common.Record;
import cn.sylinx.hbatis.db.mapper.ModelBuilder;
import cn.sylinx.hbatis.db.mapper.QueryMapper;
import cn.sylinx.hbatis.ext.parse.SqlParser;
import cn.sylinx.hbatis.ext.res.ClasspathSqlResource;
import cn.sylinx.hbatis.kit.Tuple;
import cn.sylinx.hbatis.plugin.JdbcResourcePlugin;
/**
* MirageService, 读取类路径下的sql文件
*
* @author han
*
*/
public class DefaultMirageService extends DefaultHbatisService implements MirageService {
/**
* 数据源名称
*/
private String datasourceName;
/**
* 缓存支持
*/
private final MirageCacheQuery mirageCacheQuery;
/**
* 多数据源 MirageService缓存
*/
private final Map dbServiceMap = new WeakHashMap();
public DefaultMirageService() {
this(JdbcResourcePlugin.DEFAULT_JDBC_RESOURCE_NAME);
}
public DefaultMirageService(String datasourceName) {
super(datasourceName);
this.datasourceName = datasourceName;
this.mirageCacheQuery = createMirageCacheQuery();
dbServiceMap.put(datasourceName, this);
}
@Override
public MirageCacheQuery withMirageCache() {
if (getCacheQuery() == null || mirageCacheQuery == null) {
throw new UnsupportedOperationException("不支持缓存查询");
}
return mirageCacheQuery;
}
/**
* 创建缓存查询对象
*
* @param params
* @return
*/
private MirageCacheQuery createMirageCacheQuery(Object... params) {
return new DefaultMirageCacheQuery(getCacheQuery());
}
@Override
public DbOper getDbOper() {
return this;
}
public MirageService useService(String datasourceName) {
MirageService dbService = dbServiceMap.get(datasourceName);
if (dbService == null) {
synchronized (datasourceName.intern()) {
createInstanceInner(datasourceName);
}
dbService = dbServiceMap.get(datasourceName);
}
return dbService;
}
private void createInstanceInner(String datasourceName) {
MirageService dbService = dbServiceMap.get(datasourceName);
if (dbService == null) {
dbService = createInstance(datasourceName);
dbServiceMap.put(datasourceName, dbService);
}
}
/**
* 创建一个MirageService
*
* @param datasourceName
* @return
*/
protected MirageService createInstance(String datasourceName) {
return new DefaultMirageService(datasourceName);
}
@Override
public List query(ClasspathSqlResource sqlResource, Map params, Class clz) {
Tuple tp = SqlParser.parseSql(sqlResource, params);
String st = (String) tp.get(0);
Object[] pms = (Object[]) tp.get(1);
QueryMapper mapper = ModelBuilder.buildQueryMapper(clz);
return useService(datasourceName).query(st, mapper, pms);
}
@Override
public List