RepositoryTemplate.ftl 2.68 KB
package ${basePackage}.repository;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;

import org.msgpack.MessagePack;
import org.springframework.stereotype.Repository;

import ${basePackage}.bean.entity.${entityClassSimpleName};
import ${basePackage}.bean.vo.${voClassSimpleName};
import com.taover.repository.CustomJdbcTemplate;
import com.taover.util.UtilHttpRequestMap;
import com.taover.util.UtilObject;

@Repository
public class ${repositoryClassSimpleName} extends CustomJdbcTemplate<${entityClassSimpleName}, Integer>{
	
	public ${repositoryClassSimpleName}() throws Exception {
		super();
	}
	
	public String getSsidById(Integer id) throws Exception{
		MessagePack pack = new MessagePack();
		return Base64.getEncoder().encodeToString(pack.write(new Integer[]{id, id, id}));
	}
	
	public Integer getIdBySsid(String ssid) throws Exception{
		MessagePack pack = new MessagePack();
		return pack.read(Base64.getDecoder().decode(ssid), Integer[].class)[0];
	}
	
	public Map<String, Object> getVoPage(List<Object[]> condition, Integer page, Integer size) throws Exception{
		Map<String, Object> entityData = this.findPageByCondition(condition, page, size);		
		List<${entityClassSimpleName}> entityList = (List<${entityClassSimpleName}>) entityData.get("rows");
		List<${voClassSimpleName}> voList = new ArrayList<${voClassSimpleName}>();
		for(int i=0; i<entityList.size(); ++i){
			voList.add(this.getVoByEntity(entityList.get(i)));
		}
		entityData.put("rows", voList);
		return entityData;
	}
	
	public ${voClassSimpleName} getVoBySsid(String ssid) throws Exception{
		return this.getVoByEntity(this.findEntityByID(this.getIdBySsid(ssid)));
	}
	
	public ${voClassSimpleName} getVoByEntity(${entityClassSimpleName} entity) throws Exception{
		${voClassSimpleName} voObj = null;
		try {
			voObj = UtilObject.fieldCopy(entity, ${voClassSimpleName}.class, true, new String[]{"id"});
		} catch (Exception e) {
			throw new Exception("对象转换失败");
		}
		voObj.setSsid(this.getSsidById(entity.getId()));
		return voObj;
	}
	
	public void updateEntityBySsid(String ssid, List<Object[]> updateList) throws Exception{
		this.updateEntityById(updateList, this.getIdBySsid(ssid));
	}
	
	public void deleteEntityBySsid(String ssid) throws Exception{
		this.deleteEntityByID(this.getIdBySsid(ssid));
	}
	
	public ${voClassSimpleName} addEntityByMap(Map<String, Object> data) throws Exception{
		${entityClassSimpleName} entityObj = UtilHttpRequestMap.getBeanByMap(data, ${entityClassSimpleName}.class, false, "id");
		BigInteger resultId = this.addEntityForAutoincrementId(entityObj);
		return this.getVoByEntity(this.findEntityByID(resultId.intValue())); 
	}
}