发表于: 2019-11-07 23:08:07

1 780


今天完成的事情:

今天主要解决多表查询问题,更准确的是一对一的查询

这是完成的Controller 但是用不了插件排序…………

@Autowired
StudyCollectionServer studyCollectionServer;

/**
* 收藏列表查询
* 查找当前用户收藏等于对应类型的所有学习对象
* @param type 学习对象类型
*/
@GetMapping("/a/u/list/collection")
public Map<String, Object> collectionListSelect(Long id,Integer type,
                                               @RequestParam(value = "page", defaultValue = "1") Integer page,
                                               @RequestParam(value = "size", defaultValue = "10") Integer size) {
   log.info("获取第{}页共{}行{}类型的收藏学习对象信息", page, size, type);
   HashMap<String, Object> result = new HashMap<>(16);

   StudyCollection studyCollection = new StudyCollection();
   studyCollection.setCreateBy(id);

   StudyObject studyObject = new StudyObject();
   studyObject.setType(type);
   studyCollection.setStudyObject(studyObject);
   PageHelper.startPage(page,size);
   List studyCollectionList=studyCollectionServer.selectUserStudyCollection(studyCollection);
   PageInfo pageInfo=new PageInfo<>(studyCollectionList);
   Long total = pageInfo.getTotal();
   result.put("code", SUCCESS.getCode());
   result.put("msg", SUCCESS.getMsg());
   result.put("total", total);
   result.put("data", studyCollectionList);
   return result;
}


要弄mybatis的一对一查询,要三步

从表实体类要导入主表的类。生成get和set

private StudyObject studyObject;

public StudyObject getStudyObject() {
   return studyObject;
}

public void setStudyObject(StudyObject studyObject) {
   this.studyObject = studyObject;
}

在从表的mapper.xml里用association添加主表的返回类型。

<resultMap id="BaseResultMap" type="com.happynewyear.user.pojo.StudyCollection">
 <id column="id" jdbcType="BIGINT" property="id" />
 <result column="study_object_id" jdbcType="BIGINT" property="studyObjectId" />
 <result column="create_at" jdbcType="BIGINT" property="createAt" />
 <result column="update_at" jdbcType="BIGINT" property="updateAt" />
 <result column="create_by" jdbcType="BIGINT" property="createBy" />
 <result column="update_by" jdbcType="BIGINT" property="updateBy" />
 <association property="studyObject" column="id" javaType="com.happynewyear.user.pojo.StudyObject">
   <result property="id" column="id"/>
   <result property="title" column="title"/>
   <result property="type" column="type"/>
   <result property="cover" column="cover"/>
   <result property="price" column="price"/>
   <result property="discount" column="discount"/>
   <result property="content" column="content"/>
 </association>
</resultMap>

使用的SQL语句。

<select id="selectUserStudyCollection" resultMap="BaseResultMap">
 SELECT c.study_object_id ,o.title,c.id
FROM
study_collection as c,study_object as o
WHERE
c.study_object_id=o.id AND c.create_by=#{createBy}
</select>

只能做到这步了。


明天计划的事情:

把收藏接口写完。把排序加上弄的出来的话


遇到的问题:

虽然做出来了,但是还不是太理解


收获:

mybatis一对一查询


返回列表 返回列表
评论

    分享到