使用工具类

package com.base.utils;

public class StringUtil {

	/**
	 *判断字符串是否为空。是:则返回true;否则返回false
	 */
	public static boolean isNull(String str){
		if(str.equals(null) || "".equals(str) || str == null){
			return true;
		}
		return false;
	}
	
	/**
	 *判断字符串是否为空 
	 */
	public static String isNullThenNewString(String src, String dest){
		if(isNull(src)){
			return dest;
		}else{
			return src;
		}
	}
	/**
	 * 将String[]转换为形如"…,…,…,…"的字符串,用来在删除时使用
	 * @return
	 */
	public static String arrayToString(String[] fid) {
		String serperater = ",";
		return arrayToString(fid, serperater);
	}

	/**
	 * 用指定的分隔符来将一个字符串数组组合成一个字符串
	 */
	public static String arrayToString(String[] fids, String serperater) {
		String strfids = "";
		for (int i = 0; i < fids.length; i++) { // 将所有选择的记录删除
			if (i == 0){
				strfids = fids[0];
			}
			else{
				strfids += serperater + fids[i];
			}
		}
		return strfids;
	}
	/**
	 * 用指定的分隔符来将一个字符串切割成为数组
	 */
	public static String[] stringToArray(String fids, String serperater) {
		if(isNull(serperater)){
			serperater = ",";
		}
		
		if(fids.endsWith(serperater)){
			fids = fids.substring(0, fids.length()-1);
		}
		return fids.split(serperater);
	}
	
}

package com.base.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;

import com.base.constant.DataTypes;
import com.base.exception.DefaultException;
import com.base.po.BasePO;
import com.base.vo.BaseVO;

public class BeanUtils {

	/**
	 *得到对象Object的所有属性组成的字符串
	 *@param Object
	 *@return String
	 */
	public static String getAllFieldsName(Object object) throws DefaultException{
		Field[] fields = object.getClass().getDeclaredFields();
		StringBuffer selectedFields = new StringBuffer("");
		
		if(object==null){
			try {
				object=object.getClass().newInstance();
			} catch (InstantiationException e) {
				System.out.println("加载类的实例出错!");
				throw new  DefaultException("加载类的实例出错!");
			} catch (IllegalAccessException e) {
				System.out.println("非法访问类的私有属性!");
				throw new  DefaultException("非法访问类的私有属性!");
			}
		}
		for(int i=0;i<fields.length;i++){
			selectedFields.append(fields[i].getName()+",");
		}
		return (selectedFields.toString()).substring(0, selectedFields.toString().lastIndexOf(","));
	}
	
