测试环境:hibernate 3.3
1、hibernate下载:www.hibernate.org
2、pojo类
public class UserInfo
{
private Integer id;
private String userName;
private String password;
... ...
3、映射文件:
dtd声明:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="hibernate.bean.UserInfo" table="user_info">
<id name="id" type="integer">
<column name="id"></column>//在上边
<generator class="assigned"></generator>//在下边
</id>
<property name="userName" column="user_name" type="string" length="100"></property>
<property name="password" type="string">
<column name="password" length="100"></column>
</property>
</class>
</hibernate-mapping>
4、配置文件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">
<hibernate-configuration>
<session-factory>
<!--
com.ddtek.jdbc.sqlserver.SQLServerDriver
jdbc:datadirect:sqlserver://localhost:1433
com.mysql.jdbc.Driver jdbc:mysql://localhost/MyTest
org.hibernate.dialect.MySQLDialect org.hibernate.dialect.SQLServerDialect
-->
<property name="connection.driver_class">com.ddtek.jdbc.sqlserver.SQLServerDriver</property>
<property name="connection.url">jdbc:datadirect:sqlserver://localhost:1433;databaseName=test</property>
<property name="connection.username">sa</property>
<property name="connection.password"> </property>
<!-- 连接池大小 -->
<property name="connection.pool_size">20</property>
<!-- -->
<property name="show_sql">true</property>
<property name="jdbc.fetch_size">50</property>
<property name="jdbc.batch_size">25</property>
<property name="jdbc.use_scrollable_resultset">false</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<!--
validate 加载hibernate时,验证创建数据库表结构
create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
create-drop 加载hibernate时创建,退出是删除表结构
update 加载hibernate自动更新数据库结构
-->
<property name="hbm2ddl.auto">none</property>
<!-- Enable Hibernate's automatic session context management
集成Hibernate的环境下(例如Jboss),在session-factory段加入:jta
-->
<property name="current_session_context_class">thread</property>
<!--
Disable the second-level cache
二级缓存是SessionFactory级别的全局缓存
org.hibernate.cache.EhCacheProvider
org.hibernate.cache.OSCacheProvider
如果使用查询缓存,加上 hibernate.cache.use_query_cache=true
缓存可以简单的看成一个Map,通过key在缓存里面找value。
如果用ehcache,配置ehcache.xml
在Hbm文件中添加<cache usage="read-only" >
如果需要“查询缓存”,还需要在使用Query或Criteria()时设置其setCacheable(true);属性
-->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<mapping resource="hibernate/bean/UserInfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5、编写HibernateUtil(hibernate.cfg.xml放在src下)
public class HibernateUtil
{
public static SessionFactory buildSessionFactory()
{return new Configuration().configure().buildSessionFactory();}
public static SessionFactory getSessionFactory(){return sessionFactory; }
public static final SessionFactory sessionFactory = buildSessionFactory();
}
6、测试类:JUnit
@Test
public void testOne() throws Exception
{
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();
UserInfo info = new UserInfo();
info.setId(new Integer(1));
info.setUserName("test");
info.setPassword("password");
session.save(info);
transaction.commit();
transaction = null;
HibernateUtil.getSessionFactory().close();
}