mybatis 注解的方式查询数据,查询参数中有List,返回结果也为list

一、po层的代码

package com.zc.entities.po;

import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * Created by ZC
 */
@Data
public class Blog {

    private Long id;
    private String title;
    private String content;
    private String firstPicture;
    private String flag;
    private Integer views;
    private boolean appreciation;
    private boolean shareStatement;
    private boolean commentabled;
    private boolean published;
    private boolean recommend;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createTime;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date updateTime;
    private Long typeId;
    private Long userId;

    private String description;
}

二、dao层的代码

其中,经过我的测试,@Results注解内的代码不要也可以正常运行,但个人推荐加上,使之能够与po层一一对应

 @Select({
            ""
    })
    @Results({
            @Result(column = "id",property = "id",jdbcType = JdbcType.BIGINT),
            @Result(column = "appreciation",property = "appreciation",jdbcType = JdbcType.BOOLEAN),
            @Result(column = "commentabled",property = "commentabled",jdbcType = JdbcType.BOOLEAN),
            @Result(column = "content",property = "content",jdbcType = JdbcType.LONGVARCHAR),
            @Result(column = "createTime",property = "createTime",jdbcType = JdbcType.DATE),
            @Result(column = "firstPicture",property = "firstPicture",jdbcType = JdbcType.VARCHAR),
            @Result(column = "flag",property = "flag",jdbcType = JdbcType.VARCHAR),
            @Result(column = "published",property = "published",jdbcType = JdbcType.BOOLEAN),
            @Result(column = "recommend",property = "recommend",jdbcType = JdbcType.BOOLEAN),
            @Result(column = "shareStatement",property = "shareStatement",jdbcType = JdbcType.BOOLEAN),
            @Result(column = "title",property = "title",jdbcType = JdbcType.VARCHAR),
            @Result(column = "updateTime",property = "updateTime",jdbcType = JdbcType.DATE),
            @Result(column = "views",property = "views",jdbcType = JdbcType.INTEGER),
            @Result(column = "typeId",property = "typeId",jdbcType = JdbcType.BIGINT),
            @Result(column = "userId",property = "userId",jdbcType = JdbcType.BIGINT),
            @Result(column = "description",property = "description",jdbcType = JdbcType.LONGVARCHAR)
    })
    List<Blog>getBlogByIds(@Param("blogIds") List<Long> blogIds);

三、service层的代码(我使用了pageHelper进行了分页查询)

 public PageDto<List<BlogVo>> getBlogByIds(PageDto pageDto, BlogQueryDTO blogQueryDTO) {

        List<Long> blogIds = blogsIdTagsIdDao.getBlogIdsByTag(blogQueryDTO.getTagId());
        Page<Blog> blogPage;
        if (blogIds != null && blogIds.size() > 0){
            blogPage = PageHelper.startPage(pageDto.getCurPage(),pageDto.getPageSize(),true)
                    .setOrderBy("updateTime desc")
                    .doSelectPage(() -> blogDao.getBlogByIds(blogIds));
        }else{
            blogPage = PageHelper.startPage(pageDto.getCurPage(),pageDto.getPageSize(),true)
                    .setOrderBy("updateTime desc")
                    .doSelectPage(() -> blogDao.getAllBlog());
        }
        List<BlogVo> blogVoList = blogPage.getResult().stream().map(this::blog2Vo).collect(Collectors.toList());

        return Page2DTOUtil.page2PageDto(blogPage,blogVoList);
    }

你可能感兴趣的:(JAVA,JDBC,数据库)