Properties 解析类
package com.pj.utils;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
/**
* Properties 解析类
* @author pj
*/
public class PropertiesUtil {
private String properiesName = "";
public PropertiesUtil() {
}
public PropertiesUtil(String fileName) {
this.properiesName = fileName;
}
/**
* 按key获取值
*
* @param key
* @return
*/
public String readProperty(String key) {
String value = "";
InputStream is = null;
try {
is = PropertiesUtil.class.getClassLoader().getResourceAsStream(properiesName);
Properties p = new Properties();
p.load(is);
value = p.getProperty(key);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return value;
}
/**
* 获取整个配置信息
*
* @return
*/
public Properties getProperties() {
Properties p = new Properties();
InputStream is = null;
try {
is = PropertiesUtil.class.getClassLoader().getResourceAsStream(properiesName);
p.load(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return p;
}
/**
* key-value写入配置文件
*
* @param key
* @param value
*/
public void writeProperty(String key, String value) {
InputStream is = null;
OutputStream os = null;
Properties p = new Properties();
try {
is = new FileInputStream(properiesName);
// is = PropertiesUtil.class.getClassLoader().getResourceAsStream(properiesName);
p.load(is);
// os = new FileOutputStream(PropertiesUtil.class.getClassLoader().getResource(properiesName).getFile());
os = new FileOutputStream(properiesName);
p.setProperty(key, value);
p.store(os, key);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is)
is.close();
if (null != os)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// sysConfig.properties(配置文件)
PropertiesUtil p = new PropertiesUtil("sysConfig.properties");
System.out.println(p.getProperties().get("db.url"));
System.out.println(p.readProperty("db.url"));
PropertiesUtil q = new PropertiesUtil("resources/sysConfig.properties");
q.writeProperty("myUtils", "wang");
System.exit(0);
}
}
mapper中的test表达式工具类
主要是为了在ognl表达式判断字段是否为空及相等时可以减少长长的表达式
提供了4个方法
1. Map,Collection,String,Array,Long是否为空 (isEmpty 空返回true)
2. Map,Collection,String,Array,Long是否不为空 ( isNotEmpty 不空返回true)
3. 是否相等(equals)
4. 是否不相等(notEquals)
/**
* Ognl工具类
* 主要是为了在ognl表达式判断字段是否为空及相等时可以减少长长的表达式
*
* "@Ognl@method(param)"
*
* 示例使用:
* <if test="@Ognl@isNotEmpty(status)">
* AND are.status = #{status}
* </if>
*
*/
public class Ognl {
/**
* 可以用于判断 Map,Collection,String,Array,Long是否为空
*/
public static boolean isEmpty(Object o) throws IllegalArgumentException {
return CommonUtil.isEmpty(o);
}
/**
* 可以用于判断 Map,Collection,String,Array是否不为空
*/
public static boolean isNotEmpty(Object o) {
return !isEmpty(o);
}
/**
* 可以用于判断Long类型是否不为空
*/
public static boolean isNotEmpty(Long o) {
return !isEmpty(o);
}
/**
* 判断是否相等
*/
public static boolean equals(Object o1, Object o2) {
return o1.equals(o2);
}
/**
* 判断是否不相等
*/
public static boolean notEquals(Object o1, Object o2) {
return !equals(o1, o2);
}
}
/**
*
* 计算时效
* @param startTime 开始时间
* @param endTime 结束时间
* @return 时效
*/
public String calculateAging(Date startTime, Date endTime)
throws ParseException {
if (isEmpty(endTime) || isEmpty(startTime)) {
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
startTime = sdf.parse(sdf.format(startTime));
endTime = sdf.parse(sdf.format(endTime));
Long millisecond = startTime.getTime() - endTime.getTime();
if ("0".equals(millisecond.toString())) {
return "0";
}
int d = 24 * 60 * 60 * 1000;
int h = 60 * 60 * 1000;
int m = 60 * 1000;
Long day = millisecond / d;
Long hour = (millisecond % d) / h;
Long minute = ((millisecond % d) % h) / m;
return day + "天" + hour + "小时" + minute + "分钟";
}
判空
public static boolean isEmpty(Object o) {
if (o == null) {
return true;
}
if ((o instanceof String)) {
if (((String)o).trim().length() == 0) {
return true;
}
}
else if ((o instanceof Collection)) {
if (((Collection)o).isEmpty()) {
return true;
}
}
else if (o.getClass().isArray()) {
if (((Object[])o).length == 0) {
return true;
}
}
else if (((o instanceof Map)) && (((Map)o).isEmpty())) {
return true;
}
return false;
}