hibernate-cfg.xml//名字
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>    <session-factory>
     <property name="connection.url">jdbc:sqlserver://localhost:1433; DatabaseName=hibernate</property>
     <property name="connection.username">sa</property>
     <property name="connection.password"></property>
     <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
     <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
   <property name="show_sql">ture</property>
  
   <mapping resource="Person.hbm.xml"/>
    </session-factory></hibernate-configuration>
person.hbk.xml//名字<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="com.test.model.Person" table="person">

<id name="id" column="id" type="int">
<generator class="increment"></generator>
</id>

<property name="username" column="username" type="string"></property>
<property name="password" column="password" type="string"></property>
<property name="age" column="age" type="int"></property>
<property name="registerDate" column="registerDate" type="date"></property>



</class>
</hibernate-mapping>
struts.xml//名字
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="hibernate" extends="struts-default">
<action name="savePerson" class="com.test.action.PersonAction" method="savePerson">

</action>

</package>
</struts>register.jsp
 <body>
    <form action="savePerson">
    username:<input type="text" name="username" size="20"><br>
    password:<input type="password" name="password" size="20"><br>
    age:<input type="text" name="age" size="20"><br>
    <input type="submit" value="submit">
    </form>
  </body>PersonAction.java//名字
package com.test.action;import com.opensymphony.xwork2.ActionSupport;
import com.test.model.Person;
import com.test.service.PersonService;
import com.test.service.PersonServiceImpl;public class PersonAction extends ActionSupport
{
private String username;

private String password;

private int age;

public String getUsername()
{
return username;
} public void setUsername(String username)
{
this.username = username;
} public String getPassword()
{
return password;
} public void setPassword(String password)
{
this.password = password;
} public int getAge()
{
return age;
} public void setAge(int age)
{
this.age = age;
} public String savePerson() throws Exception
{

Person person = new Person();
person.setUsername(username);
person.setPassword(password);
person.setAge(age);
java.sql.Date registerDate = new java.sql.Date(new java.util.Date().getTime());
person.setRegisterDate(registerDate);

PersonService personService = new PersonServiceImpl();

personService.savePerson(person);
return SUCCESS;
}

}PersonDAO.java//名字
package com.test.dao;import com.test.model.Person;public interface PersonDAO
{
public void savePerson(Person person);
}PersonDAOImpi.java//名字
package com.test.dao.impl;import org.hibernate.Session;
import org.hibernate.Transaction;import com.test.dao.PersonDAO;
import com.test.model.Person;
import com.test.util.HibernateUtil;public class PersonDAOImpl implements PersonDAO
{ @Override
public void savePerson(Person person)
{
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();

try
{
session.save(person);
tx.commit();
System.out.println("hehe");
}
catch(Exception ex)
{
if(null !=tx)
{
tx.rollback();
}
}
finally
{
HibernateUtil.close(session);
}



}}Person.java//名字
package com.test.model;import java.sql.Date;public class Person
{
private Integer id;

private String username;

private String password;

private Integer age;

private Date registerDate; public Integer getId()
{
return id;
} public void setId(Integer id)
{
this.id = id;
} public String getUsername()
{
return username;
} public void setUsername(String username)
{
this.username = username;
} public String getPassword()
{
return password;
} public void setPassword(String password)
{
this.password = password;
} public Integer getAge()
{
return age;
} public void setAge(Integer age)
{
this.age = age;
} public Date getRegisterDate()
{
return registerDate;
} public void setRegisterDate(Date registerDate)
{
this.registerDate = registerDate;
}







}PersonService.java//名字
package com.test.service;import com.test.model.Person;public interface PersonService
{
public void savePerson(Person person);
}PersonServiveImpl.java//名字
package com.test.service;import com.test.dao.PersonDAO;
import com.test.dao.impl.PersonDAOImpl;
import com.test.model.Person;public class PersonServiceImpl implements PersonService
{ @Override
public void savePerson(Person person)
{
PersonDAO persondao = new PersonDAOImpl();
persondao.savePerson(person); }}
HibernateUtil.java//名字
package com.test.util;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class HibernateUtil
{
private static SessionFactory sessionFactory;

static
{
try
{
sessionFactory = new Configuration().
configure().buildSessionFactory();


}
catch(Exception e)
{
e.printStackTrace();
}
}
public static Session openSession()
{
Session session = sessionFactory.openSession();
return session;

}
public static void close(Session session)
{
if(session != null)
{
session.close();
}
}
}
hibernatestruts数据库