@@ -1,8 +1,10 @@ | |||||
package cc.smtweb.framework.core.db; | package cc.smtweb.framework.core.db; | ||||
import cc.smtweb.framework.core.db.impl.BaseBean; | |||||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | import cc.smtweb.framework.core.db.impl.DefaultEntity; | ||||
import cc.smtweb.framework.core.db.jdbc.IdGenerator; | import cc.smtweb.framework.core.db.jdbc.IdGenerator; | ||||
import cc.smtweb.framework.core.db.jdbc.JdbcEngine; | import cc.smtweb.framework.core.db.jdbc.JdbcEngine; | ||||
import cc.smtweb.framework.core.mvc.controller.scan.BeanManager; | |||||
import cc.smtweb.framework.core.util.SpringUtil; | import cc.smtweb.framework.core.util.SpringUtil; | ||||
import lombok.extern.slf4j.Slf4j; | import lombok.extern.slf4j.Slf4j; | ||||
import org.springframework.jdbc.core.JdbcTemplate; | import org.springframework.jdbc.core.JdbcTemplate; | ||||
@@ -52,8 +54,12 @@ public class DbEngine extends JdbcEngine { | |||||
return handler; | return handler; | ||||
} | } | ||||
public EntityDao<DefaultEntity> findDao(String tableName) { | |||||
EntityDao<DefaultEntity> handler = tableDaoMap.get(type); | |||||
public EntityDao findDao(String tableName) { | |||||
Class<?> clazz = BeanManager.getInstance().getTableClass(tableName); | |||||
if (clazz != null) { | |||||
return findDao(clazz); | |||||
} | |||||
EntityDao<DefaultEntity> handler = tableDaoMap.get(tableName); | |||||
if (handler == null) { | if (handler == null) { | ||||
synchronized (tableDaoMap) { | synchronized (tableDaoMap) { | ||||
@@ -1,5 +1,6 @@ | |||||
package cc.smtweb.framework.core.db; | package cc.smtweb.framework.core.db; | ||||
import cc.smtweb.framework.core.SwException; | |||||
import cc.smtweb.framework.core.db.dao.AbstractEntityDao; | import cc.smtweb.framework.core.db.dao.AbstractEntityDao; | ||||
import cc.smtweb.framework.core.db.dao.EntityColumn; | import cc.smtweb.framework.core.db.dao.EntityColumn; | ||||
import cc.smtweb.framework.core.db.jdbc.JdbcEngine; | import cc.smtweb.framework.core.db.jdbc.JdbcEngine; | ||||
@@ -220,16 +221,6 @@ public class EntityDao<T> extends AbstractEntityDao<T> { | |||||
} | } | ||||
/** | /** | ||||
* 读取实体对象ID值 | |||||
* | |||||
* @param entity | |||||
* @return | |||||
*/ | |||||
public Long readEntityId(T entity) { | |||||
return (Long) readId(entity); | |||||
} | |||||
/** | |||||
* 查询单行数据,返回bean | * 查询单行数据,返回bean | ||||
*/ | */ | ||||
public T queryEntity(Long id) { | public T queryEntity(Long id) { | ||||
@@ -278,4 +269,20 @@ public class EntityDao<T> extends AbstractEntityDao<T> { | |||||
return jdbcEngine.query(sb.toString(), type, params); | return jdbcEngine.query(sb.toString(), type, params); | ||||
} | } | ||||
public void checkUnique(T bean, String... fields) { | |||||
if (fields.length == 0) return; | |||||
String ss = "", sTitle = ""; | |||||
List<Object> args = new ArrayList<>(fields.length + 1); | |||||
args.add(readId(bean)); | |||||
for (String f: fields) { | |||||
if (StringUtils.isEmpty(f)) continue; | |||||
ss += " and " + f + "=?"; | |||||
args.add(readValue(bean, f)); | |||||
sTitle += "+" + modelTable.getFieldTitle(f); | |||||
} | |||||
if (jdbcEngine.isExists("select 1 from " + tableName + " where " + modelTable.getIdField() + "=? " + ss, args.toArray())) | |||||
throw new SwException(sTitle.substring(1) + " 不能重复!"); | |||||
} | |||||
} | } |
@@ -21,6 +21,7 @@ import java.util.Set; | |||||
public class ModelTableCache extends AbstractCache<ModelTable> { | public class ModelTableCache extends AbstractCache<ModelTable> { | ||||
private final static String mk = "k"; | private final static String mk = "k"; | ||||
private final static String md = "d"; | private final static String md = "d"; | ||||
private final static String mf = "l"; | |||||
public static ModelTableCache getInstance() { | public static ModelTableCache getInstance() { | ||||
return CacheManager.getIntance().getCache(ModelTableCache.class); | return CacheManager.getIntance().getCache(ModelTableCache.class); | ||||
@@ -29,6 +30,7 @@ public class ModelTableCache extends AbstractCache<ModelTable> { | |||||
public ModelTableCache() { | public ModelTableCache() { | ||||
regMap(mk, k-> k.getName().toUpperCase()); | regMap(mk, k-> k.getName().toUpperCase()); | ||||
regList(md, k-> String.valueOf(k.getDatabaseId())); | regList(md, k-> String.valueOf(k.getDatabaseId())); | ||||
regList(mf, k-> k.get); | |||||
} | } | ||||
@Override | @Override | ||||
@@ -84,7 +84,7 @@ public abstract class AbstractEntityDao<T> { | |||||
*/ | */ | ||||
public AbstractEntityDao(String tableName) { | public AbstractEntityDao(String tableName) { | ||||
this.tableName = tableName; | this.tableName = tableName; | ||||
this.type = (Class<T>)DefaultEntity.class; | |||||
this.type = (Class<T>) DefaultEntity.class; | |||||
modelTable = cacheManager.getCache(ModelTableCache.class).getByName(tableName); | modelTable = cacheManager.getCache(ModelTableCache.class).getByName(tableName); | ||||
for (ModelField field : modelTable.getFields()) { | for (ModelField field : modelTable.getFields()) { | ||||
@@ -94,7 +94,8 @@ public abstract class AbstractEntityDao<T> { | |||||
/** | /** | ||||
* 根据字段名,得到类中的字段属性名 | * 根据字段名,得到类中的字段属性名 | ||||
* @param tbAbbr 表缩写,一般是字段的前缀 | |||||
* | |||||
* @param tbAbbr 表缩写,一般是字段的前缀 | |||||
* @param fieldName 字段名 | * @param fieldName 字段名 | ||||
* @return | * @return | ||||
*/ | */ | ||||
@@ -305,7 +306,7 @@ public abstract class AbstractEntityDao<T> { | |||||
sql.append("DELETE FROM ").append(tableName).append(" WHERE ").append(idColumn.getField().getName()).append("=?"); | sql.append("DELETE FROM ").append(tableName).append(" WHERE ").append(idColumn.getField().getName()).append("=?"); | ||||
} | } | ||||
private Object readValue(T obj, String fieldName) { | |||||
protected Object readValue(T obj, String fieldName) { | |||||
EntityColumn beanColumn = getBeanColumn(fieldName); | EntityColumn beanColumn = getBeanColumn(fieldName); | ||||
return beanColumn.readValue(obj); | return beanColumn.readValue(obj); | ||||
@@ -379,5 +380,9 @@ public abstract class AbstractEntityDao<T> { | |||||
return (Long) idColumn.readValue(entity); | return (Long) idColumn.readValue(entity); | ||||
} | } | ||||
public T createBean() throws Exception { | |||||
return this.type.newInstance(); | |||||
} | |||||
} | } | ||||
@@ -1,5 +1,6 @@ | |||||
package cc.smtweb.framework.core.db.impl; | package cc.smtweb.framework.core.db.impl; | ||||
import cc.smtweb.framework.core.SwMap; | |||||
import cc.smtweb.framework.core.util.JsonUtil; | import cc.smtweb.framework.core.util.JsonUtil; | ||||
import cc.smtweb.framework.core.util.NumberUtil; | import cc.smtweb.framework.core.util.NumberUtil; | ||||
import com.fasterxml.jackson.databind.JsonNode; | import com.fasterxml.jackson.databind.JsonNode; | ||||
@@ -89,10 +90,14 @@ public class BaseBean implements Serializable { | |||||
return this.data.get(fieldName); | return this.data.get(fieldName); | ||||
} | } | ||||
public void readFromJson(String json){ | |||||
public void readFromJson(String json) { | |||||
Map map = JsonUtil.parseMap(json); | Map map = JsonUtil.parseMap(json); | ||||
if (map != null) { | if (map != null) { | ||||
data.putAll(map); | data.putAll(map); | ||||
} | } | ||||
} | } | ||||
public void readFromMap(SwMap map) { | |||||
data.putAll(map); | |||||
} | |||||
} | } |
@@ -388,6 +388,10 @@ public class JdbcEngine { | |||||
return jdbcTemplate.query(sql + " LIMIT " + start + "," + limit, createRowMapper(type)); | return jdbcTemplate.query(sql + " LIMIT " + start + "," + limit, createRowMapper(type)); | ||||
} | } | ||||
public boolean isExists(String sql, Object... params) { | |||||
return jdbcTemplate.queryForObject(sql + " LIMIT 0, 1", int.class, params) > 0; | |||||
} | |||||
private <T> RowMapper<T> createRowMapper(Class<T> type) { | private <T> RowMapper<T> createRowMapper(Class<T> type) { | ||||
RowMapper<T> rowMapper; | RowMapper<T> rowMapper; | ||||
if (java.util.Map.class.isAssignableFrom(type)) { | if (java.util.Map.class.isAssignableFrom(type)) { | ||||
@@ -35,4 +35,8 @@ public class ModelField { | |||||
private String link; | private String link; | ||||
//控件类型:TEXT/TextArea/NUMBER/COMBO | //控件类型:TEXT/TextArea/NUMBER/COMBO | ||||
private String editor; | private String editor; | ||||
public boolean isNotNull() { | |||||
return notNull == 1; | |||||
} | |||||
} | } |
@@ -17,4 +17,8 @@ public class ModelIndex { | |||||
private String type; | private String type; | ||||
private String name; | private String name; | ||||
private String fields; | private String fields; | ||||
public boolean isUnique() { | |||||
return TYPE_UNIQUE.equals(type); | |||||
} | |||||
} | } |
@@ -62,6 +62,11 @@ public class ModelTable implements Serializable { | |||||
return null; | return null; | ||||
} | } | ||||
public String getFieldTitle(String fieldName) { | |||||
ModelField field = findField(fieldName); | |||||
return field != null ? field.getTitle() : fieldName; | |||||
} | |||||
public String fullName() { | public String fullName() { | ||||
return dbName + '.' + name; | return dbName + '.' + name; | ||||
} | } | ||||
@@ -23,7 +23,8 @@ import java.util.List; | |||||
public class ApplicationScanner { | public class ApplicationScanner { | ||||
public static void scan(ConfigurableApplicationContext applicationContext) throws Exception { | public static void scan(ConfigurableApplicationContext applicationContext) throws Exception { | ||||
MethodAccessManager methodAccessManager = applicationContext.getBean(MethodAccessManager.class); | MethodAccessManager methodAccessManager = applicationContext.getBean(MethodAccessManager.class); | ||||
BeanManager beanManager = new BeanManager(applicationContext); | |||||
BeanManager beanManager = BeanManager.getInstance(); | |||||
beanManager.install(applicationContext); | |||||
String[] names = applicationContext.getBeanNamesForType(ControllerConfig.class); | String[] names = applicationContext.getBeanNamesForType(ControllerConfig.class); | ||||
if (names != null) { | if (names != null) { | ||||
@@ -32,7 +32,21 @@ public class BeanManager implements IActionManager { | |||||
private List<OrderMethodAccess> destroyMethods = new ArrayList<>(); | private List<OrderMethodAccess> destroyMethods = new ArrayList<>(); | ||||
private List<IMethodAccess> beanMethods = new ArrayList<>(); | private List<IMethodAccess> beanMethods = new ArrayList<>(); | ||||
BeanManager(ApplicationContext applicationContext) { | |||||
private Map<String, Class<?>> mapTableClass = new HashMap<>(); | |||||
private static BeanManager instance = null; | |||||
static { | |||||
instance = new BeanManager(); | |||||
} | |||||
public static BeanManager getInstance() { | |||||
return instance; | |||||
} | |||||
private BeanManager() { | |||||
} | |||||
public void install(ApplicationContext applicationContext) { | |||||
this.applicationContext = applicationContext; | this.applicationContext = applicationContext; | ||||
beanContext = new BeanContext(beanMap, applicationContext); | beanContext = new BeanContext(beanMap, applicationContext); | ||||
} | } | ||||
@@ -127,6 +141,14 @@ public class BeanManager implements IActionManager { | |||||
return this.putIfAbsent(url, methodAccess) == null; | return this.putIfAbsent(url, methodAccess) == null; | ||||
} | } | ||||
public void addTableClass(String tableName, Class<?> clzz) { | |||||
mapTableClass.put(tableName, clzz); | |||||
} | |||||
public Class<?> getTableClass(String tableName) { | |||||
return mapTableClass.get(tableName); | |||||
} | |||||
private static class OrderMethodAccess { | private static class OrderMethodAccess { | ||||
private final int order; | private final int order; | ||||
private final MethodAccess methodAccess; | private final MethodAccess methodAccess; | ||||
@@ -69,28 +69,35 @@ public class ClassParser { | |||||
} else { | } else { | ||||
log.error(clazz.getName() + " must be singleton, can not use field: " + singletonField.getName()); | log.error(clazz.getName() + " must be singleton, can not use field: " + singletonField.getName()); | ||||
} | } | ||||
} else { | |||||
SwService swService = clazz.getAnnotation(SwService.class); | |||||
return; | |||||
} | |||||
if (swService != null) { | |||||
urlMaker.setServiceUrl(swService.value()); | |||||
log.debug("[smt]init service: " + clazz.getName()); | |||||
SwService swService = clazz.getAnnotation(SwService.class); | |||||
if (swService != null) { | |||||
urlMaker.setServiceUrl(swService.value()); | |||||
log.debug("[smt]init service: " + clazz.getName()); | |||||
//Service类,非单例,以免多线程并发相互干扰 | |||||
ControllerAccess controllerAccess = buildControllerAccess(clazz, false); | |||||
if (controllerAccess.isSingleton()) { | |||||
controllerAccess.newSingletonInstance(); | |||||
beanManager.addSingletonController(controllerAccess); | |||||
} | |||||
ControllerAccess controllerAccess = buildControllerAccess(clazz, false); | |||||
if (controllerAccess.isSingleton()) { | |||||
controllerAccess.newSingletonInstance(); | |||||
beanManager.addSingletonController(controllerAccess); | |||||
} | |||||
// 处理url路径 | |||||
methodParser.parse(clazz, controllerAccess, true); | |||||
return; | |||||
} | |||||
// 处理url路径 | |||||
methodParser.parse(clazz, controllerAccess, true); | |||||
} else { | |||||
SwCache swCache = clazz.getAnnotation(SwCache.class); | |||||
if (swCache != null) { | |||||
CacheManager cacheManager = CacheManager.getIntance(); | |||||
cacheManager.install((AbstractCache)clazz.newInstance()); | |||||
} | |||||
} | |||||
SwCache swCache = clazz.getAnnotation(SwCache.class); | |||||
if (swCache != null) { | |||||
CacheManager cacheManager = CacheManager.getIntance(); | |||||
cacheManager.install((AbstractCache) clazz.newInstance()); | |||||
return; | |||||
} | |||||
SwTable swTable = clazz.getAnnotation(SwTable.class); | |||||
if (swTable != null) { | |||||
beanManager.addTableClass(swTable.value(), clazz); | |||||
} | } | ||||
} | } | ||||
@@ -0,0 +1,24 @@ | |||||
package cc.smtweb.framework.core.mvc.service; | |||||
import cc.smtweb.framework.core.db.DbEngine; | |||||
import cc.smtweb.framework.core.db.EntityDao; | |||||
import java.util.HashMap; | |||||
import java.util.Map; | |||||
/** | |||||
* Created by Akmm at 2022/3/3 11:13 | |||||
* 数据提供者,线程级缓存 | |||||
*/ | |||||
public class AbstractCompProvider { | |||||
//临时缓存 | |||||
private Map<String, Object> cacheData = new HashMap<>(); | |||||
protected <T> T doGetData(String key, IDataProvider<T> loader) { | |||||
cacheData.computeIfAbsent(key, s -> loader.load()); | |||||
if (!cacheData.containsKey(key)) { | |||||
cacheData.put(key, loader.load()); | |||||
} | |||||
return (T) cacheData.get(key); | |||||
} | |||||
} |
@@ -0,0 +1,50 @@ | |||||
package cc.smtweb.framework.core.mvc.service; | |||||
import cc.smtweb.framework.core.R; | |||||
import cc.smtweb.framework.core.SwMap; | |||||
import cc.smtweb.framework.core.annotation.SwBody; | |||||
import cc.smtweb.framework.core.annotation.SwService; | |||||
import cc.smtweb.framework.core.db.DbEngine; | |||||
import cc.smtweb.framework.core.db.EntityDao; | |||||
import cc.smtweb.framework.core.session.UserSession; | |||||
import java.util.HashMap; | |||||
import java.util.Map; | |||||
/** | |||||
* Created by Akmm at 2022/3/2 10:39 | |||||
* 通用业务mvc总调度 | |||||
*/ | |||||
public abstract class AbstractCompService { | |||||
public final static String TYPE_LIST = "list"; | |||||
public final static String TYPE_LOAD = "load"; | |||||
public final static String TYPE_SAVE = "save"; | |||||
public final static String TYPE_DEL = "del"; | |||||
protected abstract IHandler createHanlder(String type); | |||||
private R pageHandler(SwMap params, UserSession us, String type) { | |||||
try { | |||||
IHandler handler = createHanlder(TYPE_SAVE); | |||||
return handler.doWork(params, us); | |||||
} catch (Exception e) { | |||||
return R.error("操作失败!", e); | |||||
} | |||||
} | |||||
public R save(@SwBody SwMap params, UserSession us) { | |||||
return pageHandler(params, us, TYPE_SAVE); | |||||
} | |||||
public R load(@SwBody SwMap params, UserSession us) { | |||||
return pageHandler(params, us, TYPE_LOAD); | |||||
} | |||||
public R del(@SwBody SwMap params, UserSession us) { | |||||
return pageHandler(params, us, TYPE_DEL); | |||||
} | |||||
public R list(@SwBody SwMap params, UserSession us) { | |||||
return pageHandler(params, us, TYPE_LIST); | |||||
} | |||||
} |
@@ -0,0 +1,52 @@ | |||||
package cc.smtweb.framework.core.mvc.service; | |||||
import cc.smtweb.framework.core.R; | |||||
import cc.smtweb.framework.core.SwMap; | |||||
import cc.smtweb.framework.core.db.DbEngine; | |||||
import cc.smtweb.framework.core.session.UserSession; | |||||
import lombok.extern.slf4j.Slf4j; | |||||
/** | |||||
* Created by Akmm at 2022/3/2 19:44 | |||||
* 保存 | |||||
*/ | |||||
@Slf4j | |||||
public abstract class AbstractDelHandler<T> implements IHandler{ | |||||
protected SwMap params; | |||||
protected UserSession us; | |||||
@Override | |||||
public R doWork(SwMap params, UserSession us) throws Exception { | |||||
this.params = params; | |||||
this.us = us; | |||||
long id = readId(); | |||||
checkValid(); | |||||
DbEngine.getInstance().doTrans(() -> { | |||||
try { | |||||
del(); | |||||
saveSuccess(); | |||||
return true; | |||||
} catch (Exception e) { | |||||
saveFailed(); | |||||
log.error("保存失败!", e); | |||||
return false; | |||||
} | |||||
}); | |||||
return R.success(); | |||||
} | |||||
/** | |||||
* 读取页面传回来的id | |||||
* @return | |||||
*/ | |||||
protected long readId() { | |||||
return params.readLong("id", 0L); | |||||
} | |||||
protected abstract void checkValid() throws Exception; | |||||
protected abstract void del() throws Exception; | |||||
protected void saveSuccess() {} | |||||
protected void saveFailed() {} | |||||
} |
@@ -0,0 +1,39 @@ | |||||
package cc.smtweb.framework.core.mvc.service; | |||||
import cc.smtweb.framework.core.R; | |||||
import cc.smtweb.framework.core.SwMap; | |||||
import cc.smtweb.framework.core.session.UserSession; | |||||
/** | |||||
* Created by Akmm at 2022/3/2 19:44 | |||||
*/ | |||||
public abstract class AbstractLoadHandler<T> implements IHandler { | |||||
protected SwMap params; | |||||
protected UserSession us; | |||||
@Override | |||||
public R doWork(SwMap params, UserSession us) throws Exception { | |||||
this.params = params; | |||||
this.us = us; | |||||
long id = readId(); | |||||
T bean; | |||||
if (id <= 0L) { | |||||
bean = createComp(); | |||||
} else { | |||||
bean = loadComp(id); | |||||
} | |||||
return R.success(bean); | |||||
} | |||||
/** | |||||
* 读取页面传回来的id | |||||
* @return | |||||
*/ | |||||
protected long readId() { | |||||
return params.readLong("id", 0L); | |||||
} | |||||
protected abstract T createComp() throws Exception; | |||||
protected abstract T loadComp(long id); | |||||
} |
@@ -0,0 +1,65 @@ | |||||
package cc.smtweb.framework.core.mvc.service; | |||||
import cc.smtweb.framework.core.R; | |||||
import cc.smtweb.framework.core.SwMap; | |||||
import cc.smtweb.framework.core.db.DbEngine; | |||||
import cc.smtweb.framework.core.session.UserSession; | |||||
import lombok.extern.slf4j.Slf4j; | |||||
/** | |||||
* Created by Akmm at 2022/3/2 19:44 | |||||
* 保存 | |||||
*/ | |||||
@Slf4j | |||||
public abstract class AbstractSaveHandler<T> implements IHandler{ | |||||
protected SwMap params; | |||||
protected UserSession us; | |||||
protected T bean; | |||||
protected boolean isNew; | |||||
@Override | |||||
public R doWork(SwMap params, UserSession us) throws Exception { | |||||
this.params = params; | |||||
this.us = us; | |||||
long id = readId(); | |||||
isNew = id <= 0L; | |||||
if (isNew) { | |||||
bean = createComp(); | |||||
} else { | |||||
bean = loadComp(id); | |||||
} | |||||
readFromPage(); | |||||
checkValid(); | |||||
DbEngine.getInstance().doTrans(() -> { | |||||
try { | |||||
save(); | |||||
saveSuccess(); | |||||
return true; | |||||
} catch (Exception e) { | |||||
saveFailed(); | |||||
log.error("保存失败!", e); | |||||
return false; | |||||
} | |||||
}); | |||||
return R.success(bean); | |||||
} | |||||
/** | |||||
* 读取页面传回来的id | |||||
* @return | |||||
*/ | |||||
protected long readId() { | |||||
return params.readLong("id", 0L); | |||||
} | |||||
protected abstract void readFromPage(); | |||||
protected abstract void checkValid() throws Exception; | |||||
protected abstract void save() throws Exception; | |||||
protected void saveSuccess() {} | |||||
protected void saveFailed() {} | |||||
protected abstract T createComp() throws Exception; | |||||
protected abstract T loadComp(long id) throws Exception; | |||||
} |
@@ -0,0 +1,63 @@ | |||||
package cc.smtweb.framework.core.mvc.service; | |||||
import cc.smtweb.framework.core.SwException; | |||||
import cc.smtweb.framework.core.cache.AbstractCache; | |||||
import cc.smtweb.framework.core.cache.CacheManager; | |||||
import cc.smtweb.framework.core.db.DbEngine; | |||||
import cc.smtweb.framework.core.db.EntityDao; | |||||
import cc.smtweb.framework.core.db.cache.ModelTableCache; | |||||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | |||||
import cc.smtweb.framework.core.db.vo.ModelField; | |||||
import cc.smtweb.framework.core.db.vo.ModelIndex; | |||||
import cc.smtweb.framework.core.db.vo.ModelTable; | |||||
import org.apache.commons.lang3.StringUtils; | |||||
/** | |||||
* Created by Akmm at 2022/3/2 19:52 | |||||
* 默认实体实现 | |||||
*/ | |||||
public class DefaultDelHandler<T extends DefaultEntity> extends AbstractDelHandler<T> { | |||||
protected String tableName; | |||||
public DefaultDelHandler(String tableName) { | |||||
this.tableName = tableName; | |||||
} | |||||
@Override | |||||
protected void checkValid() throws Exception { | |||||
ModelTable table = ModelTableCache.getInstance().getByName(tableName); | |||||
EntityDao dao = DbEngine.getInstance().findDao(tableName); | |||||
for (ModelIndex mi: table.getIndexes()) { | |||||
if (mi.isUnique()) { | |||||
dao.checkUnique(bean, mi.getFields().split(",")); | |||||
} | |||||
} | |||||
} | |||||
@Override | |||||
protected void del() { | |||||
EntityDao dao = DbEngine.getInstance().findDao(tableName); | |||||
dao.deleteEntity(id); | |||||
} | |||||
@Override | |||||
protected void saveSuccess() { | |||||
super.saveSuccess(); | |||||
ModelTable table = ModelTableCache.getInstance().getByName(tableName); | |||||
if (table.isNeedCache()) { | |||||
AbstractCache<T> cache = CacheManager.getIntance().getCache(tableName); | |||||
cache.put(bean); | |||||
} | |||||
} | |||||
@Override | |||||
protected void saveFailed() { | |||||
super.saveFailed(); | |||||
ModelTable table = ModelTableCache.getInstance().getByName(tableName); | |||||
if (table.isNeedCache()) { | |||||
AbstractCache<T> cache = CacheManager.getIntance().getCache(tableName); | |||||
cache.reset(bean); | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,26 @@ | |||||
package cc.smtweb.framework.core.mvc.service; | |||||
import cc.smtweb.framework.core.db.DbEngine; | |||||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | |||||
/** | |||||
* Created by Akmm at 2022/3/2 19:52 | |||||
* 默认实体实现 | |||||
*/ | |||||
public class DefaultLoadHandler<T extends DefaultEntity> extends AbstractLoadHandler<T> { | |||||
protected String tableName; | |||||
public DefaultLoadHandler(String tableName) { | |||||
this.tableName = tableName; | |||||
} | |||||
@Override | |||||
protected T createComp() throws Exception { | |||||
return (T)DbEngine.getInstance().findDao(tableName).createBean(); | |||||
} | |||||
@Override | |||||
protected T loadComp(long id) { | |||||
return new DefaultProvider<T>(tableName).getBean(id); | |||||
} | |||||
} |
@@ -0,0 +1,21 @@ | |||||
package cc.smtweb.framework.core.mvc.service; | |||||
import cc.smtweb.framework.core.db.DbEngine; | |||||
import cc.smtweb.framework.core.db.EntityDao; | |||||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | |||||
/** | |||||
* Created by Akmm at 2022/3/3 20:04 | |||||
*/ | |||||
public class DefaultProvider<T extends DefaultEntity> extends AbstractCompProvider { | |||||
public String tableName; | |||||
public DefaultProvider(String tableName) { | |||||
this.tableName = tableName; | |||||
} | |||||
public T getBean(long id) { | |||||
EntityDao<T> dao = (EntityDao<T>)DbEngine.getInstance().findDao(tableName); | |||||
return doGetData(tableName + "." + id, () -> dao.queryEntity(id)); | |||||
} | |||||
} |
@@ -0,0 +1,90 @@ | |||||
package cc.smtweb.framework.core.mvc.service; | |||||
import cc.smtweb.framework.core.SwException; | |||||
import cc.smtweb.framework.core.cache.AbstractCache; | |||||
import cc.smtweb.framework.core.cache.CacheManager; | |||||
import cc.smtweb.framework.core.db.DbEngine; | |||||
import cc.smtweb.framework.core.db.EntityDao; | |||||
import cc.smtweb.framework.core.db.cache.ModelTableCache; | |||||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | |||||
import cc.smtweb.framework.core.db.vo.ModelField; | |||||
import cc.smtweb.framework.core.db.vo.ModelIndex; | |||||
import cc.smtweb.framework.core.db.vo.ModelTable; | |||||
import org.apache.commons.lang3.StringUtils; | |||||
/** | |||||
* Created by Akmm at 2022/3/2 19:52 | |||||
* 默认实体实现 | |||||
*/ | |||||
public class DefaultSaveHandler<T extends DefaultEntity> extends AbstractSaveHandler<T> { | |||||
protected String tableName; | |||||
public DefaultSaveHandler(String tableName) { | |||||
this.tableName = tableName; | |||||
} | |||||
@Override | |||||
protected T createComp() throws Exception { | |||||
final EntityDao dao = DbEngine.getInstance().findDao(tableName); | |||||
T bean = (T) dao.createBean(); | |||||
bean.setEntityId(dao.nextId()); | |||||
return bean; | |||||
} | |||||
@Override | |||||
protected T loadComp(long id) { | |||||
return new DefaultProvider<T>(tableName).getBean(id); | |||||
} | |||||
@Override | |||||
protected void readFromPage() { | |||||
bean.readFromMap(params); | |||||
} | |||||
@Override | |||||
protected void checkValid() throws Exception { | |||||
ModelTable table = ModelTableCache.getInstance().getByName(tableName); | |||||
for (ModelField field: table.getFields()) { | |||||
if (field.isNotNull() && StringUtils.isEmpty(bean.getStr(field.getName()))) { | |||||
throw new SwException(field.getTitle() + "不能为空!"); | |||||
} | |||||
} | |||||
EntityDao dao = DbEngine.getInstance().findDao(tableName); | |||||
for (ModelIndex mi: table.getIndexes()) { | |||||
if (mi.isUnique()) { | |||||
dao.checkUnique(bean, mi.getFields().split(",")); | |||||
} | |||||
} | |||||
} | |||||
@Override | |||||
protected void save() { | |||||
EntityDao dao = DbEngine.getInstance().findDao(tableName); | |||||
if (isNew) { | |||||
dao.insertEntity(bean); | |||||
} else { | |||||
dao.updateEntity(bean); | |||||
} | |||||
} | |||||
@Override | |||||
protected void saveSuccess() { | |||||
super.saveSuccess(); | |||||
ModelTable table = ModelTableCache.getInstance().getByName(tableName); | |||||
if (table.isNeedCache()) { | |||||
AbstractCache<T> cache = CacheManager.getIntance().getCache(tableName); | |||||
cache.put(bean); | |||||
} | |||||
} | |||||
@Override | |||||
protected void saveFailed() { | |||||
super.saveFailed(); | |||||
ModelTable table = ModelTableCache.getInstance().getByName(tableName); | |||||
if (table.isNeedCache()) { | |||||
AbstractCache<T> cache = CacheManager.getIntance().getCache(tableName); | |||||
cache.reset(bean); | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,8 @@ | |||||
package cc.smtweb.framework.core.mvc.service; | |||||
/** | |||||
* Created by Akmm at 2022/3/3 12:10 | |||||
*/ | |||||
public interface IDataProvider<T> { | |||||
T load(); | |||||
} |
@@ -0,0 +1,13 @@ | |||||
package cc.smtweb.framework.core.mvc.service; | |||||
import cc.smtweb.framework.core.R; | |||||
import cc.smtweb.framework.core.SwMap; | |||||
import cc.smtweb.framework.core.session.UserSession; | |||||
/** | |||||
* Created by Akmm at 2022/3/2 19:01 | |||||
* 所有handler的接口 | |||||
*/ | |||||
public interface IHandler { | |||||
R doWork(SwMap params, UserSession us) throws Exception; | |||||
} |
@@ -0,0 +1,88 @@ | |||||
package cc.smtweb.system.bpm.web.design.project; | |||||
import cc.smtweb.framework.core.annotation.SwTable; | |||||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | |||||
/** | |||||
* Created by Akmm at 2022/3/1 15:35 | |||||
*/ | |||||
@SwTable("ASP_MODEL_PROJECT") | |||||
public class ModelProject extends DefaultEntity { | |||||
public static final String ENTITY_NAME = "ASP_MODEL_PROJECT"; | |||||
public ModelProject() { | |||||
super(ENTITY_NAME); | |||||
} | |||||
public long getPrjId() { | |||||
return getLong("prj_id"); | |||||
} | |||||
public void setPrjId(long prjId) { | |||||
put("prj_id", prjId); | |||||
} | |||||
public String getPrjName() { | |||||
return getStr("prj_name"); | |||||
} | |||||
public void setPrjName(String prjName) { | |||||
put("prj_name", prjName); | |||||
} | |||||
public String getPrjDepends() { | |||||
return getStr("prj_depends"); | |||||
} | |||||
public void setPrjDepends(String prjDepends) { | |||||
put("prj_depends", prjDepends); | |||||
} | |||||
public String getPrjDesc() { | |||||
return getStr("prj_desc"); | |||||
} | |||||
public void setPrjDesc(String prjDesc) { | |||||
put("prj_desc", prjDesc); | |||||
} | |||||
public int getPrjStatus() { | |||||
return getInt("prj_status"); | |||||
} | |||||
public void setPrjStatus(int prjStatus) { | |||||
put("prj_status", prjStatus); | |||||
} | |||||
public long getPrjCreateUid() { | |||||
return getLong("prj_create_uid"); | |||||
} | |||||
public void setPrjCreateUid(long prjCreateUid) { | |||||
put("prj_create_uid", prjCreateUid); | |||||
} | |||||
public long getPrjUpdateUid() { | |||||
return getLong("prj_update_uid"); | |||||
} | |||||
public void setPrjUpdateUid(long prjUpdateUid) { | |||||
put("prj_update_uid", prjUpdateUid); | |||||
} | |||||
public long getPrjCreateAt() { | |||||
return getLong("prj_create_at"); | |||||
} | |||||
public void setPrjCreateAt(long prjCreateAt) { | |||||
put("prj_create_at", prjCreateAt); | |||||
} | |||||
public long getPrjUpdateAt() { | |||||
return getLong("prj_update_at"); | |||||
} | |||||
public void setPrjUpdateAt(long prjUpdateAt) { | |||||
put("prj_update_at", prjUpdateAt); | |||||
} | |||||
} |
@@ -0,0 +1,37 @@ | |||||
package cc.smtweb.system.bpm.web.design.project; | |||||
import cc.smtweb.framework.core.R; | |||||
import cc.smtweb.framework.core.SwMap; | |||||
import cc.smtweb.framework.core.annotation.SwBody; | |||||
import cc.smtweb.framework.core.annotation.SwParam; | |||||
import cc.smtweb.framework.core.annotation.SwService; | |||||
import cc.smtweb.framework.core.db.DbEngine; | |||||
import cc.smtweb.framework.core.db.EntityDao; | |||||
import cc.smtweb.framework.core.mvc.service.AbstractCompService; | |||||
import cc.smtweb.framework.core.mvc.service.DefaultLoadHandler; | |||||
import cc.smtweb.framework.core.mvc.service.DefaultSaveHandler; | |||||
import cc.smtweb.framework.core.mvc.service.IHandler; | |||||
import cc.smtweb.framework.core.session.UserSession; | |||||
import cc.smtweb.system.bpm.engine.entity.AspModelPO; | |||||
/** | |||||
* Created by Akmm at 2022/3/1 17:00 | |||||
* 项目服务类 | |||||
*/ | |||||
@SwService | |||||
public class ModelProjectService extends AbstractCompService { | |||||
@Override | |||||
protected IHandler createHanlder(String type) { | |||||
switch (type) { | |||||
case TYPE_LOAD: | |||||
return new DefaultLoadHandler<ModelProject>(ModelProject.ENTITY_NAME); | |||||
case TYPE_SAVE: | |||||
return new DefaultSaveHandler<>(ModelProject.ENTITY_NAME); | |||||
case TYPE_DEL: | |||||
return new DefaultSaveHandler<>(ModelProject.ENTITY_NAME); | |||||
} | |||||
return null; | |||||
} | |||||
} |
@@ -0,0 +1,59 @@ | |||||
package cc.smtweb.system.bpm.test; | |||||
import cc.smtweb.framework.core.db.cache.ModelTableCache; | |||||
import cc.smtweb.system.bpm.util.CodeGenUtil; | |||||
import org.apache.commons.lang3.StringUtils; | |||||
import org.junit.Test; | |||||
/** | |||||
* Created by Akmm at 2022/3/1 9:41 | |||||
* 生成bean | |||||
*/ | |||||
public class BuildJavaBean { | |||||
@Test | |||||
public void buildBean() { | |||||
String str = "`prj_id` bigint(20) NOT NULL DEFAULT '-1',\n" + | |||||
" `prj_name` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '项目名称',\n" + | |||||
" `prj_depends` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '依赖其他的项目',\n" + | |||||
" `prj_desc` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '项目描述',\n" + | |||||
" `prj_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '状态:0 启用,1 禁用',\n" + | |||||
" `prj_create_uid` bigint(20) DEFAULT NULL,\n" + | |||||
" `prj_update_uid` bigint(20) DEFAULT NULL,\n" + | |||||
" `prj_create_at` bigint(20) NOT NULL DEFAULT '0' COMMENT '创建时间',\n" + | |||||
" `prj_update_at` bigint(20) NOT NULL DEFAULT '0' COMMENT '更新时间',"; | |||||
String[] ss = str.split("\n"); | |||||
for (String s: ss) { | |||||
String[] s0 = s.trim().split(" "); | |||||
if (StringUtils.isEmpty(s0[0])) continue; | |||||
String tn = s0[0].substring(1, s0[0].indexOf("`", 2)); | |||||
String tnu = CodeGenUtil.underlineToUpperHump(tn); | |||||
String tnn = CodeGenUtil.underlineToHump(tn); | |||||
String tt = s0[1]; | |||||
String ttj; | |||||
if (tt.contains("(")) tt = tt.substring(0, tt.indexOf("(")); | |||||
switch (tt) { | |||||
case "bigint": | |||||
tt = "long"; | |||||
ttj = "Long"; | |||||
break; | |||||
case "tinyint": | |||||
case "int": | |||||
tt = "int"; | |||||
ttj = "Int"; | |||||
break; | |||||
default: | |||||
tt = "String"; | |||||
ttj = "Str"; | |||||
break; | |||||
} | |||||
System.out.println("private " + tt + " get" + tnu + "(){\n" + | |||||
"return get" + ttj + "(\"" + tn + "\");\n" + | |||||
"}\n" + | |||||
"" + | |||||
"private void set" + tnu + "(" + tt + " " + tnn + "){\n" + | |||||
"put(\"" + tn + "\", " + tnn + ");\n" + | |||||
"}\n"); | |||||
} | |||||
} | |||||
} |