数据库中定义如下:
public static void testFindProfileBean() {
ProgramFileDao profiledao = new ProgramFileDaoImpl();
try {
ProgramFile profilefind = profiledao.findById(1, 29);
System.out.println("final"+profilefind.getinstruName());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public ProgramFile findById(int ProIDHost, int ProIDPatch) throws Exception {
// TODO Auto-generated method stub
String sql = " SELECT ProVerHost,ProVerPatch,InstruName,AuthId,IfNotSpecial,SpecialVer,BinFile FROM t_program WHERE ProVerHost = ? AND ProVerPatch = ?";
// ProgramFile profile = null;
ProgramFile profile = (ProgramFile) db.getObject(ProgramFile.class, sql, new Object[] {ProIDHost,ProIDPatch });
return profile;
}
结果运行testFindProfileBean 后出现问题 打印为null
final:null
往下挖底层代码,在里面增加控制台打印调试:
public Object getObject(Class<?> type, String sql,Object[] paramList) throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(type);
Object obj = type.newInstance();
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
Map map = getObject(sql, paramList);
System.out.println("Map::::"+map);
for (int i = 0; i< propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
System.out.println("propertyName:"+propertyName);
if (map.containsKey(propertyName)) {
Object value = map.get(propertyName);
System.out.println("Object value:"+value);
Object[] args = new Object[1];
args[0] = value;
descriptor.getWriteMethod().invoke(obj, args);
System.out.println("Object."+propertyName+":"+value);
}
}
return obj;
}
打印出来的如下:
Map::::AuthID IfNotSpecial InstruName
....
propertyName:authID
propertyName:class
propertyName:ifNotSpecial
Object value:true
Object.ifNotSpecial:true
propertyName:instruName
Object value:3060-B
Object.instruName:3060-B
propertyName:proVerHost
Object value:1
Object.proVerHost:1
原来出错的原因为数据库中的字段名字和要转换成的实体类中的名字大写不一致:if (map.containsKey(propertyName))
没有判断成功,
最终obj对象中没有添加进去实际值
数据库表格中 AuthID != authID
可是我实体类中的属性明明是开头首字母大写的啊,为什么PropertyDescriptor 打印出来的首字母变成小写了??
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
参见说明
https://bbs.csdn.net/topics/360211521
原帖:http://pouyang.iteye.com/blog/634844
他说了JavaBean的属性名前两个字母要不都大写,要不都小写,然后用Java源码进行揭秘。
问题是看到最后一段代码:
public static String decapitalize(String s)
{
if(s == null || s.length() == 0)
//空处理
return s;
if(s.length() > 1 && Character.isUpperCase(s.charAt(1)) && Character.isUpperCase(s.charAt(0))){
//长度大于1,并且前两个字符大写时,返回原字符串
return s;
} else{
//其他情况下,把原字符串的首个字符小写处理后返回
char ac[] = s.toCharArray();
ac[0] = Character.toLowerCase(ac[0]);
return new String(ac);
}
}
真是个坑啊 会自动修改大小写 好吧 我承认主要是我经验不足。。。
public class ProgramFile {
private int AuthID ;
file 对象不等于 数据库中的blob
原来 实体类中 定义binFile来接受数据库中的bin文件
public class ProgramFile {
private int proVerHost; // 程序版本号host
private int proVerPatch; // 程序版本号patch
private String instruName; // 对应仪器的ID
private int authID; // 程序作者
private boolean ifNotSpecial;
private int specialVer;
private File binFile;
数据库中字段类型定义如下:
结果报错:
Object value:456
Object.authID:456
propertyName:binFile
Object value:[B@455abe01
java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.bbs.util.DBConnection.getObject(DBConnection.java:406)
at com.bbs.dao.impl.ProgramFileDaoImpl.findById(ProgramFileDaoImpl.java:53)
at com.bbs.test.DBTestProgramFile.testFindProfileBean(DBTestProgramFile.java:105)
at com.bbs.test.DBTestProgramFile.main(DBTestProgramFile.java:119)
按如下修改后正常
public class ProgramFile {
private int proVerHost; // 程序版本号host
private int proVerPatch; // 程序版本号patch
private String instruName; // 对应仪器的ID
private int authID; // 程序作者
private boolean ifNotSpecial;
private int specialVer;
private byte[] binFile;
byte[] aaa 就是数组
直接打印得到的是
testAddProfileBean();
ProgramFile pf1 = testFindProfileBean();
byte[] aaaa = pf1.getbinFile();
System.out.println(aaaa);
System.out.println(aaaa[0]);
System.out.println(aaaa[1]);
System.out.println(aaaa[2]);
System.out.println(aaaa[3]);
打印结果:
PF:ProgramFile [Version=2. 35instruName=2037, authID=123]
Flag = 1
final2037
[B@4057e145
7
8
9
0