【SpringBoot + MapStruct】SpringBoot简单整合MapStruct,解决生成对象没有赋值的问题

RT

这里真的是简单整合,毕竟网站里面已经好多人写了整合案例了,只不过,我在这些教程里面发现了一些痛点,让我吐血三升,含泪写一份简单的整合示例

感谢

MapStruct实现对象映射
感谢这个大哥写的这篇文章,让我找到了问题所在

痛点

站内某些博主的案例,有一点很重要的没有贴出来,那就是:MapStruct的转换是在编译阶段完成的,所以,需要在打包项目的pom.xmlmaven-compiler-plugin插件中添加如下配置:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                    <annotationProcessorPaths>
                        <!-- 此处,lombok依赖一定要放在,Mapstruct-processor依赖之前。否则,生成了maptruct的实现类,但该类只创建了对象,没有进行赋值 -->
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok-mapstruct-binding</artifactId>
                            <!-- 如果是0.1.0 有可能出现生成了MapStruct的实现类,但该类只创建了对象,没有进行赋值 -->
                            <version>0.2.0</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>

如果你没有上述配置,你生成出来的转换代码实现,只是创建了对象,但是没有具体实现,即赋值过程

整合过程

在pom.xml中添加mapstruct的依赖:(这边版本用的 1.4.1.final)

<dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <version>1.18.20</version>
 </dependency>
 <dependency>
     <groupId>org.mapstruct</groupId>
     <artifactId>mapstruct</artifactId>
     <version>${mapstruct.version}</version>
 </dependency>
 <dependency>
     <groupId>org.mapstruct</groupId>
     <artifactId>mapstruct-processor</artifactId>
     <version>${mapstruct.version}</version>
 </dependency>

接着,生成对应的Mapper对象就好,如下所示:

@Mapper
public interface PojoConverter {
    PojoConverter INSTANCE = Mappers.getMapper(PojoConverter.class);

    /**
     * SourcePojo转换成TargetPojo对象
     */
    TargetPojo orderInfoToConsumeOrder(SourcePojo sourcePojo);
}

OK,到了这里基本完事了
然后maven install一下,就会在target目录下,生成一个generated-sources目录,就能看到它的转换实现代码了
在这里插入图片描述

建议

建议能使用mapstruct就使用mapstruct,因为性能提升确实挺大的。在没使用这个东西之前,我能get/set的就get/set,基本不使用BeanUtils,这个东西太坑了,拉性能

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Spring Boot中使用Vue和Element UI来显示后端图片,可以按照以下步骤进行操作: 1. 在Spring Boot项目中,创建一个用于存储图片的文件夹,比如将其命名为"images"。 2. 将需要显示的图片上传到该文件夹中。 3. 在后端编写一个Controller,用于处理图片请求。可以添加一个接口,比如"/api/image/{imageName}",用于获取某个特定图片的路径。 ```java @RestController @RequestMapping("/api/image") public class ImageController { @Value("${image.path}") private String imagePath; @GetMapping("/{imageName}") public void getImage(@PathVariable String imageName, HttpServletResponse response) throws IOException { File file = new File(imagePath + "/" + imageName); FileInputStream fis = new FileInputStream(file); IOUtils.copy(fis, response.getOutputStream()); } } ``` 配置文件中的image.path属性需要指向存放图片的文件夹路径。 4. 在Vue中,使用Element UI的表格组件来显示图片。首先在Vue组件中定义一个data属性,用于保存图片数据。 ```javascript data() { return { imageUrl: '/api/image/' tableData: [] }; } ``` "imageUrl"属性用于指定获取图片的接口路径。 5. 从后端获取图片数据,在Vue组件的"mounted"生命周期中发起一个请求,获取后端返回的图片数据,并将其赋值给表格的数据属性。 ```javascript mounted() { axios.get('/api/imageData') .then(response => { let data = response.data; // 对返回的data进行处理,将图片路径拼接上指定的路径 data.forEach(item => { item.imageUrl = this.imageUrl + item.imageName; }); this.tableData = data; }) .catch(error => { console.error('Error:', error); }); } ``` 6. 在表格的列定义中,使用Element UI的"el-table-column"组件来显示图片。 ```html <el-table-column prop="imageUrl" label="图片"> <template slot-scope="scope"> <img :src="scope.row.imageUrl" width="100" height="100"> </template> </el-table-column> ``` 通过以上的步骤,就可以在Spring Boot中使用Vue和Element UI来显示后端图片了。在表格中的图片列中,会显示相应的图片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

验证码有毒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值