# mapstruct **Repository Path**: lxjblog/mapstruct ## Basic Information - **Project Name**: mapstruct - **Description**: mapstruct 类型转换 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-02-01 - **Last Updated**: 2024-07-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## MapStruct > 用于将 PO、VO、DTO 各种实体类之间的相互转换 1、引入依赖 ```xml org.mapstruct mapstruct ${mapstruct.version} org.projectlombok lombok ${lombok.version} ``` 2、创建接口基本接口 ```java public interface DtoPoMapStruct { PO toPo(DTO var1); DTO toDto(PO var1); List toPo(List var1); List toDto(List var1); } ``` 3、创建接口并添加注解,model 选择 spring 的方式,这个会生成一个 spring 的 bean,方便后续使用 ```java @Mapper(componentModel = "spring") public interface UserDtoPoMapStruct extends DtoPoMapStruct { } ``` 4、配置 pom.xml 打包 ```xml org.apache.maven.plugins maven-compiler-plugin 3.11.0 1.8 1.8 org.projectlombok lombok ${lombok.version} org.mapstruct mapstruct-processor ${mapstruct.version} ``` 5、启动项目即可自动生成对应的 mapStruct 实现类了 ![img.png](img.png) 6、使用的时候将这个实现类依赖注入就可以了 ## mybatis自动创建表 > 通过在实体类上添加注解,自动生成和修改数据库中对应的表 1、引入依赖 ```xml com.baomidou mybatis-plus-boot-starter 3.5.4 com.gitee.sunchenbin.mybatis.actable mybatis-enhance-actable 1.5.0.RELEASE com.baomidou mybatis-plus-annotation mysql mysql-connector-java ``` 2、修改 application.yml 配置 ```yaml # 配置数据库连接信息 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/admin_sys?characterEncoding=UTF8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true username: root password: 123456 # 配置 mapper.xml 的扫描路径,这里需要直接指定以下内容 mybatis-plus: mapper-locations: classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml # 建表基本配置 actable: table: auto: update model: pack: com.example.mapstruct_test.entity database: type: mysql ``` 3、修改启动类,添加扫描路径 ```java @SpringBootApplication(scanBasePackages = {"com.gitee.sunchenbin.mybatis.actable.manager.*"}) @MapperScan(basePackages = {"com.gitee.sunchenbin.mybatis.actable.dao.*"}) public class MapstructTestApplication { public static void main(String[] args) { SpringApplication.run(MapstructTestApplication.class, args); } } ``` 4、在实体类上添加注解 ```java @Data @TableName("test_user") @Table(comment = "测试表") public class User { @TableId @Column(comment = "id", isNull = false) private Integer id; @Column(comment = "userName", isNull = false) private String userName; @Column(comment = "age", isNull = false) private Integer age; @Column(comment = "address", isNull = false) private String address; @Column(comment = "delFlg", isNull = false) private Integer delFlg; } ``` 5、启动项目即可看到对应的输出日志和表 ![img_1.png](img_1.png) ![img_2.png](img_2.png)