最近从springside3项目里看到个值对象拷贝框架 dozer 学习了一下,发现还是满有用的,写点学习小结
适用情况:分层架构时对DTO,VO,Pojo之间值属性的拷贝,并且这种拷贝可以跨属性的,即两个对象可以完全不一样(字段名称,字段属性)
比如将基本类型转为包装类型,数组转字符串,各种Collection集合类型之间互相转化,举个例子页面有个产品form有个关键词属性 是个String数组,对应页面一排输入框,而在数据库里是用逗号(,)分隔的字符串,一般都是在程序里用
String.split(","),StringUtils.join(String[]s)这样的方法进行转化,现在将这两个互相转化的方法定义的pojo中
写道
public void setWords(String words) {
this.keyWords = words.split(",");
}
public String getWords(){
return StringUtils.join(keyWords);
}
在相应的dozer-bean-mapping.xml文件中定义get,set方法
写道
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mappings PUBLIC "-//DOZER//DTD MAPPINGS//EN"
"http://dozer.sourceforge.net/dtd/dozerbeanmapping.dtd">
<mappings>
<configuration>
<stop-on-errors>true</stop-on-errors>
<date-format>yyyy-MM-dd</date-format>
<wildcard>true</wildcard>
<trim-strings>true</trim-strings>
</configuration>
<mapping map-null="false">
<class-a>com.spring.bean.Students</class-a>
<class-b>com.spring.view.command.StudentCommand</class-b>
<field>
<a>word</a>
<b set-method="setWords(java.lang.String)" get-method="getWords">keyWords</b>
</field>
</mapping>
</mappings>
测试代码
写道
package com.spring.test;
import static java.lang.System.out;
import java.util.Date;
import net.sf.dozer.util.mapping.DozerBeanMapper;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
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;
import com.spring.bean.Students;
import com.spring.view.command.StudentCommand;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class BaseTest {
@Autowired
private DozerBeanMapper dozer;
@Test
public void fristDozerTetst() {
Students stu = new Students();
stu.setAge(" dff ");
stu.setId("dd");
stu.setDate(new Date());
stu.setWord("1,2,3,4,5,6");
StudentCommand studentCommand = (StudentCommand) dozer.map(stu,
StudentCommand.class);
out.println(ReflectionToStringBuilder.toString(studentCommand));
StudentCommand sc1 = new StudentCommand();
sc1.setAge("444");
sc1.setName("");
sc1.setValue(5555);
sc1.setDate("2008-08-08");
sc1.setKeyWords(new String[] { "a", "b", "c", "d", "e", "f" });
Students stu1 = (Students) dozer.map(sc1, Students.class);
out.println(ReflectionToStringBuilder.toString(stu1));
String s=stu1.getWord();
out.println(stu1.getWord());
}
控制台输出
写道
com.spring.view.command.StudentCommand@f9c40[id=dd,name=<null>,age=dff,value=0,date=2008-06-03,keyWords={1,2,3,4,5,6}]
com.spring.bean.Students@1bb5c09[id=<null>,name=,age=444,value=5555,order=<null>,date=Fri Aug 08 00:00:00 CST 2008,orderByType=<null>,word=abcdef]
abcdef
可见 dozer将原来的字符串"1,2,3,4,5,6"转化为字符数组={1,2,3,4,5,6}
而字符数组{ "a", "b", "c", "d", "e", "f" }则变为word=abcdef;
另外dozer也可格式化时间
<date-format>yyyy-MM-dd</date-format>
date=2008-06-03
sc1.setDate("2008-08-08");
date=Fri Aug 08 00:00:00 CST 2008
若两个需要互相拷贝的java对象有相同的字段名,则在配置文件中无需在指出就可以自带的copy,很方便
在程序里再也不用出现一堆的setXXX(XXX.getXXX)了
最后是spring的context文件
写道
<?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.5.xsd">
<bean id="net.sf.dozer.util.mapping.MapperIF"
class="net.sf.dozer.util.mapping.DozerBeanMapper" scope="singleton">
<property name="mappingFiles">
<list>
<value>dozer-bean-mapping.xml</value>
</list>
</property>
</bean>
</beans>
分享到:
相关推荐
一. 在tomcat的lib目录下添加jar包cors-filter-1.5.0.1.jar,java-property-utils-1.9.jar 二. tomcat路径下conf/web.xml文件中添加以下内容,可以在原文件中找到标签酌情添加。不要加到开头或者结尾。 <filter> ...
Indoor-Mapping-Using-the-VLC-Channel-State-Information-master源码
matlab-mapping toolbox-提取自r2019b,解压后放到matlab目录下的toolbox中,并在预设目录中添加他和子目录,最后在预设-常规中点击更新工具箱路径缓存即可
hibernate-mapping-3.0.dtd hibernate-mapping-3.0.dtd hibernate-mapping-3.0.dtd hibernate-mapping-3.0.dtd hibernate-mapping-3.0.dtd hibernate-mapping-3.0.dtd hibernate-mapping-3.0.dtd hibernate-mapping-...
Mapping co-authorships with BibExcel
hibernate-mapping-3.0.dtd 配置后,就会在xml中进行提示
绘图工具箱(Mapping Toolbox)提供的工具和实用程序,用于分析地理数据和创建地图显示。您可以从Shapefile文件,GeoTIFF格式,SDTS DEM和其他格式的文件导入矢量和栅格数据,以及从网络地图服务(WMS)服务器导入...
NULL 博文链接:https://erbin.iteye.com/blog/367375
Mapping Of Salt-Affected Soils Using Remote Sensing And Geograp Information Systems A Case Study Of Nakhon Ratchaslma Thailand
Unicode-Mapping-on-Domain-names-源码.rar
因为Hibernate在读出hbm.xml文件时需要通过网络读取到hibernate-mapping-3.0.dtd 文件。 如果没有网络不能正常工作。 所以提供上述文件。 以及hibernate-mapping-3.0.dtd,hibernate-configuration-3.0.dtd提供下载...
Building-Mapping-Applications-with-QGIS-Create-your-own-sophisticated-applications-to-analyze-and-display-geospatial-information-using-QGIS-and-Python.pdf
maven项目插件的配置,大家如果在进行clean,package或者运行时出错,有可能是maven插件的问题,可以试试这个配置
资源来自pypi官网。 资源全名:jsonmapping-0.3.1-py2-none-any.whl
python库。 资源全名:seed_intersphinx_mapping-0.4.1-py3-none-any.whl
hibernate-configuration-3.0.dtd、hibernate-mapping-3.0.dtd、hibernate.properties(5.0.7)