hibernate配置文件
<?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">
<hibernate-configuration>
<session-factory>
<property name="myeclipse.connection.profile">
JDBC for Oracle
</property>
<!--URL地址-->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:oracle</property>
<!--用户名-->
<property name="connection.username">scott</property>
<!--密码-->
<property name="connection.password">tiger</property>
<!--驱动-->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="dialect"></property>
<property name="dialect">
org.hibernate.dialect.OracleDialect
</property>
<!--开发user表的映射文件User.hbm.xml-->
<mapping resource="com/demo/hibernate/beans/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
<!--hibernate的配置文件-->
映射文件
<!--hibernate映射文件包含了对象/关系所需要的元数据 ,元数据包含持久化类的声明,和属性到数据库的映射
根元素hibernate-mapping,并通过package指定类所在的包,每一个表使用一个class定义,name属性表示类的名称,table表示关联的表名,通过property子元素来映射类的变量名与数据库表字段之间的映射关系-->
<?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">
<hibernate-mapping package="com.demo.hibernate.beans">
<class name="User" table="user">
<id name="id" column="ID" type="integer">
<generator class="native" />
</id>
<property name="username" column="username" type="string" />
<property name="password" column="password" type="string" />
<property name="email" column="email" type="string">
</hibernate>
持久化java类
public class User
{
private java.lang.Integer id ;
private java.lang.String username ;
private java.lang.String password ;
private java.lang.String email ;
};
//为属性字段声明访问器getter和setter
//提供一个标识属性
//使用非final的类
DAO类
import org.hibernate.HibernateException ;
import org.hibernate.Query ;
import org.hibernate.Session ;
import org.hibernate.Transaction ;
import com.demo.hibernate.beans.User ;
public class UserDAO
{
public User getUser() throws HibernateException
{
Session session = null ;
Transaction tx = null ;
User user = null ;
try
{
session = HibernateSessinFactory.currentSession() ;//取得session
tx = session.beginTransaction() ;
Query query = session.createQuery(from User where username=?) ;
query.setString(0,uername.trim()) ;
user = (User)query.uniqueResult() ;
query = null ;
tx.commit() ;
}
catch (HibernateException e)
{
throw e ;
}
finally
{
if (tx != null)
{
tx.rollback() ;
}
HibernateSessionFactory.closeSession() ;
}
return user ;
}
}
session工厂类
//这是一个持久化管理器,我们通过它从数据库中存取User
//SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory() ;
import org.Hibernate.HibernateException ;
import org.Hibernate.Session ;
import org.Hibernate.cfg.Configuration ;
public class HibernateSessionFactory
{
private static String CONFIG_FILE_LOCATION = "/Hibernate.cfg.xml" ;
private static final ThreadLocal threadLocal = new ThreadLocal() ;
private static final Configuration cfg = new Configuration() ;
private static org.hibernate.SessionFactory sessionFactory ;
//取得session
public static Session currentSession() throws HibernateException
{
Session session = (Session)threadLocal.get() ;//Session引入的是org.Hibernate.Session
if (session == nll)
{
if (session == null)
{
try
{
cfg.configure(CONFIG_FILE_LOCATION) ;//通过对configure()的调用来装载Hibernate.cfg.xml配置文件,并初始化成一个Configuration实例
sessionFactory = cfg.buildSessionFactory() ;//?
}
catch (Exception e)
{
System.err.println("%%%%Error Creating SessionFactory%%%") ;
}
}
session = sessionFactory.openSession() ;
threadLocal.set(session) ;
}
return session ;
}
//关闭session
public static void closeSession() throws HibernateException
{
Session session = (Session)threadLocal.get() ;
threadLocal.set(null) ;
if (session != null)
{
session.close() ;
}
}
}
service类
import com.demo.hibernate.beans.User ;
public class UserService
{
public boolean valid(String name,String word)
{
UserDAO test = new UserDAO() ;
User user = test.getUser("admin") ;
if (user.getPassword().equals(password))
{
return true ;
}
else
{
return false ;
}
}
public static void main(String args[])
{
UserService service = new UserService() ;
boolean login = service.valid("admin","admin") ;
System.out.println("验证结果为:"+login) ;
}
}