使用 Spring 的 NamedParameterJdbcTemplate
将 POJO 插入下面的键值表的最简单方法是什么无需手动循环 遍历每个字段并调用插入?
POJO:
class Person {
String firstName;
String lastName;
int age;
...many more fields
}
键值 person_attributes
表:
attribute | value
---------------------
firstName | Bob
lastName | Billy
age | 30
最佳答案
您可以使用 jackson 的 ObjectMapper将 POJO 转换为键值对的类
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> personMap = mapper.convertValue(person, Map.class);
然后迭代每个映射条目并将其插入数据库
或者您可以使用Apache commons beanutils
关于java - 如何将 POJO 插入到键值数据库表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34400987/