如何使用单个URL映射检索数据并将其插入数据库

时间:2015-07-16 11:13:11

标签: java mysql spring hibernate rest

我正在使用Spring REST和hibernate,我希望在单个url映射中检索和更新数据到数据库中。我正在检索一个列数据,总结一下,我希望将该数据更新到另一个表但无法更新的数据。

这是我的 DAO

    public List<Post> getPostList() throws Exception {
session = sessionFactory.openSession();
    Criteria cr = session.createCriteria(Post.class);
    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.sum("val"));
    projList.add(Projections.groupProperty("userId"));
    cr.setProjection(projList);
    List postList = cr.list();
    tx = session.getTransaction();
    session.beginTransaction();
    tx.commit();
    return postList;
}

这是我的实体类 Post.java

@Entity
@Table(name="post")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Post implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
@Column(name="id")
private long id;

@Column(name="uid")
private long userId;

@Column(name="value")
private long val;

public long getId() {
    return id;
}

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

public long getUserId() {
    return userId;
}

public void setUserId(long userId) {
    this.userId = userId;
}

public long getVal() {
    return val;
}

public void setVal(long val) {
    this.val = val;
}

}

这是我的REST控制器

    @RequestMapping(value = "/posts", method = RequestMethod.GET)
public @ResponseBody
List<Post> getEmployee() {

    List<Post> postList = null;
    try {
        postList = profileService.getPostList();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return postList;
}

我想使用单个网址检索和更新数据。请提示我

0 个答案:

没有答案