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数据库
解决方案 »
- 根据表名查询mySql中的字段、类型和字段注释的SQL语句
- 从数据库获取最近几天记录
- 有木有学习多线程和io比较好的书啊
- SessionBean的生命周期
- strust1.2动态上传多个文件只能得到一个的问题
- 用过WAS6.1的朋友帮帮忙~
- 送分!!请教Struts高手!!!!!!!!
- 我现在正有用一台hp-nux下,装有tomcat+oracle开发jsp环境,可是JAVA进程老是占用很大,而且TOMCAT总是死掉,不知是为什么?
- 用http上传下载文件
- JAVA解析JSON串
- 【100分】 org.eclipse.jetty.io.EofException 什么原因啊?
- HQL联表查询的问题。。
这句话输出了吗