关于hibernate中用ant来显示后台信息

kongxuzhoumo0 2007-10-26 12:03:48
我的build.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Stduent" default="run" basedir=".">
<property name="appName" value="Stduent"></property>
<property name="src.dir" value="src"></property>
<property name="webroot.dir" value="WebRoot"></property>
<property name="lib.dir" value="${webroot.dir}/WEB-INF/lib"></property>
<property name="classes" value="${webroot.dir}/WEB-INF/classes"></property>

<path id="libAndClass.classpath">
<pathelement location="${classes}"/>
<fileset dir="${lib.dir}" includes="**/*.jar" ></fileset>
</path>

<target name="compile">
<javac srcdir="${src.dir}" destdir="${classes}">
<classpath refid="libAndClass.classpath"></classpath>
</javac>
<copy todir="${classes}">
<fileset dir="${src.dir}">
<include name="**/*.xml"/>
<include name="**/*.properties"/>
</fileset>
</copy>
</target>
<!--<target name="junit">
<junit printsummary="yes">
<test name="junit.StudentDAOTest"></test>
<classpath refid="libAndClass.classpath"></classpath>
</junit>
</target>-->
<target name="schemaexport" depends="compile">
<taskdef name="schemaexport" classname="org.hibernate.tool.hbm2ddl.SchemaExportTask" >
<classpath refid="libAndClass.classpath"></classpath>
</taskdef>
<schemaexport config="${classes}/hibernate.cfg.xml" quiet="no" text="no" drop="no" delimiter=";" output="schema-export.sql">

</schemaexport>
</target>
<target name="run" depends="schemaexport">
<java classname="com.Jesse.junit.StudentADOTest">
<classpath refid="libAndClass.classpath"></classpath>
</java>
</target>
</project>

studentADO的代码如下:
package com.Jesse.hibernate.student;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.Jesse.hibernate.sessionFactory.HibernateUtil;

public class StudentADO {
private Session session;
private Student student;
public Transaction ts;

public void createStudent(Student student){
try{
session=HibernateUtil.currentSession();
ts=session.beginTransaction();
session.save(session);
ts.commit();
}catch(HibernateException ex){
ex.printStackTrace();
ts.rollback();
}finally{
HibernateUtil.closeSession(session);
}
}
public Student getStudentByCardId(String cardId){
try{
session=HibernateUtil.currentSession();
ts=session.beginTransaction();
Query q=session.createQuery("from Student s where s.cardId=:cardId");
q.setString("cardId", cardId);
student=(Student)q.uniqueResult();
ts.commit();

}catch(HibernateException e){
e.printStackTrace();
ts.rollback();
}finally{
if(session!=null)
HibernateUtil.closeSession(session);
}
return student;
}

}

测试StudentADO的代码如下:
package com.Jesse.junit;

import org.hibernate.HibernateException;

import com.Jesse.hibernate.student.Student;
import com.Jesse.hibernate.student.StudentADO;

import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class StudentADOTest extends TestCase {
public StudentADOTest(String name){
super(name);
}
public void testCreateStudent() {
try{
Student student=new Student();
StudentADO studentADO=new StudentADO();
student.setCardId("040308022");
student.setAge("24");
student.setName("lwc");
studentADO.createStudent(student);
}catch(HibernateException ex){
ex.printStackTrace();
Assert.fail(ex.getMessage());
}

}

public void testGetStudentByCardId() {
try{
String cardId="040308021";
StudentADO dao=new StudentADO();
Student student=dao.getStudentByCardId(cardId);
System.out.println(student.getName());
}catch(HibernateException ex){
ex.printStackTrace();
Assert.fail(ex.getMessage());
}
}
public static Test suite(){
TestSuite suite=new TestSuite();
//suite.addTest(new StudentADOTest("testCreateStudent"));
suite.addTest(new StudentADOTest("testGetStudentByCardId"));
return suite;
}

public static void main(String[] args){
junit.textui.TestRunner.run(StudentADOTest.class);
}

}


在cmd里面执行ant的结果如下:
其中的:

schemaexport:

[schemaexport]drop table if exists student;
[schemaexport]create table student <id varchar(255) not null,name varchar(255) ,
cardId varchar(255),age varchar(255),primary key(id));

run:
[java].
[java]Time:12.344
[java]
[java]OK <2 tests>
[java]

...全文
40 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
likgui 2007-10-26
  • 打赏
  • 举报
回复
关注

67,542

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