jibx 1.2.6 maven项目 ant编译的使用方法

学习netty的过程中无意中接触到jibx这个类库,但是网上一直没有基于maven项目和ant的完整的例子。

所以写下来给大家参考参考。


1 首先新建一个普通的maven项目 引入依赖

<dependency>
	<groupId>org.jibx</groupId>
	<artifactId>jibx-bind</artifactId>
	<version>1.2.6</version>
</dependency>
<dependency>
	groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.11</version>
	<scope>test</scope>
</dependency>

2 编写2个测试bean

public class Account {
    private int id;
    private String name;
    private String email;
    private String address;
    private Birthday birthday;
    //getter、setter
    
    @Override
    public String toString() {
        return this.id + "#" + this.name + "#" + this.email + "#" + this.address + "#" + this.birthday;
    }

   

public class Birthday {
    private String birthday;
    
    public Birthday(String birthday) {
        super();
        this.birthday = birthday;
    }
    //getter、setter
    public Birthday() {}
    
    @Override
    public String toString() {
        return this.birthday;
    }
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
}


3  准备开始编写数据绑定关系了,

第一步是根据实体类生成binding.xml描述文件,这一步也可以手动自己写,主要是描述实体和xml的映射关系。

第二步是根据binding.xml编译生成中间操作类,实际上解码编码的时候都是用的生成的中间类,所以如果需要修改映射的时候也需要重新生成中间类.

ant文件如下,由于刚学ant,写的不好的地方多指教。主要是要在 compile编译的时候 指定destdir位子是maven项目的class位子。其中maven里面的jar文件路径自行修改下。

binggen是第一步,bind是第二步。生成好binding.xml后,第一步就不用每次都用ant编译了。

<?xml version="1.0" encoding="utf-8"?>
<project default="main" basedir=".">
	<path id="classpath">
		<dirset dir="${basedir}/target/classes" />
		<dirset dir="${basedir}/target/test-classes" />
		<fileset dir="D:/maven_repo/org/jibx/jibx-bind/1.2.6/" includes="*.jar" />
		<fileset dir="D:/maven_repo/org/jibx/jibx-run/1.2.6/" includes="*.jar" />
		<fileset dir="D:/maven_repo/bcel/bcel/5.1/" includes="*.jar" />
	</path>
	<target name="main" depends="compile,binggen, bind" description="Main target" />
	<target name="compile" description="Compilation target">
		<echo>Building file.</echo>
		<javac srcdir="${basedir}/src/main/java" destdir="${basedir}/target/classes" includeantruntime="true" />
	</target>
	<target name="binggen">
		<echo message="Running BindGen tool" />
		<java classpathref="classpath" fork="true" failonerror="true" classname="org.jibx.binding.BindingGenerator">
			<arg value="com.keeley.Account" />
		</java>
	</target>
	<target name="bind">
		<echo message="Running bind" />
		<taskdef name="bind" classname="org.jibx.binding.ant.CompileTask">
			<classpath refid="classpath"/>
		</taskdef>
		<bind binding="${basedir}/binding.xml">
			<classpath refid="classpath"/>
		</bind>
	</target>
</project>

 然后看target目录  应该多出很多 JiBX_开头的class文件。 

 4 编写测试类

 

public class JibxTest {
	private IBindingFactory factory = null;

	private StringWriter writer = null;
	private StringReader reader = null;

	private Account bean = null;
	@Before
        public void init() {
        bean = new Account();
        bean.setAddress("北京");
        bean.setEmail("email");
        bean.setId(1);
        bean.setName("jack");
        Birthday day = new Birthday();
        day.setBirthday("2010-11-22");
        bean.setBirthday(day);
        try {
            factory = BindingDirectory.getFactory(Account.class);
        } catch (JiBXException e) {
            e.printStackTrace();
        }
    }
    
    @After
    public void destory() {
        bean = null;
        try {
            if (writer != null) {
                writer.flush();
                writer.close();
            }
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void bean2XML() {
        try {
            writer = new StringWriter();
            // marshal 编组
            IMarshallingContext mctx = factory.createMarshallingContext();
            mctx.setIndent(2);
            mctx.marshalDocument(bean, "UTF-8", null, writer);
            fail(writer);
            
            reader = new StringReader(writer.toString());
            //unmarshal 解组
            IUnmarshallingContext uctx = factory.createUnmarshallingContext();
            Account acc = (Account) uctx.unmarshalDocument(reader, null);
            fail(acc);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void fail(Object o) {
        System.out.println(o);
    }
    
    public void failRed(Object o) {
        System.err.println(o);
    }

}

 5 运行junit之后的文件大概是这样的

 

<?xml version="1.0" encoding="UTF-8"?>
<account id="1">
  <name>jack</name>
  <email>email</email>
  <address>北京</address>
  <birthday>
    <birthday>2010-11-22</birthday>
  </birthday>
</account>

  id给自动弄成属性了,如果想改成子节点,修改 binding.xml 文件 

 usage="optional" 这个代表属性是可选的 不是必须的。

 在 <value name="id" field="id"/> 上面 加上属性  style="element" 就可以了。

然后重新运行 ant bind就好了(去掉 ant中的第一步 binggen)


参考了一下文章 http://www.cnblogs.com/hoojo/archive/2011/04/27/2030205.html


转载于:https://my.oschina.net/hongse/blog/376354

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值