`
slip_123
  • 浏览: 84237 次
  • 性别: Icon_minigender_1
  • 来自: 石家庄
社区版块
存档分类
最新评论

hibernate-HelloWorld

    博客分类:
  • java
 
阅读更多
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class"
>com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="connection.url"
>jdbc:sqlserver://localhost;databaseName=test</property>
        <property name="connection.username"
>sa</property>
        <property name="connection.password"
>sasasa</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size"
>1</property>

        <!-- SQL dialect -->
        <property name="dialect"
>org.hibernate.dialect.SQLServerDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class"
>thread</property>

        <!-- Disable the second-level cache  -->
        <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql"
>true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto"
>update</property>

        <mapping resource="org/aaa/model/User.hbm.xml"/>
		<mapping resource="org/aaa/model/Role.hbm.xml"/>
		<mapping resource="org/aaa/model/Student.hbm.xml"/>
		<mapping resource="org/aaa/model/Book.hbm.xml"/>
    </session-factory>

</hibernate-configuration
>


Test.java
SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session= sf.getCurrentSession();
        session.beginTransaction();
//        //添加图书
//        Books book = new Books();
//        book.setName("aa");
//        book.setPirce(23);
//        book.setPublishTime(new Date());
//        session.save(book);
session.getTransaction().commit();


Book.java
<?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">

<!--

    Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping package="org.aaa.model">

    <class name="org.aaa.model.TestBook" table="tbl_testbook" >

        <id name="bid" >
            <generator class="native"/>
        </id>

        <property name="bname">
            <column name="bname"/>
        </property>

        <property name="bprice">
            <column name="bprice"/>
        </property>

    </class>

</hibernate-mapping>


需要的包:以hibernate-distribution-3.3.1.GA为例
hibernate3.jar
/lib/required目录所有包
连接数据库的驱动包
commons-logging-1.0.4.jar
ehcache-1.2.3.jar
如图:

  • 大小: 16 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics