Hibernate入门

测试环境:hibernate 3.3

1hibernate下载:www.hibernate.org

2pojo

       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"

    如果需要查询缓存,还需要在使用QueryCriteria()时设置其setCacheable(true);属性

    -->     

    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

       <mapping resource="hibernate/bean/UserInfo.hbm.xml"/>      

    </session-factory>

</hibernate-configuration>

 

5、编写HibernateUtilhibernate.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();  
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值