码迷,mamicode.com
首页 > Web开发 > 详细

hibernate入门

时间:2015-12-17 19:22:18      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:hibernnate 入门 hibernate.cfg.xml

技术分享技术分享


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 >


           <!-- Database connection settings -->

           <property name= "connection.driver_class">com.mysql.jdbc.Driver </property >

           <property name= "connection.url">jdbc:mysql://localhost/hibernate</property >

           <property name= "connection.username">root</property >

           <property name= "connection.password">1234rewq</property >


           <!-- JDBC connection pool (use the built-in) -->

           <property name= "connection.pool_size">1</property >


           <!-- SQL dialect 对应数据库语言 -->

           <property name= "dialect">org.hibernate.dialect.MySQLDialect </property >


           <!-- Enable Hibernate‘s automatic session context management -->

           <property name= "current_session_context_class">thread</property >


           <!-- Disable the second-level cache -->

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


     

           <!-- Echo all executed SQL to stdout -->

           <!-- 是否打印sql语句在控制台 -->

           <property name ="show_sql">true</ property>

          

           <!-- 是否在控制台按格式化输出 sql语句 -->

           <property name ="format_sql">true</ property>

          

           <!-- Drop and re-create the database schema on startup -->

           <property name ="hbm2ddl.auto">update</ property>

          

           <!-- 实体类与数据库映射 -->

           <mapping resource= "com/company/project/bean/Book.hbm.xml" />


     </session-factory >


</hibernate-configuration>

----------------------------------------------------------------------------

Book.hbm.xml


<?xmlversion="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" >

<hibernate-mapping package= "com.company.project.bean">

     <!-- name对应类里的属性  table对应数据表的内容  catalog对应数据的名字 -->

     <class name ="Book" table="book" catalog= "hibernate">

           <id name ="bookid" column="bookid" type= "java.lang.Integer">

     <!-- <generator>子元素是一个Java类的名字, 用来为该持久化类的实例生成唯一的标识。 -->

               <generator class ="native"></generator>

           </id >

           <property name ="bookname" column="bookname" type= "java.lang.String" length ="20"/>

           <property name ="bookprice" column="bookprice" type= "java.lang.Float"/>

           <property name ="author"   column= "author"  type= "java.lang.String" length ="20"/>

     </class >

</hibernate-mapping>

-----------------------------------------------------------------------------------

实体类


package com.company.project.bean;


public class Book {

     private String bookname;

     private int bookid;

     private float bookprice;

     private String author;


     public String getAuthor() {

           return author;

     }


     public void setAuthor(String author) {

           this. author = author;

     }


     public String getBookname() {

           return bookname;

     }


     public void setBookname(String bookname) {

           this. bookname = bookname;

     }


     public int getBookid() {

           return bookid;

     }


     public void setBookid(int bookid) {

           this. bookid = bookid;

     }


     public float getBookprice() {

           return bookprice;

     }


     public void setBookprice( float bookprice) {

           this. bookprice = bookprice;

     }


}

-----------------------------------------------------------------------------------

测试类


package com.company.project.test;


import java.util.List;


import org.hibernate.Query;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

import org.hibernate.classic.Session;


import com.company.project.bean.Book;


public class TestMain {

     public static void main(String[] args) {

          Configuration config = new Configuration();

           config.configure(); // 加载配置文件 默认加载hibernate.cfg.xml

          SessionFactory sessionFactory = config.buildSessionFactory(); // 得到一个SessionFactory

          Session session = sessionFactory.openSession(); // 通过sessionFactory

                                                                    // 创建连接session

                                                                    // 即JDBC中的Conncetion

          Book book = new Book();

           book.setBookname( "hibernate 3.3");

           book.setBookprice(118.0f);

           book.setAuthor( "conan");


           session.save( book); // 添加数据 由hibernate完成

          

           //增加一个事务,只有提交了才会从 hibernate缓存中添加到数据库中

           session.beginTransaction().commit();


          Query query = session.createQuery( "from Book");  //查询

          List<?> list = query.list();

           for (Object o : list) {

              Book b = (Book) o;

              System. out.println( b.getBookid()+ " "+b .getBookname()+" " +b .getBookprice()+" "+b .getAuthor());

          }

     }

}


hibernate入门

标签:hibernnate 入门 hibernate.cfg.xml

原文地址:http://icola.blog.51cto.com/5412204/1725779

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!