BeanUtils.setProperty(bean, key, value)怎么没起作用啊

[复制链接]
查看11 | 回复4 | 2021-1-27 06:43:52 | 显示全部楼层 |阅读模式
上一部分代码:
publicstaticListgetForList(Classclazz,Stringsql,
Object...args){
Listlist=newArrayList();
Connectionconn=null;
PreparedStatementpreparedStatement=null;
ResultSetresultSet=null;
try{
conn=JDBCTools.getConnection();
preparedStatement=(PreparedStatement)conn.prepareStatement(sql);
for(inti=0;imap=null;
List>listMap=newArrayList();
while(resultSet.next()){
map=newHashMap();
for(inti=0;i0){
for(Mapmap1:listMap){
bean=clazz.newInstance();
for(Map.Entryentry:map1.entrySet()){
Stringkey=entry.getKey();
Objectvalue=entry.getValue();
System.out.println(key);//debug
System.out.println(value);//debug
BeanUtils.setProperty(bean,key,value);//问题出在这,感觉bean没被赋值。
}
list.add(bean);
}
}
}catch(Exceptione){
e.printStackTrace();
}finally{
JDBCTools.release(resultSet,preparedStatement,conn);
}
returnlist;
}
//测试类
@Test
publicvoidtestGetForList2(){
Stringsql="SELECTID,Name,Age,Emailfromstudents";
Liststudents=DAO.getForList(Student.class,sql);
System.out.println(students);
}
//结果输出:从打印结果可以看出,两条debug的输出是有数据的,怎么紧接着的BeanUtils.setProperty(bean,key,value),没有对bean对象赋值呢?求大神解答。
Email
[email protected]
ID
1
Age
18
Name
zhang,sanfeng
Email
[email protected]
ID
2
Age
19
Name
li,si
Email
[email protected]
ID
3
Age
20
Name
Wang,wu
Email
[email protected]
ID
4
Age
21
Name
Zhao,liu
Email
[email protected]
ID
6
Age
22
Name
Wang,yuan
Email
[email protected]
ID
7
Age
20
Name
wang,mazi
[Student[id=0,name=null,age=0,email=null]
,Student[id=0,name=null,age=0,email=null]
,Student[id=0,name=null,age=0,email=null]
,Student[id=0,name=null,age=0,email=null]
,Student[id=0,name=null,age=0,email=null]
,Student[id=0,name=null,age=0,email=null]
]

分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:43:52 | 显示全部楼层
我和你出现啦一样的错误!改啦还几天才整过来
你这个错误原因应该是:根据键key和无法将值value初始化化到对象中去才会出现你这个问题
所以可能是你这个student对象的类的权限设置的小啦你改为public的话就对啦
回复

使用道具 举报

千问 | 2021-1-27 06:43:52 | 显示全部楼层
不知楼主的问题解决了没有,我今天也遇见了这个问题Student[FlowID=0,Type=0,IDCard=22222222222222,ExamCard=null,StudentName=null,Location=null,Grade=0]奇怪的是只有IDCard赋上值了,Student类也很正常无错误,想破脑袋液想不明白,如果楼主问题解决了,求帮助

//测试类
@Test
publicvoidtestGet(){
Stringsql="selectFlow_IDasFlowID,Type,ID_CardIDCard,"
+"Exam_CardExamCard,Student_NameStudentName,Location,"
+"Gradefromstud_inforWhereGrade=?;";
Studentstudent=dao.get(Student.class,sql,60);
System.out.println(student);
}
//查询方法
publicTget(Classclazz,Stringsql,Object...args){
Tentity=null;
Connectionconnection=null;
PreparedStatementps=null;
ResultSetrs=null;
try{
//
connection=JDBCTools.getConnection();
ps=connection.prepareStatement(sql);
for(inti=0;imap=newHashMap();
if(rs.next()){
for(inti=0;i0){
entity=clazz.newInstance();
for(Map.Entryentry:map.entrySet()){
StringfieldName=entry.getKey();
ObjectfieldValue=entry.getValue();
//ReflectionUtils.setFieldValue(entity,fieldName,fieldValue);
BeanUtils.setProperty(entity,fieldName,fieldValue);
}
}
}catch(Exceptione){
e.printStackTrace();
}finally{
JDBCTools.release(rs,ps,connection);
}
returnentity;
}

回复

使用道具 举报

千问 | 2021-1-27 06:43:52 | 显示全部楼层
beanUtils.setProperty是基于对象属性的set方法,以提供的key拼接成set方法;并且对象必须符合javabean规范
回复

使用道具 举报

千问 | 2021-1-27 06:43:52 | 显示全部楼层
是你key的值第一个字母是大写的问题如:NamebeanUtils.setProperty是基于set方法进行封装,
publicstaticvoidmain(String[]args)throwsInvocationTargetException,IllegalAccessException{
Stringvalue="abc";
Studentstudent=newStudent();
BeanUtils.setProperty(student,"name",value);
System.out.println(student);
}
这样打印出来是Student{name='abc'}
publicstaticvoidmain(String[]args)throwsInvocationTargetException,IllegalAccessException{
Stringvalue="abc";
Studentstudent=newStudent();
BeanUtils.setProperty(student,"Name",value);
System.out.println(student);
}
这样打印出来是Student{name='null'}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行