使用dozer对两个实体进行映射

1、spring注入dozer
applicationContext.xml
<!--dozer-->
    <import resource="dozer.xml"/>
2.dozer.xml
<?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>
                <value>classpath*:dozer/dozer-mapping.xml</value>
            </list>
        </property>
    </bean>
</beans>
3、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>cn.swifthealth.api_write.model.ComPatientInfoVo</class-a>
        <class-b>cn.swifthealth.api_write.model.ComPatientInfo</class-b>
        <field>
            <a>eDate</a>
            <b>end_date</b>
        </field>
    </mapping>
    <mapping>
        <class-a>cn.swifthealth.api.common.patient.PatVisitRecord</class-a>
        <class-b>cn.swifthealth.api_write.model.ComPatientInfo</class-b>
        <field>
            <a>id</a>
            <b>card_No</b>
        </field>
        <field>
            <a>patUserId</a>
            <b>prof_Code</b>
        </field>
        <--对于直接能映射的,使用xml,不能的话,使用api,注入到mapping-->
        <field custom-converter="cn.swifthealth.api_write.model.ComPatientInfoConverter">
            <a>clinicType</a>
            <b>work_Home</b>
        </field>
    </mapping>
</mappings>
4.对于直接能映射的,使用xml,不能的话,使用api,注入到mapping
public class ComPatientInfoConverter  implements CustomConverter{
    public String convert(Object destination, Object source,
                          Class destClass, Class sourceClass) {
        if (source == null) {
            return null;
        }
        String dest = null;
        if (source instanceof OType) {
            // check to see if the object already exists
            if ("已退款".equals(((OType) source).getName())) {
                dest = "0";
            } else {
                dest = "1";
            }
        }
        return dest;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值