RT
解决方案 »
- 【在线等】 weblogic 下部署 struts 1.3
- ★★★WebLogic7发布ear包报错了★★
- 求高手帮忙写一条SQL,想了2天都没实现想要的结果。。。
- 一个Entity的困惑
- Java支持藏文彝文报表预览,实现技术探讨交流!
- 急啊,高分求助关于通过web services相互交互文件附件的实例
- 客户端测试程序调用ejb出错
- jsp(j2sdk1.4.2+tomcat5.0)链接mysql出错:package news does not exist。
- ssh框架小问题,求助!!!!!!!!!
- druid远程连接sqlserver2000遇到以下问题,求解
- 求教myeclipse课程设计,烦了我两天了,数据库链接No suitable driver found for url
- 关于在HQL中使用date_add()函数的问题
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Convert {
// 没有什么实际内容可以定义的,留空
}
@Retention元标注千万别忘记了,否则反射不出来,等于白干。2)然后自己写一个工具类,专门用来转换。package net.csdn.bank.util;import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;import net.csdn.bank.annotation.Convert;
import net.csdn.bank.domain.Account;public abstract class ConvertUtils { private static String[] TABLE = "零,壹,貮,叁,肆,伍,陆,柒,捌,玖".split(",");
public static String convert(String string) {
StringBuilder sb = new StringBuilder();
for (int i=0; i<string.length(); i++) {
char c = string.charAt(i);
if (c < '0' || c > '9') {
sb.append(c);
} else {
sb.append(TABLE[c-'0']);
}
}
return sb.toString();
}
public static Collection convert(Collection c) throws Exception {
for (Object o : c) {
convert(o);
}
return null;
}
private static void convert(Object obj) throws Exception {
Class klass = obj.getClass();
Field[] fs = klass.getDeclaredFields();
for (Field f : fs) {
Annotation a = f.getAnnotation(Convert.class);
if (a != null || f.getType() == String.class) {
f.setAccessible(true);
String oldValue = (String) f.get(obj);
String newValue = convert(oldValue);
f.set(obj, newValue);
}
}
}
}转换的逻辑看楼主这么多勋章,我就随便弄两下。就不跑出来丢人现眼了。3)测试用的实体类, 账户类package net.csdn.bank.domain;import net.csdn.bank.annotation.Convert;public class Account { private Integer id;
private String name;
@Convert
private String balance; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getBalance() {
return balance;
} public void setBalance(String balance) {
this.balance = balance;
}
}注意看我标注加的位置,在field上,不是在setter上。4)再来个测试吧public static void main(String[] args) throws Exception {
List<Account> list = new ArrayList<Account>();
for (int i = 0; i<10; i++) {
Account account = new Account();
account.setName("zhangsan");
account.setBalance("12345");
list.add(account);
}
ConvertUtils.convert(list);
for (Account a : list) {
System.out.println(a.getBalance());
}
}
结果壹貮叁肆伍
壹貮叁肆伍
壹貮叁肆伍
壹貮叁肆伍
壹貮叁肆伍
壹貮叁肆伍
壹貮叁肆伍
壹貮叁肆伍
壹貮叁肆伍
壹貮叁肆伍
6)关于使用时机,建议以AOP方式使用ConvertUtils类。
比如在在Dao方法上横切一下,把查询的结果来一个“无声无息地”转换
/**
* 描述: 将数字改成人民币大写
*
* @param strVal
* 输入的值 如:125.76
* @return 返回值:String 返回人民币大写
*/
public static String changeToBig(String strVal) {
if (strVal == null || "null".equals(strVal) || "".equals(strVal))
strVal = "0";
if (strVal.equals("0"))
return "零圆整";
double value = Double.parseDouble(strVal);
if (value == 0)
return "零圆整";
char[] hunit = { '拾', '佰', '仟' }; // 段内位置表示
char[] vunit = { '万', '亿' }; // 段名表示
char[] digit = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; // 数字表示
long midVal = (long) (value * 100); // 转化成整形
String valStr = String.valueOf(midVal); // 转化成字符串
String head = valStr.substring(0, valStr.length() - 2); // 取整数部分
String rail = valStr.substring(valStr.length() - 2); // 取小数部分
String prefix = ""; // 整数部分转化的结果
String suffix = ""; // 小数部分转化的结果
// 处理小数点后面的数
if (rail.equals("00")) { // 如果小数部分为0
suffix = "整";
} else {
suffix = digit[rail.charAt(0) - '0'] + "角"
+ digit[rail.charAt(1) - '0'] + "分"; // 否则把角分转化出来
}
// 处理小数点前面的数
char[] chDig = head.toCharArray(); // 把整数部分转化成字符数组
char zero = '0'; // 标志'0'表示出现过0
byte zeroSerNum = 0; // 连续出现0的次数
for (int i = 0; i < chDig.length; i++) { // 循环处理每个数字
int idx = (chDig.length - i - 1) % 4; // 取段内位置
int vidx = (chDig.length - i - 1) / 4; // 取段位置
if (chDig[i] == '0') { // 如果当前字符是0
zeroSerNum++; // 连续0次数递增
if (zero == '0') { // 标志
zero = digit[0];
} else if (idx == 0 && vidx > 0 && zeroSerNum < 4) {
prefix += vunit[vidx - 1];
zero = '0';
}
continue;
}
zeroSerNum = 0; // 连续0次数清零
if (zero != '0') { // 如果标志不为0,则加上,例如万,亿什么的
prefix += zero;
zero = '0';
}
prefix += digit[chDig[i] - '0']; // 转化该数字表示
if (idx > 0)
prefix += hunit[idx - 1];
if (idx == 0 && vidx > 0) {
prefix += vunit[vidx - 1]; // 段结束位置应该加上段名如万,亿
}
} if (prefix.length() > 0)
prefix += '圆'; // 如果整数部分存在,则有圆的字样
return prefix + suffix; // 返回正确表示
}