Mybatis 多表查询(注解方式)

2020年02月02日 111点热度 0人点赞 0条评论

之前一直用的hibernate和nutz,第一次用mybatis,不太熟悉,记录下来防止忘记= =

配置文件

为了少写点@Result,可在设置中开启下划线转驼峰

mybatis:
  configuration:
    mapUnderscoreToCamelCase: true

实体类

@Data
@Table(name = "sz_article")
public class Article implements Serializable {
    @Id
    @KeySql(useGeneratedKeys = true)
    private Long id;
    @NotNull(message = "分类不能为空")
    private Long categoryId;
    private Category category;
    private Long userId;
    @NotEmpty(message = "标题不能为空")
    private String title;
    private String content;
    private String image;
    private Date createTime;
    private Date updateTime;
    private Boolean sticky;
    private Integer status;
    private Boolean commentDisabled;
}

实体类中最好都使用包装类,今天在用通用mapper时,遇到非包装类会出现点问题= =

Mapper

    @Select("select sz_article.*,sz_article_category.name from sz_article left join sz_article_category on sz_article.category_id=sz_article_category.id")
    @Results(@Result(property = "category.name",column = "name"))
    List<Article> selectAll();

likole

一个后端小白

文章评论