Canvas中的category获取不到值

Canvas中的category获取不到值

CanvasDao的selectAll方法,Canvas的普通属性都能获取到值,但category是空,什么原因


Category类

public class Category {
   private Integer id;
   private String name;
   private String createName;
   private Date createTime;
   private Date updateTime;
   private String description;

   public Category() {
   }

   public Category(Integer id, String name, String createName, Date createTime, Date updateTime, String description) {
       this.id = id;
       this.name = name;
       this.createName = createName;
       this.createTime = createTime;
       this.updateTime = updateTime;
       this.description = description;
   }

   public Integer getId() {
       return id;
   }

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

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getCreateName() {
       return createName;
   }

   public void setCreateName(String createName) {
       this.createName = createName;
   }

   public Date getCreateTime() {
       return createTime;
   }

   public void setCreateTime(Date createTime) {
       this.createTime = createTime;
   }

   public Date getUpdateTime() {
       return updateTime;
   }

   public void setUpdateTime(Date updateTime) {
       this.updateTime = updateTime;
   }

   public String getDescription() {
       return description;
   }

   public void setDescription(String description) {
       this.description = description;
   }
}


Canvas类

public class Canvas {
private Integer id;
private Integer categoryId;
private String name;
private String creator;
private String imgPath;
private Date createTime;
private Date updateTime;
private String description;
private String details;
private Category category;

   public Canvas() {
   }

   public Canvas(Integer id, Integer categoryId, String name, String creator, String imgPath, Date createTime, Date updateTime, String description, String details, Category category) {
       this.id = id;
       this.categoryId = categoryId;
       this.name = name;
       this.creator = creator;
       this.imgPath = imgPath;
       this.createTime = createTime;
       this.updateTime = updateTime;
       this.description = description;
       this.details = details;
       this.category = category;
   }

   public Integer getId() {
       return id;
   }

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

   public Integer getCategoryId() {
       return categoryId;
   }

   public void setCategoryId(Integer categoryId) {
       this.categoryId = categoryId;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getCreator() {
       return creator;
   }

   public void setCreator(String creator) {
       this.creator = creator;
   }

   public String getImgPath() {
       return imgPath;
   }

   public void setImgPath(String imgPath) {
       this.imgPath = imgPath;
   }

   public Date getCreateTime() {
       return createTime;
   }

   public void setCreateTime(Date createTime) {
       this.createTime = createTime;
   }

   public Date getUpdateTime() {
       return updateTime;
   }

   public void setUpdateTime(Date updateTime) {
       this.updateTime = updateTime;
   }

   public String getDescription() {
       return description;
   }

   public void setDescription(String description) {
       this.description = description;
   }

   public String getDetails() {
       return details;
   }

   public void setDetails(String details) {
       this.details = details;
   }

   public Category getCategory() {
       return category;
   }

   public void setCategory(Category category) {
       this.category = category;
   }

   @Override
   public String toString() {
       return "Canvas{" +
               "id=" + id +
               ", categoryId=" + categoryId +
               ", name='" + name + '\'' +
               ", creator='" + creator + '\'' +
               ", imgPath='" + imgPath + '\'' +
               ", createTime=" + createTime +
               ", updateTime=" + updateTime +
               ", description='" + description + '\'' +
               ", details='" + details + '\'' +
               ", category=" + category +
               '}';
   }
}



CanvasDao类

public interface CanvasDao {

   @Select("select a.*,c.id cId,c.name cName,c.createTime cCreateTime,c.updateTime cUpdateTime,c.description cDescription from canvas a left join category c on a.categoryId=c.id;")
   @Results(value = {
           @Result(column = "id",property = "id",id = true),
           @Result(column = "categoryId",property = "categoryId"),
           @Result(column = "name",property = "name"),
           @Result(column = "creator",property = "creator"),
           @Result(column = "price",property = "price"),
           @Result(column = "imgPath",property = "imgPath"),
           @Result(column = "createTime",property = "createTime"),
           @Result(column = "updateTime",property = "updateTime"),
           @Result(column = "description",property = "description"),
           @Result(column = "details",property = "details"),
           @Result(column = "cId",property = "category.id"),
           @Result(column = "cName",property = "category.name"),
           @Result(column = "cCreateName",property = "category.createName"),
           @Result(column = "cCreateTime",property = "category.createTime"),
           @Result(column = "cUpdateTime",property = "category.updateTime"),
           @Result(column = "cDescription",property = "category.description")
   })
   List<Canvas> selectAll();

}

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
慕布斯645313 2019-03-11 17:14:36

你的sql语句中不是查询的category类吧,你查的是属性吗?你的sql语句写的有问题吧。你试试向我这么写好不好使。

package com.imooc.canvas.dao;

import com.imooc.canvas.entity.Canvas;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CanvasDAO {

    @Insert("insert into canvas(categoryId,name,creator,price,smallImg,createTime,updateTime,description) values" +
            "(#{categoryId},#{name},#{creator},#{price},#{smallImg},#{createTime},#{updateTime},#{description})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    void insert(Canvas canvas);

    @Delete("delete from canvas where id = #{id}")
    void delete(int id);

    @Update("update canvas set name=#{name}, categoryId=#{categoryId}, price=#{price}, smallImg=#{smallImg}, updateTime=#{updateTime}, description=#{description} where id=#{id}")
    void update(Canvas canvas);

    @Select("select * from canvas")
    @Results(id = "all",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "categoryId",property = "categoryId"),
            @Result(column = "name",property = "name"),
            @Result(column = "creator",property = "creator"),
            @Result(column = "price",property = "price"),
            @Result(column = "smallImg",property = "smallImg"),
            @Result(column = "createTime",property = "createTime"),
            @Result(column = "updateTime",property = "updateTime"),
            @Result(column = "description",property = "description"),
            @Result(column = "details",property = "details")
    })
    List<Canvas> selectAll();

    @Select("select * from canvas where id = #{id}")
    @ResultMap("all")
    Canvas selectById(int id);

    //根据油画分类查询油画
    @Select("select * from canvas where categoryId = #{cateId}")
    @ResultMap("all")
    List<Canvas> selectByCateId(int cateId);
}


chrismorgen 2019-03-10 15:21:35

你好同学,建议你在workbench中执行一下当前的sql语句,看一下是否能查询到category的值呢?祝学习愉快~

  • 提问者 花陽親Go #1
    select a.*,c.id cId,c.name cName,c.createTime cCreateTime,c.updateTime cUpdateTime,c.description cDescription from canvas a left join category c on a.categoryId=c.id 在workbench里执行可以查到category的值,但就是无法映射到Canvas类中的category属性中去
    2019-03-10 22:15:20
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
Java数据库开发与实战应用2018版
  • 参与学习           人
  • 提交作业       277    份
  • 解答问题       4297    个

Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师