All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.starmcc.qmdata.base.AbstractQmDataBase Maven / Gradle / Ivy
package com.starmcc.qmdata.base;
import com.starmcc.qmdata.common.QmData;
import com.starmcc.qmdata.exception.QmDataException;
import com.starmcc.qmdata.note.Style;
import com.starmcc.qmdata.note.Table;
import com.starmcc.qmdata.util.ConvertUtil;
import com.starmcc.qmdata.util.QmDataStyleTools;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
import java.util.Map;
/**
* @author qm
* @Title QmBase实现类
* @Date 注释时间:2019/1/23 12:43
*/
public abstract class AbstractQmDataBase implements QmData {
/**
* 命名空间
*/
private static final String QM_NAMESPACE = "QmBaseMapper.";
/**
* 获取Mybatis SqlSession
*/
protected SqlSessionFactory sqlSessionFactory;
/**
* 解析命名空间
*
* @param sqlName
* @return
*/
private String getSqlName(String sqlName) {
try {
String methodName = sqlName.substring(sqlName.indexOf("Mapper") + 6);
String nameSpace = sqlName.substring(0, sqlName.indexOf("Mapper") + 6);
return nameSpace + "." + methodName;
} catch (Exception e) {
throw new QmDataException("Mapper命名空间错误!'" + sqlName + "' Error!", e);
}
}
@Override
public List selectList(String sqlName, Object params) {
SqlSession session = sqlSessionFactory.openSession();
try {
List list = session.selectList(getSqlName(sqlName), params);
session.commit();
return list;
} catch (Exception e) {
throw new QmDataException(getErrorMsg("selectList"), e);
} finally {
session.close();
}
}
@Override
public M selectOne(String sqlName, Object params) {
SqlSession session = sqlSessionFactory.openSession();
try {
M obj = session.selectOne(getSqlName(sqlName), params);
session.commit();
return obj;
} catch (Exception e) {
throw new QmDataException(getErrorMsg("selectOne"), e);
} finally {
session.close();
}
}
@Override
public int insert(String sqlName, Object params) {
SqlSession session = sqlSessionFactory.openSession();
try {
int result = session.insert(getSqlName(sqlName), params);
session.commit();
return result;
} catch (Exception e) {
throw new QmDataException(getErrorMsg("insert"), e);
} finally {
session.close();
}
}
@Override
public int update(String sqlName, Object params) {
SqlSession session = sqlSessionFactory.openSession();
try {
int result = session.update(getSqlName(sqlName), params);
session.commit();
return result;
} catch (Exception e) {
throw new QmDataException(getErrorMsg("update"), e);
} finally {
session.close();
}
}
@Override
public int delete(String sqlName, Object params) {
SqlSession session = sqlSessionFactory.openSession();
try {
int result = session.delete(getSqlName(sqlName), params);
session.commit();
return result;
} catch (Exception e) {
throw new QmDataException(getErrorMsg("delete"), e);
} finally {
session.close();
}
}
@Override
public List autoSelectList(Q entity, Class clamm) {
SqlSession session = sqlSessionFactory.openSession();
List> mapLis;
try {
if (entity == null) {entity = clamm.newInstance();}
mapLis = session.selectList(QM_NAMESPACE + "selectAuto", new QmDataDto(entity, false).getParamsMap());
session.commit();
} catch (Exception e) {
throw new QmDataException(getErrorMsg("autoSelectList"), e);
} finally {
session.close();
}
// 如果是空的直接返回null
if (mapLis == null) {return null;}
// 如果数据库字段是下划线样式 则进行转换
Table table = entity.getClass().getAnnotation(Table.class);
if (table != null && table.style() == Style.UNDERLINE) {
for (int i = 0; i < mapLis.size(); i++) {
Map map = mapLis.get(i);
map = QmDataStyleTools.transformMapForHump(map);
mapLis.set(i, map);
}
}
return ConvertUtil.mapsToObjects(mapLis, clamm);
}
@Override
public Q autoSelectOne(Q entity, Class clamm) {
SqlSession session = sqlSessionFactory.openSession();
Map map;
try {
if (entity == null) {entity = clamm.newInstance();}
map = session.selectOne(QM_NAMESPACE + "selectAutoOne", new QmDataDto(entity, false).getParamsMap());
session.commit();
} catch (Exception e) {
throw new QmDataException(getErrorMsg("autoSelectOne"), e);
} finally {
session.close();
}
if (map == null) {return null;}
// 如果数据库字段是下划线样式 则进行转换
Table table = entity.getClass().getAnnotation(Table.class);
if (table != null && table.style() == Style.UNDERLINE) {
map = QmDataStyleTools.transformMapForHump(map);
}
// 如果是空的直接返回null
return ConvertUtil.mapToBean(map, entity);
}
@Override
public int autoInsert(Q entity) {
if (entity == null) {return -1;}
SqlSession session = sqlSessionFactory.openSession();
try {
int result = session.insert(QM_NAMESPACE + "insertAuto", new QmDataDto(entity, true).getParamsMap());
session.commit();
return result;
} catch (Exception e) {
throw new QmDataException(getErrorMsg("autoInsert"), e);
} finally {
session.close();
}
}
@Override
public int autoUpdate(Q entity) {
if (entity == null) {return -1;}
SqlSession session = sqlSessionFactory.openSession();
try {
int result = session.update(QM_NAMESPACE + "updateAuto", new QmDataDto(entity, true).getParamsMap());
session.commit();
return result;
} catch (Exception e) {
throw new QmDataException(getErrorMsg("autoUpdate"), e);
} finally {
session.close();
}
}
@Override
public int autoDelete(Q entity) {
if (entity == null) {return -1;}
SqlSession session = sqlSessionFactory.openSession();
try {
int result = session.delete(QM_NAMESPACE + "deleteAuto", new QmDataDto(entity, true).getParamsMap());
session.commit();
return result;
} catch (Exception e) {
throw new QmDataException(getErrorMsg("autoDelete"), e);
} finally {
session.close();
}
}
@Override
public int autoSelectCount(Q entity) {
if (entity == null) {return -1;}
SqlSession session = sqlSessionFactory.openSession();
try {
int result = session.selectOne(QM_NAMESPACE + "selectCount", new QmDataDto(entity, false).getParamsMap());
session.commit();
return result;
} catch (Exception e) {
throw new QmDataException(getErrorMsg("autoSelectCount"), e);
} finally {
session.close();
}
}
/**
* 配置错误信息
*
* @param methodName
* @return
*/
private String getErrorMsg(String methodName) {
return "SQL error using " + methodName + "! 使用 " + methodName + " 发生SQL错误!";
}
}