Dozer的使用: 整合Spring
https://blog.csdn.net/github_36581033/article/details/79187116
Dozer與Spring的使用
Dozer是物件轉換工具。比如說,當你在Controller層有一個物件ClassA,但是你要傳一個ClassAVO物件給Service層,這時可以使用Dozer將物件ClassA轉換成所需要的VO類。當然你也可以在其它層做這樣的操作,下面使用一個例子展現Dozer到底是如何工作的:
1
1.使用Maven管理依賴包
pom.xml檔案如下
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.symbio</groupId>
<artifactId>dozer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.version>5.0.3.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2.新建四個類ClassA、ClassB、SubClassA、SubClassB
import java.util.Date;
import java.util.List;
public class ClassA {
private Integer id;
private String name;
private Date date;
private List<SubClassA> subA;
一律省略getter and setter...
}
import java.util.Date;
import java.util.List;
public class ClassB {
private Integer id;
private String username;
private Date birth;
private List<SubClassB> subB;
...
}
public class SubClassA {
private String color;
private Integer price;
...
}
public class SubClassB {
private String myColor;
private String price;
...
}
3.使用xml方式對映,在src/main/resource下新建dozer-mapping.xml檔案
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net
http://dozer.sourceforge.net/schema/beanmapping.xsd">
<mapping date-format="yyyy-MM-dd">
<class-a>com.symbio.dozertest.ClassA</class-a>
<class-b>com.symbio.dozertest.ClassB</class-b>
<field>
<a>name</a>
<b>username</b>
</field>
<field>
<a>date</a>
<b>birth</b>
</field>
<field>
<a>subA</a>
<b>subB</b>
<!-- 若兩個物件中還有需要mapping的物件,使用hint標籤,
再新建一個mapping標籤,一直到內部沒有包含可以mapping
的物件為止 -->
<a-hint>com.symbio.dozertest.SubClassA</a-hint>
<b-hint>com.symbio.dozertest.SubClassB</b-hint>
</field>
</mapping>
<mapping>
<class-a>com.symbio.dozertest.SubClassA</class-a>
<class-b>com.symbio.dozertest.SubClassB</class-b>
<field>
<a>color</a>
<b>myColor</b>
</field>
<!-- 如果兩個fields的名字一樣但屬性不同,也可以忽略
但比較簡單的型別可以這樣做,更復雜的需要自定義Converter
來完成物件的轉換 -->
<!-- <field>
<a>price</a>
<b>price</b>
</field> -->
</mapping>
</mappings>
4.Spring和Dozer整合,在這裡新建spring-dozer.xml檔案,當然其它名字也可以de
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
default-autowire="byName" default-lazy-init="false">
<bean id="mapper" class="org.dozer.spring.DozerBeanMapperFactoryBean">
<property name="mappingFiles">
<list>
<!-- 把Dozer對映檔案引入 -->
<value>classpath*:dozer-mapping.xml</value>
</list>
</property>
</bean>
</beans>
5.測試類DozerTest.java
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.dozer.Mapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
<!-- 一定要指定Spring配置檔案的路徑 -->
@ContextConfiguration(locations = {"classpath:spring-dozer.xml"})
public class DozerTest {
@Autowired
Mapper mapper;
@Test
public void dozerMap() {
System.out.println("mapper:"+mapper);
ClassA a = new ClassA();
a.setId(123);
a.setName("Curry");
a.setDate(new Date());
List<SubClassA> subAList = new ArrayList<SubClassA>();
SubClassA subA = new SubClassA();
subA.setColor("red");
subA.setPrice(120);
subAList.add(subA);
a.setSubA(subAList);
ClassB b = mapper.map(a, ClassB.class);
System.out.println("ClassB:"+b);
}
}
測試結果
相關文章
- spring+redis的整合,使用spring-data-redis來整合SpringRedis
- Spring Cache + Caffeine的整合與使用Spring
- ActiveMQ的使用及整合spring的使用例項MQSpring
- Mybatis 的使用(整合Spring、SpringBoot)MyBatisSpring Boot
- Spring Boot(十八):使用 Spring Boot 整合 FastDFSSpring BootAST
- 使用Spring Boot整合ConsulSpring Boot
- Dozer掃盲級教程
- 使用Spring Boot整合Apollo配置中心Spring Boot
- dozer- JavaBean對映工具JavaBean
- spring boot使用註解的方式整合mybaitsSpring BootAI
- Spring的Junit整合Spring
- spring:spring與mybatis的整合SpringMyBatis
- Dozer封裝物件或List的轉換封裝物件
- Spring Boot整合swagger使用教程Spring BootSwagger
- 使用 CXF 整合 Spring 開發 Web ServiceSpringWeb
- Spring3+MyBatis 整合多方法使用SpringMyBatis
- Spring Test 整合 JUnit 4 使用總結Spring
- Spring 對Mabatis的整合SpringBAT
- CAS與Spring的整合Spring
- Spring和JUnit的整合Spring
- struts與spring 的整合Spring
- springboot(十八):使用Spring Boot整合FastDFSSpring BootAST
- Spring整合Quartz案例使用RAM儲存方式Springquartz
- Spring整合Quartz案例使用JDBC儲存方式SpringquartzJDBC
- 怎樣使用Spring Boot 整合配置 HTTPSSpring BootHTTP
- 使用CXF與Spring整合實現RESTFul WebServiceSpringRESTWeb
- Ehcache 整合Spring 使用頁面、物件快取Spring物件快取
- Spring Boot整合Spring BatchSpring BootBAT
- Spring Boot整合Spring SecuritySpring Boot
- Spring Boot整合Spring AopSpring Boot
- Spring 整合 MyBatisSpringMyBatis
- mybatis整合springMyBatisSpring
- ActiveMq整合SpringMQSpring
- Spring整合MyBatisSpringMyBatis
- Spring整合JUnitSpring
- Spring整合WebSocketSpringWeb
- Spring Cloud 整合SpringCloud
- Spring MVC整合redis的配置SpringMVCRedis