com.fhs.pagex.service.PageXAutoSqlService Maven / Gradle / Ivy
The newest version!
package com.fhs.pagex.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fhs.common.spring.SpringContextUtil;
import com.fhs.common.utils.CheckUtils;
import com.fhs.common.utils.ConverterUtils;
import com.fhs.common.utils.StringUtil;
import com.fhs.core.exception.ParamException;
import com.fhs.pagex.bean.DefaultPageXBean;
import com.fhs.pagex.dao.DefaultPageXDAO;
import com.fhs.pagex.dto.PagexAddDTO;
import com.fhs.pagex.dto.PagexListSettDTO;
import com.mybatis.jpa.cache.JpaTools;
import com.mybatis.jpa.common.ColumnNameUtil;
import com.mybatis.jpa.meta.PersistentMeta;
import com.mybatis.jpa.statement.MybatisStatementAdapter;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;
import javax.script.ScriptException;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 自动生成sql服务
*
* @ProjectName: framework_v2_idea2
* @Package: com.fhs.pagex.service
* @ClassName: PageXAutoSqlService
* @Author: JackWang
* @CreateDate: 2018/12/11 0011 21:14
* @UpdateUser: JackWang
* @UpdateDate: 2018/12/11 0011 21:14
* @Version: 1.0
*/
@Service
public class PageXAutoSqlService {
/**
*
*/
public void autoSql(String js) throws NoSuchMethodException, ScriptException {
PagexAddDTO pagexAddDTO = new PagexAddDTO(js);
String namespace = ConverterUtils.toString(pagexAddDTO.getModelConfig().get("namespace"));
String insertSql = autoInsert(pagexAddDTO);
parseSql("insertPageX", namespace, "insert", insertSql);
String deleteSql = autoDel(pagexAddDTO);
parseSql("delPageX", namespace, "delete", deleteSql);
deleteSql = autoDelForFkey(pagexAddDTO);
parseSql("delFkeyPageX", namespace, "delete", deleteSql);
String updateSql = autoUpdateSql(pagexAddDTO);
parseSql("updatePageX", namespace, "update", updateSql);
String findSql = autoFind(pagexAddDTO);
parseSql("findBeanPageX", namespace, "select", findSql);
PagexListSettDTO pagexListSettDTO = new PagexListSettDTO(js);
String findPageSql = autoFindPage(pagexListSettDTO);
parseSql("findPageX", namespace, "select", findPageSql);
String findPageCountSql = autoFindPageCount(pagexListSettDTO);
parseSql("findPageCountPageX", namespace, "select", findPageCountSql);
}
/**
* 已经存在的sqlid
*/
private Set existSqlIdSet = new HashSet<>();
private Map mappedStatements;
/**
* 将sql 添加到mybatis中
*
* @param sqlName sql的名字
* @param nameSpace namespace
* @param type 类型
* @param scriptSql sql
*/
private synchronized void parseSql(String sqlName, String nameSpace, String type, String scriptSql) {
if (mappedStatements == null) {
Configuration configuration = SpringContextUtil.getBeanByName(SqlSession.class).getConfiguration();
try {
Field mappedStatementsField = Configuration.class.getDeclaredField("mappedStatements");
mappedStatementsField.setAccessible(true);
mappedStatements = (Map) mappedStatementsField.get(configuration);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
String sqlId = nameSpace + "_" + sqlName;
//吧之前的缓存清除掉
mappedStatements.remove(DefaultPageXDAO.class.getName() + "." + sqlId);
/*if(existSqlIdSet.contains(sqlId))
{
throw new ParamException("sql已经存在:" + nameSpace + "." + type);
}*/
MybatisStatementAdapter adapter = JpaTools.statementAdapterMap.get(DefaultPageXBean.class.getName());
// 方法名
adapter.setMethodName(sqlId);
existSqlIdSet.add(sqlId);
adapter.setParameterTypeClass(Object.class);
PersistentMeta persistentMeta = JpaTools.persistentMetaMap.get(DefaultPageXDAO.class.getName());
SqlCommandType sqlCommandType = null;
String fromPreSql = null;
switch (type) {
case "select":
sqlCommandType = SqlCommandType.SELECT;
// adapter.setResultMapId(ResultMapConstants.DEFAULT_NAMESPACE + "." + persistentMeta.getEntityName());
// 返回值类型
adapter.setResultType(Map.class);
break;
case "delete":
sqlCommandType = SqlCommandType.DELETE;
adapter.setResultType(Integer.class);
break;
case "update":
sqlCommandType = SqlCommandType.UPDATE;
adapter.setResultType(Integer.class);
break;
case "insert":
sqlCommandType = SqlCommandType.INSERT;
adapter.setResultType(Integer.class);
break;
default:
throw new ParamException("不支持此类型");
}
// sqlScript
adapter.setSqlScript(scriptSql);
adapter.setSqlCommandType(sqlCommandType);
adapter.setResultMapId(null);
// 主键策略
adapter.setKeyGenerator(new NoKeyGenerator());
adapter.setKeyProperty(null);
adapter.setKeyColumn(null);
try {
//如果一个sql已经存在是正常现象
adapter.parseStatement();
} catch (IllegalArgumentException e) {
}
}
/**
* 自动生成insert sql
*
* @param pagexAddDTO pagexAddDTO
* @return insert 的sql
*/
public String autoInsert(PagexAddDTO pagexAddDTO) {
Map modelConfig = pagexAddDTO.getModelConfig();
StringBuilder sqlBuilder = new StringBuilder("INSERT INTO " + modelConfig.get("table") + " (`");
sqlBuilder.append(modelConfig.get("pkey") + "`");
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy