小编Til*_*man的帖子

如何使用JDBI SQL对象API创建一对多关系?

我正在使用使用JDBI的dropwizard创建一个简单的REST应用程序.下一步是集成一个与另一个具有一对多关系的新资源.到目前为止,我无法弄清楚如何在我的DAO中创建一个方法来检索一个包含另一个表中对象列表的对象.

POJO表示将是这样的:

public class User {

    private int id;
    private String name;

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public class Account {

    private int id;
    private String name;
    private List<User> users;

    public Account(int id, String name, List<User> users) {
        this.id …
Run Code Online (Sandbox Code Playgroud)

java join one-to-many jdbi dropwizard

27
推荐指数
2
解决办法
1万
查看次数

在Spring Boot中启用HAL序列化以获取自定义控制器方法

我正在尝试使用spring-boot-starter-data-rest使用Spring Boot构建RESTful API.有一些实体:帐户,交易,类别和用户 - 只是通常的东西.

当我通过默认生成的API 检索http:// localhost:8080/transactions中的对象时,一切顺利,我得到一个包含所有事务的列表作为JSON对象,如下所示:

{
  "amount": -4.81,
  "date": "2014-06-17T21:18:00.000+0000",
  "description": "Pizza",
  "_links": {
    "self": {
      "href": "http://localhost:8080/transactions/5"
    },
    "category": {
      "href": "http://localhost:8080/transactions/5/category"
    },
    "account": {
      "href": "http://localhost:8080/transactions/5/account"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但现在的目标是仅检索该URL下的最新事务,因为我不想序列化整个数据库表.所以我写了一个控制器:

@Controller
public class TransactionController {
    private final TransactionRepository transactionRepository;

    @Autowired
    public TransactionController(TransactionRepository transactionRepository) {
        this.transactionRepository = transactionRepository;
    }

    // return the 5 latest transactions
    @RequestMapping(value = "/transactions", method = RequestMethod.GET)
    public @ResponseBody List<Transaction> getLastTransactions() {
        return  transactionRepository.findAll(new PageRequest(0, 5, new Sort(new Sort.Order(Sort.Direction.DESC, "date")))).getContent(); …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-data-rest spring-hateoas spring-boot

11
推荐指数
1
解决办法
6103
查看次数