5.0 本地测试DEMO加深理解
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example.DemoCrud</groupId> <artifactId>democurd</artifactId> <version>0.0.1-SNAPSHOT</version> <name>democurd</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- mybatis整合Springboot --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <!-- maven 添加json--> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> </dependency> <!--jquery-webjar--> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.0</version> </dependency> <!--bootstrap-webjar--> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>4.0.0</version> </dependency> <!--配置文件注入时使用后会有提示--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.3.2</version> </dependency> <!--mapStruct依赖--> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-jdk8</artifactId> <version>1.2.0.CR1</version> </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>1.2.0.CR1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>1.3.1.Final</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectors</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> <!-- 添加资源 --> <resources> <resource> <directory>src/main/resources</directory> <!-- src/main/resources下的指定资源放行 --> <includes> <include>**/*.properties</include> <include>**/*.yml</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
代码结构:
先创建2个实体类:
package com.example.democrud.democurd.DoVO; import lombok.Data; /** * 来源对象 */ @Data public class DoUser { private String id; private String user; private String age; }
package com.example.democrud.democurd.DoVO; import lombok.Data; /** * 目标对象 */ @Data public class VoUser { private String id; private String user; private String age; }
为了加深理解我们先手写一个映射关系;
@Mapper public interface CarMap { CarMap INSTANCE = Mappers.getMapper( CarMap.class ); DoUser toVoUser(VoUser demo); }
实现类
package com.example.democrud.democurd.DoVO; public class CarMapImpl implements CarMap{ @Override public DoUser toVoUser(VoUser demo) { if ( demo == null ) { return null; } DoUser demoDto = new DoUser(); demoDto.setId( demo.getId() ); demoDto.setUser( demo.getUser() ); demoDto.setAge(demo.getAge()); return demoDto; } }
控制台:
public static void main(String[] args) { VoUser user = new VoUser(); user.setUser("闫文超"); user.setAge("12"); user.setId("66"); DoUser doUser=CarMap.INSTANCE.toVoUser(user); System.out.println("doUser==>"+doUser); } }
这样2个实体类的关系就会达成映射关系
还有一个简单的方法;(componentModel = “spring”) 加入注解就可以自动实现他的方法了(和上面 的区别只添加这个即可)
package com.example.democrud.democurd.DoVO; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; @Mapper(componentModel = "spring") public interface CarMap { CarMap INSTANCE = Mappers.getMapper( CarMap.class ); // DoUser toVoUser(VoUser demo); DoUser toVoUsers(VoUser demo); }
测试结果:
是不是很容易啊,这样既能分别定义的VO、PO、DTO,又能快速完成开发工作了;
DEMO2:实用版本
实体类和上面相识;
重点 BaseVo 工具类:
package com.example.democrud.democurd.DoVO; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.converters.BigDecimalConverter; import org.apache.commons.beanutils.converters.DoubleConverter; import org.apache.commons.beanutils.converters.FloatConverter; import org.apache.commons.beanutils.converters.IntegerConverter; import org.apache.commons.beanutils.converters.LongConverter; import org.apache.commons.beanutils.converters.ShortConverter; /** * * VO基类 * @author clwang * @version 2019年4月17日 */ public class BaseVO implements Serializable { /** * 意义,目的和功能,以及被用到的地方<br> */ private static final long serialVersionUID = 7474374467497907429L; /** * * PO转VO * @param po po对象 * @param voClass VO的class对象 * @param <T> 具体VO对象的泛型 * @return 返回VO */ public static <T extends BaseVO> T toVO(Object po, Class<T> voClass) { if (null == po) { return null; } setDefaultValue(); try { T vo = voClass.newInstance(); BeanUtils.copyProperties(vo, po); return vo; } catch (InstantiationException e) { System.out.println(e); } catch (IllegalAccessException e) { System.out.println(e); } catch (InvocationTargetException e) { System.out.println(e); } return null; } /** * BeanUtils空值拷贝默认属性设置,空值任然为空,不赋值默认值 */ private static void setDefaultValue() { ConvertUtils.register(new LongConverter(null), Long.class); ConvertUtils.register(new IntegerConverter(null), Integer.class); ConvertUtils.register(new ShortConverter(null), Short.class); ConvertUtils.register(new FloatConverter(null), Float.class); ConvertUtils.register(new DoubleConverter(null), Double.class); ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class); } /** * * VO转PO * @param poClass po class对象 * @param <T> 具体po对象的泛型 * @return PO对象 */ public <T> T toPO(Class<T> poClass) { setDefaultValue(); try { T po = poClass.newInstance(); BeanUtils.copyProperties(po, this); return po; } catch (InstantiationException e) { System.out.println(e); } catch (IllegalAccessException e) { System.out.println(e); } catch (InvocationTargetException e) { System.out.println(e); } return null; } }
我们看下如何调用:
public static void main(String[] args) { VoUser user = new VoUser(); user.setUser("闫文超"); user.setAge("12"); user.setId("66"); VoUser user1 = new VoUser(); user1.setUser("闫22"); user1.setAge("33"); user1.setId("77"); test1(user); // test2(user1); } public static void test1(VoUser vouser) { //用实体类继承该f方法后调用 //前面是空数据需要导入数据的类 vouser是有数据的 DoUser douser = vouser.toPO(DoUser.class); System.out.println("test1==>"+douser); } public static void test2(VoUser vouser) { DoUser doUser=DoUser.toVO(vouser,DoUser.class); System.out.println("test2==>"+doUser); }
补充:
public static void test3(VoUser vouser) { DoUser doUser=new DoUser(); //前面的为值 后面是需要赋值的实体类 BeanUtil.copyProperties(vouser,doUser); System.out.println("test3==>"+doUser); }
想用那种大家随意喽;