@Data @NoArgsConstructor @AllArgsConstructor @Entity @Table(name = "ad") public class Ad { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; @ManyToOne @JoinColumn(name = "position_id") private AdPosition position; @Column(name = "start_date") private LocalDateTime startDate; @Column(name = "end_date") private LocalDateTime endDate; private String link; } 和 @Data @NoArgsConstructor @AllArgsConstructor @Entity @Table(name = "ad_image") public class AdImage { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String url; @OneToOne(mappedBy = "image") private Ad ad; } ,但是方法 @PostMapping public Ad add(@RequestBody AdDto adDto) { AdPosition position = adPositionRepository.getOne(adDto.getPositionId()); AdImage image = new AdImage(); image.setUrl(adDto.getImageUrl()); adImageRepository.save(image); Ad ad = new Ad(); ad.setName(adDto.getName()); ad.setPosition(position); ad.setStartDate(adDto.getStartDate()); ad.setEndDate(adDto.getEndDate()); ad.setLink(adDto.getLink()); ad.setImage(image); return adRepository.save(ad); } 中的ad.setImage(image);报错Cannot resolve method 'setimage' in 'Advert',请说明原因和解决方案
时间: 2023-11-22 12:54:55 浏览: 186
出现该错误的原因是Ad类中没有定义名为setImage的方法,因为在Ad类中定义的是一个名为setImage的方法,而不是setimage。因此,应该将ad.setImage(image)更改为ad.setImage(image),即将image的首字母改为大写。
修改后的代码如下:
```
@PostMapping
public Ad add(@RequestBody AdDto adDto) {
AdPosition position = adPositionRepository.getOne(adDto.getPositionId());
AdImage image = new AdImage();
image.setUrl(adDto.getImageUrl());
adImageRepository.save(image);
Ad ad = new Ad();
ad.setName(adDto.getName());
ad.setPosition(position);
ad.setStartDate(adDto.getStartDate());
ad.setEndDate(adDto.getEndDate());
ad.setLink(adDto.getLink());
ad.setImage(image);
return adRepository.save(ad);
}
```
相关问题
@Data @NoArgsConstructor @AllArgsConstructor @Entity @Table(name = "ad") public class Ad { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; @ManyToOne @JoinColumn(name = "position_id") private AdPosition position; @Column(name = "start_date") private LocalDateTime startDate; @Column(name = "end_date") private LocalDateTime endDate; private String link; }和@Data @NoArgsConstructor @AllArgsConstructor @Entity @Table(name = "ad_image") public class AdImage { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String url; @OneToOne(mappedBy = "image") private Ad ad; },为什么 public Ad add(@RequestBody AdDto adDto) { AdPosition position = adPositionRepository.getOne(adDto.getPositionId()); AdImage image = new AdImage(); image.setUrl(adDto.getImageUrl()); adImageRepository.save(image); Ad ad = new Ad(); ad.setName(adDto.getName()); ad.setPosition(position); ad.setStartDate(adDto.getStartDate()); ad.setEndDate(adDto.getEndDate()); ad.setLink(adDto.getLink()); ad.setImage(image); return adRepository.save(ad); }报错Cannot resolve method 'setImage' in 'Advert'
这个错误是因为您的Ad类中没有定义setImage()方法。在Ad类中,您定义了一个AdImage对象,并使用@OneToOne注释将其映射到AdImage表中的Ad列。要设置AdImage对象,您应该使用set方法而不是setImage()方法。因此,您应该在Ad类中为AdImage对象定义一个set方法,然后使用set方法设置AdImage对象,就像这样:
```
public void setImage(AdImage image) {
this.image = image;
}
```
然后在您的add()方法中,将以下行:
```
ad.setImage(image);
```
更改为:
```
ad.setimage(image);
```
这样您的代码就不会再出现这个错误了。
@Data @AllArgsConstructor @NoArgsConstructor @Builder @Table(name = "advert") public class Advert { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @ApiModelProperty(value = "广告唯一id") private Integer id; @ApiModelProperty(value = "广告名称") @NotBlank(message = "广告名称不可为空", groups = {AddGroup.class, UpdateGroup.class}) private String name; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "position_id") private AdPosition position; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "media_id") private MediaFile media; } @Data @AllArgsConstructor @NoArgsConstructor @Builder @Table(name = "ad_position") public class AdPosition { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @ApiModelProperty(value = "位置名称") @NotBlank(message = "位置名称不可为空", groups = {AddGroup.class}) private String name; @OneToMany(mappedBy = "AdPosition", cascade = CascadeType.ALL) private List<Advert> adverts = new ArrayList<>(); } @Data @AllArgsConstructor @NoArgsConstructor @Builder @Table(name = "media_file") public class MediaFile { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @ApiModelProperty(value = "媒体文件名称") private String fileName; @ApiModelProperty(value = "媒体文件类型") private Integer fileType; @ApiModelProperty(value = "媒体文件URL地址") private String fileUrl; @OneToMany(mappedBy = "MediaFile", cascade = CascadeType.ALL) private List<Advert> adverts = new ArrayList<>(); } public interface MyBaseMapper<T> extends Mapper<T>, MySqlMapper<T>,MyInsertListMapper<T> {} public interface AdvertMapper extends MyBaseMapper<Advert> {},如何在查询advert表时,将关联的其余两张表的数据一并查询出来?请将mapper.xml文件全部展示出来
AdvertMapper.xml文件如下:
```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.AdvertMapper">
<resultMap id="advertMap" type="com.example.entity.Advert">
<id property="id" column="id" />
<result property="name" column="name" />
<association property="position" column="position_id" javaType="com.example.entity.AdPosition">
<id property="id" column="id" />
<result property="name" column="name" />
</association>
<association property="media" column="media_id" javaType="com.example.entity.MediaFile">
<id property="id" column="id" />
<result property="fileName" column="file_name" />
<result property="fileType" column="file_type" />
<result property="fileUrl" column="file_url" />
</association>
</resultMap>
<select id="selectAdvert" resultMap="advertMap">
SELECT a.id, a.name, a.position_id, a.media_id, p.name as position_name, m.file_name, m.file_type, m.file_url
FROM advert a
LEFT JOIN ad_position p ON a.position_id = p.id
LEFT JOIN media_file m ON a.media_id = m.id
WHERE a.id = #{id}
</select>
</mapper>
```
在上面的XML文件中,我们定义了一个名为advertMap的结果映射,它将advert表、ad_position表和media_file表的数据全部查询出来。我们还定义了一个名为selectAdvert的查询语句,它使用advertMap作为结果映射,并在SELECT语句中使用LEFT JOIN将三个表连接起来。最后,我们将这个查询语句的ID设置为selectAdvert,以便在Java代码中调用它。
阅读全文