java 根據表名自動生成增,刪,改,查程序代碼, 包括Dao接口類,Dao實現類(二)

接上一節內容,

自動生成的 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語句, 方便顯示給後臺維護人員的.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章