	/**
	 * 将srcObject(基本数据类型的数据对象)以type类型输出(形成sql语句的value值形式)
	 */
	public static Object getField(Object srcObject, String type)throws DefaultException{
		if(DataTypes.STRING.equals(type)){
    		if(srcObject==null){
    			srcObject = new String("");
    		}
			return "\""+srcObject.toString()+"\"";
    	}else{
    		return changeToType(srcObject, type);
    	}
	}
	
	
	/**
	 * 将srcObject(基本数据类型的数据对象)以type类型输出
	 */
	public static Object changeToType(Object srcObject, String type) throws DefaultException{
    	try{
    		if(DataTypes.STRING.equals(type)){//String
        		if(srcObject==null){
        			srcObject = new String("");
        		}
    			return srcObject.toString();
        	}else if(DataTypes.CHAR.equals(type)){//char
        		if(srcObject==null){
        			srcObject = new Character('0');
        		}
        		return ((Character) srcObject);
        	}else if(DataTypes.CHARACTER.equals(type)){//char
        		if(srcObject==null){
        			srcObject = new Character('0');
        		}
        		return ((Character) srcObject);
        	}else if(DataTypes.SHORT.equals(type)){//short
        		if(srcObject==null){
        			srcObject = new Short("0");
        		}
        		return Short.valueOf(srcObject.toString());
        	}else if(DataTypes.SHORT_.equals(type)){//Short
        		if(srcObject==null){
        			srcObject = new Short("0");
        		}
        		return Short.valueOf(srcObject.toString());
        	}else if(DataTypes.INT.equals(type)){//int
        		if(srcObject==null){
        			srcObject = new Integer(0);
        		}
        		return Integer.valueOf(srcObject.toString());
        	}else if(DataTypes.INTEGER.equals(type)){//Integer
        		if(srcObject==null){
        			srcObject = new Integer(0);
        		}
        		return Integer.getInteger(srcObject.toString());
        	}else if(DataTypes.LONG.equals(type)){//long
        		if(srcObject==null){
        			srcObject = new Long(0);
        		}
        		return Long.valueOf(srcObject.toString());
        	}else if(DataTypes.LONG_.equals(type)){//Long
        		if(srcObject==null){
        			srcObject = new Long(0);
        		}
        		return Long.valueOf(srcObject.toString());
        	}else if(DataTypes.FLOAT.equals(type)){//float
        		if(srcObject==null){
        			srcObject = new Float(0);
        		}
        		return Float.valueOf(srcObject.toString());
        	}else if(DataTypes.FLOAT_.equals(type)){//Float
        		if(srcObject==null){
        			srcObject = new Float(0);
        		}
        		return Float.valueOf(srcObject.toString());
        	}else if(DataTypes.DOUBLE.equals(type)){//double
        		if(srcObject==null){
        			srcObject = new Double(0);
        		}
        		return Double.valueOf(srcObject.toString());
        	}else if(DataTypes.DOUBLE_.equals(type)){//Double
        		if(srcObject==null){
        			srcObject = new Double(0);
        		}
        		return Double.valueOf(srcObject.toString());
        	}else if(DataTypes.BIGDECIMAL.equals(type)){//BigDecimal
        		if(srcObject==null){
        			srcObject = new BigDecimal("0");
        		}
        		return new BigDecimal(srcObject.toString());
        	}else if(DataTypes.DATE.equals(type)){//Date
        		if(srcObject==null){
        			srcObject = new Date();
        		}
        		return new Date(srcObject.toString());
        	}else if(DataTypes.TIMESTMAP.equals(type)){//Timstamp
        		if(srcObject==null){
        			srcObject = new Timestamp(0);
        		}
        		return Timestamp.valueOf(srcObject.toString());
        	}else if(DataTypes.BOOLEAN.equals(type)){//boolean
        		if(srcObject==null){
        			srcObject = new Boolean(false);
        		}
        		return Boolean.valueOf(srcObject.toString());
        	}else if(DataTypes.BOOLEAN_.equals(type)){//Boolean
        		if(srcObject==null){
        			srcObject = new Boolean(false);
        		}
        		return Boolean.valueOf(srcObject.toString());
        	}else if(DataTypes.BYTE.equals(type)){//byte
        		if(srcObject==null){
        			srcObject = new Byte("0");
        		}
        		return Byte.valueOf(srcObject.toString());
        	}else if(DataTypes.BYTE_.equals(type)){//Byte
        		if(srcObject==null){
        			srcObject = new Byte("0");
        		}
        		return Byte.valueOf(srcObject.toString());
        	}
    	}catch(ClassCastException e){
    		e.printStackTrace();
    		throw new DefaultException("(数据类型转换出错)");
    	}
    	return srcObject;
    }
	
	
	/**
	 * 首字母大写
	 */
	public static String toUpFirstLetter(String str){
		return str.substring(0, 1).toUpperCase()+str.substring(1, str.length());
	}
	
	/**
	 * 首字母小写
	 */
	public static String toLowerFirstLetter(String str){
		return str.substring(0, 1).toUpperCase()+str.substring(1, str.length());
	}
	
