好吧,这真的很尴尬,我制作了一个标准的 pojo 类及其 dao 类用于数据检索目的。我很难理解如何处理 Pojo 类的自定义查询数据的基本过程。
假设我的用户类别是
public class User{
private int userId;
private String username;
private int addressId;
}
public class Address{
private int addressId;
private String zip;
}
public class UserDAO{
public void getUserDetails(){
String getSql = select u.userId, u.username, a.zipcode from user u, address a where u.addressId = a.addressId;
//no pojo class is now specific to the resultset returned. so we can't map result to pojo object
}
}
Run Code Online (Sandbox Code Playgroud)
现在我应该如何用我的 pojo 类对此进行建模,就好像使用 String 来管理它一样,那么面向对象的概念就会消失,并且将来的复杂性也会增加。请指导!
更新以获取进一步说明
我们知道我们可以将相同的表对象映射到相同的 …
使用:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.5.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.8.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
这是我的 DTO。它不是数据库上的实体。我只想用它来绘制地图
public class PoolStateResult {
private Integer totalCodes;
private Integer assignedCodes;
private Integer availableCodes;
private Date startDateValidate;
private Date endDateValidate;
constructors
getters and setters
Run Code Online (Sandbox Code Playgroud)
这是DAO的内容
Query q = em.createNativeQuery(" SELECT p.start_validity_date as startDateValidate, "
+ " p.end_validity_date as endDateValidate, "
+ " (SELECT count(*) from POOL_CODES p WHERE …Run Code Online (Sandbox Code Playgroud) 最近我开始将我的 Android 项目从 Java 转换为 Kotlin。我在许多类中使用 Retrofit 从 API 获取数据。在我的项目中,有很多 POJO,我需要手动将它们转换为数据类,因为它与 Android Studio 的自动转换工具不能很好地配合。
考虑我有模型类:
public class LoginResponseMinimal {
String firstName, lastName, token;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
Run Code Online (Sandbox Code Playgroud)
有什么好方法可以直接将这个模型类转换为 Kotlin 中的数据类吗?这个类手动转换是可以的,但是我有一些类有大量变量,手动将其转换为数据类会很麻烦。
我是一个 SQL 菜鸟,不明白为什么我的语句会这样。它在 Android Room DAO 中使用并返回不需要的结果。
我有这样的声明:
@Transaction
@Query("Select Distinct Category.* " +
"from Category " +
"inner join Items on Category.ID = Items.Category " +
"where IsExcluded = 0 " +
"order by lower( Category.Name ) asc")
LiveData<List<CatViewWithItemList>> getCatViewWithItemListGlobal();
Run Code Online (Sandbox Code Playgroud)
结果由一个简单的 POJO 接收:
public class CatViewWithItemList {
@Embedded
public Cat myCat;
@Relation(parentColumn = "ID",
entityColumn = "Category") public List<ItemS> ItemList;
Run Code Online (Sandbox Code Playgroud)
问题: 我不明白结果:
为什么第 5 项被退回?它应该被 where 子句排除。我的说法有错误吗?
(编辑:我现在已经放弃了布尔转换器,Room 似乎可以在没有它的情况下解析布尔值)或者这可能是由我的布尔值简单类型转换器引起的?一和零实际上是布尔值的占位符:
@TypeConverter
public Boolean fromInt(int value) {
return …Run Code Online (Sandbox Code Playgroud) 我正在尝试序列化具有如下字段的 Java 实例。
\n\npublic class Person{\n private String firstName;\n private String lastName;\n\n public String getFirstName() {\n\n return firstName;\n }\n\n public void setFirstName(String firstName) {\n\n this.firstName = firstName;\n }\n\n public String getLastName() {\n\n return lastName;\n }\n\n public void setLastName(String lastName) {\n\n this.lastName = lastName;\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n如何使用与实际字段名称不同的名称来序列化它们?这Gson可以通过使用@SerializedName("first-name")注释来实现,如下所示。
@SerializedName("first-name")\nprivate String firstName;\nRun Code Online (Sandbox Code Playgroud)\n\n里面有没有类似上面的东西snakeyaml。依赖项详细信息snakeyaml如下,
<dependency>\n <groupId>org.yaml</groupId>\n <artifactId>snakeyaml</artifactId>\n <version>1.17</version>\n </dependency>\nRun Code Online (Sandbox Code Playgroud)\n\n下面是程序的主类,其答案由flyx
public class Demo {\n\n public static void main(String[] args) …Run Code Online (Sandbox Code Playgroud) 请耐心等待,我对架构组件和 Android 总体来说是新手。我的问题与这个问题类似,但不幸的是接受的答案似乎不起作用。我有一个像这个答案一样的一对多关系的例子 。我的示例数据库有两个表 USERS 和 PETS,如下图所示:


假设我想要获取一个用户列表,其中包含按用户 ID 分组的宠物列表(仅限5 岁以下的宠物)。结果应如下所示(伪代码):
{uId:2,[Pet3,Pet4];uId: 4, [Pet6, Pet7];}
另一个要求是 Dao 需要将列表作为 LiveData 对象返回,因为我使用 MVVM 架构并希望它具有生命周期感知和可观察性。
满足这些要求,UserDao将如下所示:
@Dao
interface UserDao {
@Insert
void insert(User user);
@Transaction
@Query("SELECT USERS.uId, PETS.pId , PETS.userId, PETS.age " +
"FROM USERS INNER JOIN PETS ON PETS.userId = USERS.uId " +
"WHERE PETS.age < 5 " +
"GROUP BY USERS.uId")
LiveData<List<UserWithPets>> getUserPets();
}
Run Code Online (Sandbox Code Playgroud)
用户实体:
@Entity
public class User {
@PrimaryKey
public …Run Code Online (Sandbox Code Playgroud) 我想知道为具有“anyOf”字段的 Json 模式生成 POJO 的推荐方法是什么?
例如,给定以下 json 模式:
爱好.json{
"anyOf": [
{ "type": {"$ref": "./exercise.json" } },
{ "type": {"$ref": "./music.json" } }
]
}
Run Code Online (Sandbox Code Playgroud)
练习.json
{
"type": "object"
"properties" {
"hobbyType": {"type": "string"}
"exerciseName": { "type": "string" },
"timeSpent": { "type": "number" },
"place": { "type": "string" }
}
}
Run Code Online (Sandbox Code Playgroud)
音乐.json
{
"type": "object"
"properties" {
"hobbyType": {"type": "string"}
"instrument": { "type": "string" },
"timeSpent": { "type": "number" }
}
}
Run Code Online (Sandbox Code Playgroud)
我如何为Hobby.javaJackson 生成 POJO?
我正在为名为 Schema 的集合执行 CRUD,并且需要将 de _id 作为十六进制字符串检索,但是当我使用 collection.find() 时,我得到的是时间戳和日期而不是字符串。
我收到这个结构:
{
"_id": {
"timestamp": 1604689898,
"date": "2020-11-06T19:11:38.000+00:00"
}
}
Run Code Online (Sandbox Code Playgroud)
但我需要这样的东西:
{
"_id": "5fa5a085a4b09b307d53ed57"
}
Run Code Online (Sandbox Code Playgroud)
这是我的配置
Pom.xml
{
"_id": {
"timestamp": 1604689898,
"date": "2020-11-06T19:11:38.000+00:00"
}
}
Run Code Online (Sandbox Code Playgroud)
MongoClient配置
{
"_id": "5fa5a085a4b09b307d53ed57"
}
Run Code Online (Sandbox Code Playgroud)
模式模型
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>co.com.itau</groupId>
<artifactId>crypto-mongodb-java-ms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>crypto-mongodb-java-ms</name>
<description>Microservice to save and read data from mongoDB using CSFLE</description>
<properties>
<java.version>1.8</java.version>
<version.fabric8-maven-plugin>3.5.41</version.fabric8-maven-plugin>
<swagger.version>3.0.0</swagger.version> …Run Code Online (Sandbox Code Playgroud) 我正在使用 log4j 2.11 作为其日志记录框架的 Java 解决方案。我还使用了一个极其频繁地使用特定 POJO 的库。我希望能够将该对象直接传递到日志语句中log.debug("POJO: %s", pojo),但遗憾的是该对象没有实现 toString 并且我无法修改库代码。
据我所知,log4j 1.x 通过 ObjectRenderer 解决了这个问题。您可以实现 ObjectRenderer,然后在 log4j.properties 文件中定义一行
log4j.renderer.com.library.Pojo=com.project.PojoRenderer,然后 log4j 将调用您的渲染器来提供该字符串值。
根据https://logging.apache.org/log4j/2.x/manual/compatibility.html,但似乎此功能并未纳入log4j2。是否有一个等效的功能可以用来直接记录它,而不必求助于包装器或辅助方法?
我正在使用Hibernate搜索POJO Generator并找到了这个Hibernate POJO Generator.
我下载了它的jar文件但找不到任何使用它的方法.
我想通过提供数据库表模式来生成所有与Hibernate DAO相关的类.
我知道另一种方法是使用eclipse插件,但我想从Hibernate POJO Generator生成类.