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数据库
<?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数据库
解决方案 »
- jfreechart饼图问题
- struts2问题~这是什么问题~
- 关于java读入一行数据并根据空格分开的问题
- 问:我的Spring中这样使用总是报错<aop:aspectj-autoproxy proxy-target-class="true"/>
- VelocityWeb 即将升级,增加 SQL 分页功能
- SpringSecurity 怎样禁用缓存或者在更新数据时更新缓存?在线等
- JTA怎样管理两个不同数据库的事务一致性?
- 什么样的对象和变量才能被回收
- 请问有哪位大虾知道可以免费下载(既不需付费又不需注册)电影的站点,请告知小弟好吗?谢谢!
- 我的服務器名為pysql\develop,連接時出錯!
- 【100分】 org.eclipse.jetty.io.EofException 什么原因啊?
- HQL联表查询的问题。。
这句话输出了吗