接上一节内容,
自动生成的 IInterfaceIdMappingService 接口文件
package com.wasion.ems.commons.archive.service;
import java.io.Serializable;
import java.util.*;
import com.test.InterfaceIdMapping;
import com.test.SupDto;
import com.test.IBaseService;
import com.test.IBatchService;
/**
* INTERFACE_ID_MAPPING
* @author table tool 2019-05-23
*/
public interface IInterfaceIdMappingService extends IBaseService<InterfaceIdMapping, SupDto>, IBatchService<InterfaceIdMapping, SupDto> {
//demo: public abstract List<InterfaceIdMapping> findAll(String sqlExpression, SupDto p);
}
IInterfaceIdMappingDao.java 接口文件:
package com.test;
import java.io.Serializable;
import java.util.*;
import com.test.SupDto;
import com.test.InterfaceIdMapping;
import com.test.IBaseDao;
import com.test.IBatchDao;
/**
* INTERFACE_ID_MAPPING
* @author table tool 2019-05-23
*/
public interface IInterfaceIdMappingDao extends IBaseDao<InterfaceIdMapping, SupDto>, IBatchDao<InterfaceIdMapping, SupDto> {
//demo: public abstract List<InterfaceIdMapping> findAll(String sqlExpression, SupDto p);
}
InterfaceIdMappingServicePojoImpl.java 服务实现文件:
package com.test;
import java.io.Serializable;
import java.util.*;
import com.test.IInterfaceIdMappingDao;
import com.test.InterfaceIdMapping;
import com.test.IInterfaceIdMappingService;
import com.test.SupDto;
/**
* INTERFACE_ID_MAPPING
* @author table tool 2019-05-23
*/
public class InterfaceIdMappingServicePojoImpl implements IInterfaceIdMappingService {
private IInterfaceIdMappingDao dao;
/** spring in */
public IInterfaceIdMappingDao getDao() {
return dao;
}
public void setDao(IInterfaceIdMappingDao dao) {
this.dao = dao;
}
public InterfaceIdMapping create(InterfaceIdMapping entity, SupDto p) {
return this.dao.create(entity, p);
}
public boolean delete(InterfaceIdMapping entity, SupDto p) {
return this.dao.delete(entity, p);
}
public InterfaceIdMapping read(InterfaceIdMapping entity, SupDto p) {
return this.dao.read(entity, p);
}
public boolean update(InterfaceIdMapping entity, SupDto p) {
return this.dao.update(entity, p);
}
public List<InterfaceIdMapping> findAll(String sqlExpression, SupDto p) {
return this.dao.findAll(sqlExpression, p);
}
public boolean createBatch(List<InterfaceIdMapping> entities, SupDto p) {
return this.dao.createBatch(entities, p);
}
public boolean deleteBatch(List<InterfaceIdMapping> entities, SupDto p) {
return this.dao.deleteBatch(entities, p);
}
public boolean updateBatch(List<InterfaceIdMapping> entities, SupDto p) {
return this.dao.updateBatch(entities, p);
}
public boolean updateOrInsertBatch(List<InterfaceIdMapping> entities, SupDto p) {
return this.dao.updateOrInsertBatch(entities, p);
}
}
InterfaceIdMappingDaoJdbcImpl.java 接口dao实现文件:
package com.wasion.ems.commons.archive.dao.jdbc;
import java.util.*;
import java.math.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.test.SupDto;
import com.test.JdbcDataUtils;
import com.test.SqlCacheUnits;
import com.test.SqlStringUnits;
import com.test.IInterfaceIdMappingDao;
import com.test.InterfaceIdMapping;
import com.test.BaseMdbDaoSupport;
/**
* INTERFACE_ID_MAPPING
* @author table tool 2019-05-23
*/
public class InterfaceIdMappingDaoJdbcImpl extends BaseMdbDaoSupport implements IInterfaceIdMappingDao {
private Log logger = LogFactory.getLog(this.getClass());
private SqlStringUnits stool = new SqlStringUnits();
private JdbcDataUtils jtool = new JdbcDataUtils();
private String table = "INTERFACE_ID_MAPPING";
private String name = "INTERFACE_ID_MAPPING";
private final int BATCH_SIZE=500;
/** findAll row */
public List<InterfaceIdMapping> findAll(String sqlExpression, SupDto p) {
if(sqlExpression == null) return null;
String tablename = this.getTableName(p, this.table);
String sqlNote = this.getSqlNote(p, this.name);
String strSql = null;
strSql =" SELECT "+this.getColumnString()+" FROM "+ tablename+" "+sqlExpression;
return this.getJdbcDaoSupport(p).query(strSql, InterfaceIdMapping.class, sqlNote);
}
/** read row */
public InterfaceIdMapping read(InterfaceIdMapping entity, SupDto p) {
if(entity == null) return null;
String tablename = this.getTableName(p, this.table);
String sqlNote = this.getSqlNote(p, this.name);
String strSql = null;
strSql =" SELECT "+this.getColumnString()+" FROM "+tablename
+" WHERE OTHER_ID ="+stool.toString(entity.getOTHER_ID())
+" AND OBJECT_TYPE = "+entity.getOBJECT_TYPE()
+" AND DATA_FROM = "+entity.getDATA_FROM();
//TODO ADD WHERE CODE......
return this.getJdbcDaoSupport(p).querySingle(strSql, InterfaceIdMapping.class, sqlNote);
}
/** create row */
public InterfaceIdMapping create(InterfaceIdMapping entity, SupDto p) {
if(entity == null) return null;
String tablename = this.getTableName(p, this.table);
String sqlNote = this.getSqlNote(p, this.name);
String strSql = null;
strSql =" INSERT INTO "+ tablename +" ("+this.getColumnString()+") "+
" VALUES(?,?,?,?,?,?,?,?)";
int rs = this.getJdbcDaoSupport(p).update(strSql, this.processInsertParm(entity), sqlNote);
return rs >0 ? entity : null;
}
/** delete row */
public boolean delete(InterfaceIdMapping entity, SupDto p) {
if(entity == null) return false;
String tablename = this.getTableName(p, this.table);
String sqlNote = this.getSqlNote(p, this.name);
String strSql = null;
strSql =" DELETE "+ tablename +
" WHERE OTHER_ID ="+stool.toString(entity.getOTHER_ID()) +
" AND OBJECT_TYPE = "+entity.getOBJECT_TYPE() +
" AND DATA_FROM = "+entity.getDATA_FROM();
//TODO ADD WHERE CODE......
int rs = this.getJdbcDaoSupport(p).update(strSql, sqlNote);
return rs >= 0 ? true : false;
}
/** update row */
public boolean update(InterfaceIdMapping entity, SupDto p) {
if(entity == null) return false;
String tablename = this.getTableName(p, this.table);
String sqlNote = this.getSqlNote(p, this.name);
String strSql = null;
strSql =" UPDATE "+ tablename +" SET"+
" OTHER_ID=?,OBJECT_TYPE=?,DATA_FROM=?,OBJECT_ID=?,OBJECT_NO=?,OBJECT_CODE=?,OBJECT_NAME=?,MEMO=?"+
" WHERE OTHER_ID=? AND OBJECT_TYPE=? AND DATA_FROM=? ";
//TODO ADD WHERE CODE......
int rs = this.getJdbcDaoSupport(p).update(strSql, this.processUpdateParm(entity), sqlNote);
return rs >= 0 ? true : false;
}
/** createBatch row */
public boolean createBatch(List<InterfaceIdMapping> entities, SupDto p) {
if(entities == null || entities.size()==0) return true;
String tablename = this.getTableName(p, this.table);
String sqlNote = this.getSqlNote(p, this.name);
String strSql = null;
strSql =" INSERT INTO "+ tablename +" ("+this.getColumnString()+") "+
" VALUES(?,?,?,?,?,?,?,?)";
boolean bResult = false;
List<Object[]> args = new ArrayList<Object[]>();
int len = entities.size();
int i = 0;
for(InterfaceIdMapping e : entities){
args.add( processInsertParm(e) );
i++;
if ((i % BATCH_SIZE == 0) || i == len) {
int[] rs = this.getJdbcDaoSupport(p).batchUpdate(strSql, args, sqlNote);
args.clear();
if(logger.isDebugEnabled()){
logger.debug("[正在批量提交数据]: "+i+"/"+len+", BY "+name);
}
if(rs == null){ bResult = false; break; }
else{ bResult = true; }
}
}
logger.info("[完成批量提交数据]: "+i+"/"+len+", BY "+name);
return bResult;
}
/** deleteBatch row */
public boolean deleteBatch(List<InterfaceIdMapping> entities, SupDto p) {
if(entities == null || entities.size()==0) return true;
String tablename = this.getTableName(p, this.table);
String sqlNote = this.getSqlNote(p, this.name);
String strSql = null;
strSql =" DELETE "+ tablename +" WHERE OTHER_ID=? AND OBJECT_TYPE=? AND DATA_FROM=? ";
//TODO ADD WHERE CODE......
boolean bResult = false;
List<Object[]> args = new ArrayList<Object[]>();
int len = entities.size();
int i = 0;
for(InterfaceIdMapping e : entities){
args.add( processDeleteParm(e) );
i++;
if ((i % BATCH_SIZE == 0) || i == len) {
int[] rs = this.getJdbcDaoSupport(p).batchUpdate(strSql, args, sqlNote);
args.clear();
if(logger.isDebugEnabled()){
logger.debug("[正在批量提交数据]: "+i+"/"+len+", BY "+name);
}
if(rs == null){ bResult = false; break; }
else{ bResult = true; }
}
}
logger.info("[完成批量提交数据]: "+i+"/"+len+", BY "+name);
return bResult;
}
/** updateBatch row */
public boolean updateBatch(List<InterfaceIdMapping> entities, SupDto p) {
if(entities == null || entities.size()==0) return true;
String tablename = this.getTableName(p, this.table);
String sqlNote = this.getSqlNote(p, this.name);
String strSql = null;
strSql =" UPDATE "+ tablename +" SET"+
" OTHER_ID=?,OBJECT_TYPE=?,DATA_FROM=?,OBJECT_ID=?,OBJECT_NO=?,OBJECT_CODE=?,OBJECT_NAME=?,MEMO=?"+
" WHERE OTHER_ID=? AND OBJECT_TYPE=? AND DATA_FROM=? ";
//TODO ADD WHERE CODE......
boolean bResult = false;
List<Object[]> args = new ArrayList<Object[]>();
int len = entities.size();
int i = 0;
for(InterfaceIdMapping e : entities){
args.add( processUpdateParm(e) );
i++;
if ((i % BATCH_SIZE == 0) || i == len) {
int[] rs = this.getJdbcDaoSupport(p).batchUpdate(strSql, args, sqlNote);
args.clear();
if(logger.isDebugEnabled()){
logger.debug("[正在批量提交数据]: "+i+"/"+len+", BY "+name);
}
if(rs == null){ bResult = false; break; }
else{ bResult = true; }
}
}
logger.info("[完成批量提交数据]: "+i+"/"+len+", BY "+name);
return bResult;
}
/** updateOrInsertBatch row */
public boolean updateOrInsertBatch(List<InterfaceIdMapping> entities, SupDto p) {
if(entities==null || entities.size()==0) return true;
String tablename = this.getTableName(p, this.table);
String sqlNote = this.getSqlNote(p, this.name);
String sqlUp = null;
String sqlIn = null;
sqlIn =" INSERT INTO "+ tablename +" ("+
" OBJECT_ID,OBJECT_NO,OBJECT_CODE,OBJECT_NAME,MEMO, OTHER_ID,OBJECT_TYPE,DATA_FROM) "+
" VALUES(?,?,?,?,?,?,?,?)";
sqlUp =" UPDATE "+ tablename +" SET"+
" OTHER_ID=?,OBJECT_TYPE=?,DATA_FROM=?,OBJECT_ID=?,OBJECT_NO=?,OBJECT_CODE=?,OBJECT_NAME=?,MEMO=?"+
" WHERE OTHER_ID=? AND OBJECT_TYPE=? AND DATA_FROM=? ";
//TODO ADD WHERE CODE......
boolean bResult = false;
List<Object[]> args = new ArrayList<Object[]>();
int len = entities.size();
int i = 0;
for(InterfaceIdMapping e : entities){
args.add( processUpdateOrInsertParm(e) );
i++;
if ((i % BATCH_SIZE == 0) || i == len) {
bResult = this.getJdbcDaoSupport(p).batchUpdateOrInsert(sqlUp, sqlIn, args, sqlNote);
args.clear();
if(logger.isDebugEnabled()){
logger.debug("[正在批量提交数据]: "+i+"/"+len+", BY "+name);
}
if(! bResult){ break; }
}
}
logger.info("[完成批量提交数据]: "+i+"/"+len+", BY "+name);
return bResult;
}
//-------------------------Parameters CODE-------------------------
/** Insert parameters */
protected Object[] processInsertParm(InterfaceIdMapping e) {
Object[] parameters = new Object[]{
e.getOTHER_ID(), e.getOBJECT_TYPE(), e.getDATA_FROM(), e.getOBJECT_ID(), e.getOBJECT_NO(),
e.getOBJECT_CODE(), e.getOBJECT_NAME(), e.getMEMO() };
return parameters;
}
/** Insert parameters */
protected Object[] processDeleteParm(InterfaceIdMapping e) {
Object[] parameters = new Object[]{
e.getOTHER_ID(), e.getOBJECT_TYPE(), e.getDATA_FROM()
};
return parameters;
}
/** Insert parameters */
protected Object[] processUpdateParm(InterfaceIdMapping e) {
Object[] parameters = new Object[]{
e.getOTHER_ID(), e.getOBJECT_TYPE(), e.getDATA_FROM(), e.getOBJECT_ID(), e.getOBJECT_NO(),
e.getOBJECT_CODE(), e.getOBJECT_NAME(), e.getMEMO(),
//TODO ADD WHERE CODE......
e.getOTHER_ID(), e.getOBJECT_TYPE(), e.getDATA_FROM()
};
return parameters;
}
/** Insert parameters */
protected Object[] processUpdateOrInsertParm(InterfaceIdMapping e) {
Object[] parameters = new Object[]{
e.getOBJECT_ID(), e.getOBJECT_NO(),
e.getOBJECT_CODE(), e.getOBJECT_NAME(), e.getMEMO(),
//TODO ADD WHERE CODE......
e.getOTHER_ID(), e.getOBJECT_TYPE(), e.getDATA_FROM()
};
return parameters;
}
/** 8 Column */
protected String getColumnString() {
String sqlSql =" OTHER_ID,OBJECT_TYPE,DATA_FROM,OBJECT_ID,OBJECT_NO,OBJECT_CODE,OBJECT_NAME,MEMO ";
return sqlSql;
}
}
代码里有用到 IBatchDao 公用接口, IBaseDao接口 IBaseService接口, IBatchService 接口
附公用dao接口文件:
IBaseDao.java
package com.test;
import java.io.Serializable;
import java.util.List;
/**
* 基本数据操作接口
* @author guishuanglin 2007-11-17
* @param <T> entity
* @param <P> parameter, Table name or table type or time type or other object
*/
public interface IBaseDao<T, P extends Serializable> extends IDao {
/**
* create
* @param t
*/
public T create(T t, P p);
/**
* delete by T
* @param t
*/
public boolean delete(T t, P p);
/**
* update
* @param t
*/
public boolean update(T t, P p);
/**
* read by PK
* @param t
* @return
*/
public T read(T t, P p);
/**
* find all
* @param sqlExpression, where,order by,group by....
* @return
*/
public List<T> findAll(String sqlExpression, P p);
}
IBatchDao.java
package com.test;
import java.io.Serializable;
import java.util.*;
/**
* Batch 批量操作服务
* @author guishuanglin 2007-11-17
* @param <T> entity
* @param <P> parameter, Table name or table type or time type or other object
*/
public interface IBatchService <T,P extends Serializable> extends IService {
/**
* create by List<T>
* @param entities
*/
public boolean createBatch(List<T> entities, P p);
/**
* delete by List<T>
* @param entities
*/
public boolean deleteBatch(List<T> entities, P p);
/**
* update by List<T>
* @param entities
*/
public boolean updateBatch(List<T> entities, P p);
/**
* updateOrInsertBatch by List<T>
* @param entities
* @param p
*/
public boolean updateOrInsertBatch(List<T> entities, P p);
}
说明: IBaseDao 与 IBaseService接口内容相同, IBatchDao 与IBatchService 相同, 只是为了分层.
BaseMdbDaoSupport 可以用spring原生 JdbcDaoSupport 代替, 可能有些方法没有, 但是差不多.
SupDto 是上下文对象, 比如查询数据的云服务器名称, 或者数据源名称, 因为在分库系统, 云库系统数据存在不同的数据库.
另外在 SupDto 也包含了分页对象 PageDto 的参数.
JdbcDataUtils; SqlStringUnits; 这三个只是转换工具, 自己可以修改代码不使用它
SqlCacheUnits; 是为了缓存最近1000条 sql语句, 方便显示给后台维护人员的.