	/**
	 * 找出object对象的指定方法,params为false表示get方法,为true表示set方法
	 * @param Object,String,params
	 * @return Method
	 * @throws DefaultException 
	 */
	public static Method getMethodByName(Object object, String methodName, boolean params) throws DefaultException{
		
		if(!params){//get方法
			try {
				return object.getClass().getDeclaredMethod(methodName, null);
			} catch (SecurityException e) {
				e.printStackTrace();
				throw new DefaultException("该方法不存在或者该方法为私有的,不可访问!");
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
				throw new DefaultException("该方法不存在或者该方法为私有的,不可访问!");
			}
		}
		Method[] methods = object.getClass().getDeclaredMethods();
		for(int i=0;i<methods.length;i++){
			if(methodName.equals(methods[i].getName())){
				return methods[i];
			}
		}
		//如果该对象中不包含该方法,报异常
		throw new DefaultException("该方法不存在!");
	}
	
	/**
	 * VO与PO命名规约:1.取名时之存在VO与PO不同其他相同
	 *               2.包路径只有vo与po不同
	 */
	public static String getVONameByPOName(String POName){
		return POName.substring(0, POName.length()-2)+"VO";
	}
	
	/**
	 * PO与VO命名规约:取名时之存在VO与PO不同其他相同
	 */
	public static String getPONameByVOName(String VOName){
		return VOName.substring(0, VOName.length()-2)+"PO";
	}
	
	/**
	 *根据VO对象,得到与他名称相似的PO对象(XXVO,XXPO)
	 * @throws DefaultException 
	 */
	public static Object getPoByVoUseName(BaseVO objVO) throws DefaultException{
		String poname = BeanUtils.getPONameByVOName(objVO.getClass().getName());
		poname = poname.replaceAll("vo", "po");
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>POName:"+poname);
		return getObjectByName(poname);
	}
	
	/**
	 *根据PO对象,得到与他名称相似的VO对象(XXVO,XXPO)
	 * @throws DefaultException 
	 */
	public static Object getVoByPoUseName(BasePO objPO) throws DefaultException{
		String voname = BeanUtils.getVONameByPOName(objPO.getClass().getName());
		voname = voname.replaceAll("po", "vo");
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>voname:"+voname);
		
		return getObjectByName(voname);
	}
	
	/**
	 *根据对象的名称,得到对象实例
	 */
	public static Object getObjectByName(String className)throws DefaultException{
		
		Object obj = null;
		try {
			obj = Class.forName(className).newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println(obj.toString());
		return obj;
	}
	
	/**
	 * 根据属性名称,获取属性的类型
	 */
	public static String getPropertyType(Class cla, String pro)throws DefaultException{
		Method[] methods = cla.getDeclaredMethods();
		for(int i=0;i<methods.length;i++){
			if(("get"+BeanUtils.toUpFirstLetter(pro)).equals(methods[i].getName())){
				return methods[i].getReturnType().getName();
			}
		}
		throw new DefaultException("找不到该方法");
	}
	
	/**
	 * 根据方法名,获取方法对应的属性名(get方法)
	 */
	public static String getPropertyByMethodName(String methodName) throws DefaultException{
		if(methodName.indexOf("get")!=-1){
			return BeanUtils.toLowerFirstLetter(methodName.replaceFirst("get", ""));
		}
		throw new DefaultException("该方法不是get方法!");
	}
	
	/**
	 * 根据属性名,获取(get方法)
	 */
	public static Method getGetMethodByPropertyName(Object object, String propertyName) throws DefaultException{
		return getMethodByName(object, "get"+toUpFirstLetter(propertyName),false);
	}
	
	/**
	 * 根据属性名,获取(set方法)
	 */
	public static Method getSetMethodByPropertyName(Object object, String propertyName) throws DefaultException{
		return getMethodByName(object, "set"+toUpFirstLetter(propertyName),true);
	}
}

你可能感兴趣的:(sql)