背景
BeanMapping是什么? 请查看我之前的一篇文章: 我也造了个轮子:BeanMapping(属性拷贝)
现状
目前代码经过在一个实际项目中进行试用,该项目为公司的核心业务逻辑(产品页面数据),目前表现良好。
在原先的1.0.0版本基础上修复了几个bug,目前推荐使用1.0.1。
下载
url : https://github.com/agapple/mapping4java/tags
mvn仓库:
1. <dependency>
2. <groupId>com.googlecode.mapping4java</groupId>
3. <artifactId>mapping</artifactId>
4. <version>1.0.1</version>
5.</dependency>
Example1:
Step 1 (define mapping config)
1.<bean-mappings xmlns="http://mapping4java.googlecode.com/schema/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2. xsi:schemaLocation="http://mapping4java.googlecode.com/schema/mapping http://mapping4java.googlecode.com/svn/trunk/src/main/resources/META-INF/mapping.xsd">
3. <!-- (bean-bean) mapping 测试 -->
4. <bean-mapping batch="true" srcClass="com.agapple.mapping.object.SrcMappingObject" targetClass="com.agapple.mapping.object.TargetMappingObject" reversable="true">
5. <field-mapping srcName="intValue" targetName="intValue" />
6. <field-mapping targetName="integerValue" script="src.intValue + src.integerValue" /> <!-- 测试script -->
7. <field-mapping srcName="start" targetName="start" />
8. <field-mapping srcName="name" targetName="targetName" /> <!-- 注意不同名 -->
9. <field-mapping srcName="mapping" targetName="mapping" mapping="true" />
10. </bean-mapping>
11.
12. <bean-mapping batch="true" srcClass="com.agapple.mapping.object.NestedSrcMappingObject" targetClass="com.agapple.mapping.object.NestedTargetMappingObject" reversable="true">
13. <field-mapping srcName="name" targetName="name" defaultValue="ljh" /> <!-- 测试default value -->
14. <field-mapping srcName="bigDecimalValue" targetName="value" targetClass="string" defaultValue="10" /> <!-- 测试不同名+不同类型+default value -->
15. </bean-mapping>
16.
17. </bean-mappings>
Step 2 (do mapping)
1.public BeanMapping srcMapping = BeanMapping.create(SrcMappingObject.class, TargetMappingObject.class);
2.public BeanMapping targetMapping = BeanMapping.create(TargetMappingObject.class , SrcMappingObject.class);
3.
4. @Test
5. public void testBeanToBean_ok() {
6. SrcMappingObject srcRef = new SrcMappingObject();
7. srcRef.setIntegerValue(1);
8. srcRef.setIntValue(1);
9. srcRef.setName("ljh");
10. srcRef.setStart(true);
11.
12. TargetMappingObject targetRef = new TargetMappingObject();// 测试一下mapping到一个Object对象
13. srcMapping.mapping(srcRef, targetRef);
14.
15. SrcMappingObject newSrcRef = new SrcMappingObject();// 反过来再mapping一次
16. targetMapping.mapping(targetRef, newSrcRef);
17. }
Example2: (和BeanUtils/BeanCopier一样的使用,根据同名属性进行自动映射,不需要定义任何的mapping.xml)
1.public BeanCopy srcCopy = BeanCopy.create(SrcMappingObject.class, TargetMappingObject.class);
2. public BeanCopy targetCopy = BeanCopy.create(TargetMappingObject.class , SrcMappingObject.class);
3.
4. @Test
5. public void testBeanToBean_ok() {
6. SrcMappingObject srcRef = new SrcMappingObject();
7. srcRef.setIntegerValue(1);
8. srcRef.setIntValue(1);
9. srcRef.setName("ljh");
10. srcRef.setStart(true);
11.
12. TargetMappingObject targetRef = new TargetMappingObject();// 测试一下mapping到一个Object对象
13. srcCopy.copy(srcRef, targetRef);
14.
15. SrcMappingObject newSrcRef = new SrcMappingObject();// 反过来再mapping一次
16. targetCopy.copy(targetRef, newSrcRef);
17. }
Example3: (和BeanUtils一样,处理map<->bean)
1.<span style="white-space: normal;"> <span style="white-space: pre;">public BeanMap beanMap = BeanMap.create(SrcMappingObject.class);
2.</span></span>
3. @Test
4. public void testDescribe_Populate_ok() {
5. SrcMappingObject srcRef = new SrcMappingObject();
6. srcRef.setIntegerValue(1);
7. srcRef.setIntValue(1);
8. srcRef.setName("ljh");
9. srcRef.setStart(true);
10.
11. Map map = beanMap.describe(srcRef);
12.
13. SrcMappingObject newSrcRef = new SrcMappingObject();// 反过来再mapping一次
14. beanMap.populate(newSrcRef, map);
15. }
其他
ChangeLog
1.Release 1.0.1
2. "this" symbol GetExecutor bugfix
3. boolean isSuccessed generated set/get methods for the isSucessed()/setSuccessed(), should filter attribute is the prefix
4. Add date and sqlDate convertor
5. Add String and Enum convertor
6. Add mapping.propertis define ValueProcess plugins
7.
8.Release 1.0.0
9. Initial mapping code
时间: 2024-09-16 23:06:50