create table author ( id int primary key, name varchar(20));create table post ( id int primary key, authorId int, title varchar(100), created timestamp, content text, foreign key (authorId) REFERENCES author(id));create table comment ( id int primary key, postId int, content text, created timestamp, foreign key (postId) REFERENCES post(id));
在java类中,Author, Post和Comment分别对应的java类型为:
public class Post { private int id; private Author author; private String title; private String content; private Timestamp created; private List comments; // getters and setters}public class Author { private int id; private String name; // getters and setters}public class Comment { private int id; private int postId; private String content; private Timestamp created; // getters and setters}
select A.id, A.title, A.content, A.created, B.id as authorId, B.name, C.id as commentId, C.content as comment, C.created as commentCreated from post A join author B on A.authorid=B.id join comment C on A.id = C.postId select A.id, A.title, A.content, A.created, B.id as authorId, B.name, C.id as commentId, C.content as comment, C.created as commentCreated from post A join author B on A.authorid=B.id join comment C on A.id = C.postId where A.id=#{param1}