hibernate1
hibernate.cfg.xml
<?xmlversion='1.0'encoding='UTF-8'?>
<!DOCTYPEhibernate-configurationPUBLIC
"-//Hibernate/HibernateConfigurationDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!--GeneratedbyMyEclipseHibernateTools.-->
<hibernate-configuration>
<session-factory>
<propertyname="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<propertyname="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<propertyname="connection.username">oa100Admin</property>
<propertyname="connection.password">oa100Admin</property>
<propertyname="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<propertyname="myeclipse.connection.profile">Oracle10g</property>
<mappingresource="com/hy/oa/entity/Users.hbm.xml"/>
<mappingresource="com/hy/oa/entity/Vacation.hbm.xml"/>
<mappingresource="com/hy/oa/entity/Email.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Users.hbm.xml
<?xmlversion="1.0"encoding="utf-8"?>
<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
MappingfileautogeneratedbyMyEclipsePersistenceTools
-->
<hibernate-mapping>
<classname="com.hy.oa.entity.Users"table="USERS">
<idname="UId"type="java.lang.Integer">
<columnname="U_ID"precision="5"scale="0"/>
<generatorclass="increment"/>
</id>
<propertyname="uname"type="java.lang.String">
<columnname="UNAME"length="20"not-null="true"/>
</property>
<propertyname="nickname"type="java.lang.String">
<columnname="NICKNAME"length="20"not-null="true"/>
</property>
<propertyname="upassword"type="java.lang.String">
<columnname="UPASSWORD"length="16"not-null="true"/>
</property>
<propertyname="usex"type="java.lang.Integer">
<columnname="USEX"length="2"/>
</property>
<propertyname="uage"type="java.lang.Integer">
<columnname="UAGE"precision="3"scale="0"not-null="true"/>
</property>
<propertyname="utelephone"type="java.lang.String">
<columnname="UTELEPHONE"length="20"not-null="true"/>
</property>
<propertyname="uaddress"type="java.lang.String">
<columnname="UADDRESS"length="50"/>
</property>
<propertyname="role"type="java.lang.String">
<columnname="ROLE"length="20"not-null="true"/>
</property>
<propertyname="isdeleted"type="java.lang.Integer">
<columnname="ISDELETED"precision="2"scale="0"/>
</property>
</class>
</hibernate-mapping>
HibernateUtil.java
importjavax.servlet.jsp.jstl.core.Config;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.cfg.Configuration;
/**
*ThreadLocal是解决线程安全问题一个很好的思路,它通过为每个线程提供一个独立的变量副本解决了变量并发访问的冲突问题。
*在很多情况下,ThreadLocal比直接使用synchronized同步机制解决线程安全问题更简单,更方便,且结果程序拥有更高的并发性
*
*@authorhuangyue
*
*/
publicclassHibernateUtil{
privatestaticThreadLocal<Session>session=newThreadLocal<Session>();
privatestaticSessionFactorysf;
static{
//读取配置文件hibernate.cfg.xml,实现hibernate初始化
sf=newConfiguration().configure().buildSessionFactory();
}
publicstaticSessiongetSession(){
Sessions=session.get();
if(s==null||!s.isOpen()){
s=sf.openSession();
session.set(s);
}
returns;
}
}
publicclassUsersDaoImplimplementsUsersDao{
//查找所有用户信息
publicList<Users>findAll(){
Sessions=HibernateUtil.getSession();
Queryquery=s.createQuery("fromUsers");
List<Users>list=query.list();
s.close();
returnlist;
}
publicstaticvoidmain(String[]args){
UsersDaoImplim=newUsersDaoImpl();
List<Users>list=im.findAll();
}
publicbooleanupdate(Usersuser){
System.out.println("dao-"+user);
Sessions=HibernateUtil.getSession();
Transactiont=s.beginTransaction();
s.update(user);
t.commit();
s.close();
returntrue;
}
publicbooleanaddUser(Usersu){
Sessions=HibernateUtil.getSession();
Transactiont=s.beginTransaction();
s.save(u);
t.commit();
s.close();
returntrue;
}
//是否重名
publicbooleanfindByName(Stringuname){
Stringhql="fromUsersasuwhereu.uname=:sameName";
Sessions=HibernateUtil.getSession();
Queryq=s.createQuery(hql);
q.setString("sameName",uname);
List<Users>list=(List<Users>)q.list();
if(list.size()==0){
returntrue;
}
if(list.size()>0){
returnfalse;
}
returnfalse;
}
}
List<Vacation>list=getHibernateTemplate().find("fromVacation");