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

qq_38494246 2017-04-25 12:27:17
上一部分代码:
public static <T> List<T> getForList(Class<T> clazz, String sql,
Object ... args){

List<T> list = new ArrayList<>();
Connection conn = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
conn = JDBCTools.getConnection();
preparedStatement = (PreparedStatement) conn.prepareStatement(sql);
for(int i = 0; i < args.length; i++){
preparedStatement.setObject(i+1, args[i]);
}

resultSet = preparedStatement.executeQuery();

ResultSetMetaData rsmd = (ResultSetMetaData) resultSet.getMetaData();
Map<String, Object> map = null;
List<Map<String, Object>> listMap = new ArrayList<>();
while(resultSet.next()){
map = new HashMap<>();
for(int i = 0; i < rsmd.getColumnCount(); i++){
String columnLable = rsmd.getColumnLabel(i+1);
Object columnValue = resultSet.getObject(i+1);
map.put(columnLable, columnValue);
}
listMap.add(map);
}
T bean = null;
if(listMap.size() > 0){
for(Map<String, Object> map1 : listMap){
bean = clazz.newInstance();
for(Map.Entry<String, Object> entry : map1.entrySet()){

String key = entry.getKey();
Object value = entry.getValue();
System.out.println(key); //debug
System.out.println(value); //debug
BeanUtils.setProperty(bean, key, value); //问题出在这,感觉bean没被赋值。

}
list.add(bean);
}
}

} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.release(resultSet, preparedStatement, conn);
}

return list;
}

// 测试类
@Test
public void testGetForList2(){
String sql = "SELECT ID, Name, Age, Email from students";
List<Student> students = DAO.getForList(Student.class, sql);
System.out.println(students);
}

// 结果输出:从打印结果可以看出,两条debug的输出是有数据的,怎么紧接着的BeanUtils.setProperty(bean, key, value), 没有对bean对象赋值呢? 求大神解答。

Email
zhangsan@123.com
ID
1
Age
18
Name
zhang, sanfeng
Email
lisi@123.com
ID
2
Age
19
Name
li, si
Email
wangwu@123.com
ID
3
Age
20
Name
Wang, wu
Email
zhaoliu@123.com
ID
4
Age
21
Name
Zhao, liu
Email
wangyuan@123.com
ID
6
Age
22
Name
Wang, yuan
Email
mazi@jd.com
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]
]

...全文
1216 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhjl1989 2018-11-22
  • 打赏
  • 举报
回复
是你key的值 第一个字母是大写的问题 如:Name beanUtils.setProperty 是基于set方法进行封装, public static void main(String[] args) throws InvocationTargetException, IllegalAccessException { String value = "abc"; Student student = new Student(); BeanUtils.setProperty(student,"name",value); System.out.println(student); } 这样打印出来是 Student{name='abc'} public static void main(String[] args) throws InvocationTargetException, IllegalAccessException { String value = "abc"; Student student = new Student(); BeanUtils.setProperty(student,"Name",value); System.out.println(student); } 这样打印出来是 Student{name='null'}
大田鼠 2018-11-01
  • 打赏
  • 举报
回复
beanUtils.setProperty 是基于对象属性的set方法,以提供的key拼接成set方法;并且对象必须符合javabean规范
黑豆怕红豆 2017-05-31
  • 打赏
  • 举报
回复
不知楼主的问题解决了没有,我今天也遇见了这个问题Student [FlowID=0, Type=0, IDCard=22222222222222, ExamCard=null, StudentName=null, Location=null, Grade=0] 奇怪的是只有IDCard赋上值了,Student类也很正常无错误,想破脑袋液想不明白,如果楼主问题解决了,求帮助 //测试类 @Test public void testGet() { String sql = "select Flow_ID as FlowID,Type,ID_Card IDCard," + "Exam_Card ExamCard,Student_Name StudentName,Location," + "Grade from stud_infor Where Grade=?;"; Student student = dao.get(Student.class, sql, 60); System.out.println(student); } //查询方法 public <T> T get(Class<T> clazz,String sql,Object...args){ T entity = null; Connection connection =null; PreparedStatement ps =null; ResultSet rs= null; try { // connection=JDBCTools.getConnection(); ps=connection.prepareStatement(sql); for (int i = 0; i < args.length; i++) { ps.setObject(i+1, args[i]); } rs=ps.executeQuery(); // ResultSetMetaData rsmd = rs.getMetaData(); // Map<String,Object> map = new HashMap<String,Object>(); if(rs.next()){ for (int i = 0; i <rsmd.getColumnCount(); i++) { String columnLable=rsmd.getColumnLabel(i+1); Object columnValue = rs.getObject(columnLable); map.put(columnLable, columnValue); } } // if(map.size()>0){ entity = clazz.newInstance(); for (Map.Entry<String,Object> entry: map.entrySet()) { String fieldName=entry.getKey(); Object fieldValue=entry.getValue(); //ReflectionUtils.setFieldValue(entity, fieldName, fieldValue); BeanUtils.setProperty(entity, fieldName, fieldValue); } } } catch (Exception e) { e.printStackTrace(); }finally{ JDBCTools.release(rs, ps, connection); } return entity; }
  • 打赏
  • 举报
回复
我和你出现啦一样的错误!改啦还几天才整过来 你这个错误原因应该是:根据键key和无法将值value初始化化到对象中去才会出现你这个问题 所以可能是你这个student对象的类的权限设置的小啦 你改为public的话就对啦

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