SQL Objects API

编程入门 行业动态 更新时间:2024-10-25 20:31:30
SQL Objects API - 获取所有列?(SQL Objects API - Get all columns?)

我在我的数据访问对象的以下代码行中使用SQL对象API从SQL表中的行获取名称字段,其中ID与提供的ID匹配。

@SqlQuery("select name from users where id = :id") String findNameById(@Bind("id") int id);

这一切都很好,但是如果我想返回表中该行的所有字段,不仅仅是名称,还有该记录的所有列,我该怎么做?

我尝试过使用:

"select * from users where id = :id"

但这不对,它似乎只返回id字段(可能因为它是表中的第一列,我的方法返回一个字符串?)

无论如何,我完全没有这方面的想法,并将感谢我能得到的所有帮助。


我收到信息请求的附加信息:

这是调用我的dao的控制器中我的方法的方法签名。

public String retrieveUserRecord(@PathParam("id") int id)

如您所见,它返回一个字符串,dao中的findNameByID方法也是如此...

我想要做的是,使用我的方法签名传递id,并尽可能将我的所有列作为一个好的JSON对象返回?

I am using the SQL objects API in the following lines of code in my data access object to get the name field from a row in an SQL table where the ID matches the one provided.

@SqlQuery("select name from users where id = :id") String findNameById(@Bind("id") int id);

That works all well and good, but what on earth do I do if I want to return all of the fields for that row in the table, not just the name, but all colums for that record?

I did try using :

"select * from users where id = :id"

But that isn't right, it only seemed to return the id field (possibly as it is the first column in the table, and my method returns a string?)

Anyhow, I am all out of ideas on this and would appreciate all the help I can get.


Additional Info as I get requests for information :

This is the method signature of my method in the controller that calls my dao.

public String retrieveUserRecord(@PathParam("id") int id)

As you can see, it returns a string, as does the findNameByID method in the dao...

What I want to do, is pass the id using my method signature, and get back all of my columns as one nice JSON object if possible?

最满意答案

如果要获取所有字段,则返回类型不应为字符串。 我假设你有这样的'USER'模型。

public class User { private Integer id; private String name; public User(id, name) { this.id = id; this.name = name; } }

有一些方法可以将DB结果映射到Java模型。 您应该为类用户创建映射器。

public class UserMapper<User> implements ResultSetMapper<User> { public User map(int row, ResultSet rs, StatementContext ctx) throws SQLException { return new User(rs.getInt("id"), rs.getString("name") ); } }

你应该在sqlObject api中提到这个mapper。

@SqlQuery("select * from users where id = :id") @Mapper(UserMapper.class) String retrieveUserRecord(@Bind("id") int id);

如果模型中有setter和getter,也可以使用MapResultAsBean。

您也可以使用此库 。

Return type should not be string when you want to get all the fields. I am assuming you have 'USER' model like this.

public class User { private Integer id; private String name; public User(id, name) { this.id = id; this.name = name; } }

There are some ways to map DB result to Java model. You should create mapper for class user.

public class UserMapper<User> implements ResultSetMapper<User> { public User map(int row, ResultSet rs, StatementContext ctx) throws SQLException { return new User(rs.getInt("id"), rs.getString("name") ); } }

You should mention this mapper in sqlObject api.

@SqlQuery("select * from users where id = :id") @Mapper(UserMapper.class) String retrieveUserRecord(@Bind("id") int id);

You can also use MapResultAsBean if you have setters & getters in the model.

You can also use this library.

更多推荐

本文发布于:2023-08-07 14:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464894.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SQL   Objects   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!