在实际过程中,经常要将实体类进行封装,尤其是处理数据库的过程中;因此,对于遍历实体类能够与数据库中的一行数据对应起来。

我是使用的环境是Spring boot,访问的数据库时MongoDB

实体类遍历:

 //java中遍历实体类,获取属性名和属性值
public static void testReflect(Object model) throws Exception{
for (Field field : model.getClass().getDeclaredFields()) {
field.setAccessible(true);
System.out.println(field.getName() + ":" + field.get(model) );
}
}

pom.xml需要配依赖

<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>

<version>1.9.3</version>
</dependency>

我项目中的代码:

     public String reflectData(DianpingShopEntity entry) throws Exception{
StringBuilder stringBuilder = new StringBuilder();
for (Field field : entry.getClass().getDeclaredFields()) {
field.setAccessible(true);
stringBuilder.append(field.get(entry)).append(',');
}
return stringBuilder.deleteCharAt(stringBuilder.length()-1).toString();
} public String reflectTitle(DianpingShopEntity entry) throws Exception{
StringBuilder stringBuilder = new StringBuilder();
for (Field field : entry.getClass().getDeclaredFields()) {
field.setAccessible(true);
stringBuilder.append(field.getName()).append(',');
}
return stringBuilder.deleteCharAt(stringBuilder.length()-1).toString();
}

Java中遍历实体类(处理MongoDB)的更多相关文章

  1. java中遍历实体类,获取属性名和属性值

    方式一(实体类): //java中遍历实体类,获取属性名和属性值 public static void testReflect(Object model) throws Exception{ for ...

  2. java中遍历实体类属性和类型

    public static void testReflect(Object model) throws NoSuchMethodException, IllegalAccessException, I ...

  3. java中遍历实体类属性和类型,属性值

    public static void testReflect(Object model) throws NoSuchMethodException, IllegalAccessException, I ...

  4. Java中的实体类--Serializable接口、transient 关键字

    在java中,实体类是一个非常重要的概念,我们可以在实体类中封装对象.设置其属性和方法等.关于实体类,也经常涉及到适配器模式.装饰者模式等设计模式.那么在实际代码开发中,关于实体类的注意事项有哪些呢? ...

  5. java反射遍历实体类属性和类型,并赋值和获取值

    /* * GetModelNameAndType.java * Version 1.0.0 * Created on 2017年12月15日 * Copyright ReYo.Cn */ packag ...

  6. java中为什么实体类需要实现序列化

    当客户端访问某个能开启会话功能的资源,web服务器就会创建一个HTTPSession对象,每个HTTPSession对象都会占用一定的内存,如果在同一个时间段内访问的用户太多,就会消耗大量的服务器内存 ...

  7. java中从实体类中取值会忽略的的问题

    在我们java Map中通过get来取值时会忽略的问题是:如果取得一个空值null时,那么.toString()时就会出错,而且不知道是什么原因. 现在我给的具体方法是用条件表达式先判断一下. 例: ...

  8. java中循环遍历实体类的属性和数据类型以及属性值

    package com.walkerjava.test; import java.lang.reflect.Field; import java.lang.reflect.InvocationTarg ...

  9. java中如何遍历实体类的属性和数据类型以及属性值

      package com.walkerjava.test; import java.lang.reflect.Field; import java.lang.reflect.InvocationTa ...

随机推荐

  1. Google Protobuf结合Netty实践

    1.Win版Protobuf代码生成工具下载: https://github.com/protocolbuffers/protobuf/releases 注意下载protoc-3.6.1-win32. ...

  2. (转)renren-fast解读(一)

    (二期)8.renren-fast项目解读(一) [课程八]预防xss...注入.xmind0.2MB [课程八预习]开...解读.xmind0.5MB 课程八_日志处理与...模块.xmind0.2 ...

  3. [POJ 2386] Lake Counting(DFS)

    Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...

  4. Component 初识组件

    component组件是Vue学习的重点.重点.重点,重要的事情说三遍.所以你必须学好Vue component.其实组件就是制作自定义的标签,这些标签在HTML中是没有的.比如:<diy> ...

  5. 【译】第14节---数据注解-MaxLength/MinLength

    原文:http://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribute-i ...

  6. CentOS下修改Apache默认端口80

    打开  /etc/httpd/conf/httpd.conf  文件 修改这个地方     #Listen 12.34.56.78:80     Listen 80 #把80改为你设置的端口,我设置端 ...

  7. python 启动新进程执行脚本

    import subprocess p_restart=subprocess.Popen(['/bin/sh','/etc/init.d/xxx_service','reboot'])

  8. oracle的批量操作sql语句

    1.批量删除/批量更新 mapper: <update id="updatePrjStateByFPrjId" parameterType="string" ...

  9. VS IIS Express 支持局域网访问

    使用Visual Studio开发Web网页的时候有这样的情况:想要在调试模式下让局域网的其他设备进行访问,以便进行测试.虽然可以部署到服务器中,但是却无法进行调试,就算是注入进程进行调试也是无法达到 ...

  10. 学习笔记58—3D杯子设计

    软件下载:http://www.i3done.com/ 界面如下: 3D杯子设计步骤(参考:http://www.i3done.com/news/video/402.html): 生成杯体 1.点击基 ...