1. 程式人生 > >Hibernate (基礎入門)

Hibernate (基礎入門)

Hibernate開發步驟

  1. 建立持久化類(Book)
  2. 建立物件--關係對映檔案(Book.hbm.xml)
  3. 建立Hibernate配置檔案(Book.hbm.xml)
  4. 建立介面(BookDao)
  5. 建立實現介面類(BookImpl)

整個web結構如下

建立持久化類Book

public class Book implements Serializable{

	private static final long serialVersionUID = 1L;
	private String bookid;
	private String bookname;
	private double bookprice;
	private String bookimg;
	
    //get 和  set方法省略
	
	

}

資料庫表結構如下              

建立物件--關係對映檔案(Book.hbm.xml)

<?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>
	<!--  name="要對映的物件類名"
		  table="此物件對應的資料庫裡面的表的名字",如果不寫,自動找與類名相同的表名
		  hibernate裡面如果name與column相同,可以省略column,但推薦寫上-->
	<class name="com.lanou.bookstore.domain.Book" table="BOOKINFO">
		<id name="bookid" column="BOOKID" type="java.lang.String" />
		<property name="bookname" column="BOOKNAME" type="java.lang.String"/>
		<property name="bookprice"/>
		<property name="bookimg" column="BOOKIMG" type="java.lang.String"/>
	</class>
</hibernate-mapping>

建立Hibernate配置檔案(Book.hbm.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>
		
		<!-- 資料庫連線 -->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

		<!-- JDBC URL -->
		<property name="connection.url">jdbc:mysql://localhost/bookdb?characterEncoding=utf-8</property>

		<!-- 資料庫使用者名稱 -->
		<property name="connection.username">root</property>

		<!-- 資料庫密碼 -->
		<property name="connection.password">123</property>
		
		<!-- SQL dialect 資料庫方言 -->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

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

		<!-- Drop and re-create the database schema on startup,create無條件建立表/update沒有表的時候建立 -->
<!-- 		<property name="hbm2ddl.auto">create</property> -->

		<mapping resource="com/lanou/bookstore/config/Book.hbm.xml" />
	
	</session-factory>
</hibernate-configuration>

建立介面(BookDao)

package com.lanou.bookstore.dao;

import java.util.List;

import com.lanou.bookstore.domain.Book;

public interface BookDao {
	//新增
	public void saveBook(Book book);
	
	//修改
	public void updateBook(Book book);
	
	//刪除
	public void deteteBook(Book book);
	
	//查詢所有
	public List<Book> getBooks();
	
}

建立得到session 的工具類 HibernateUtil

package com.lanou.bookstore.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static Configuration configuration = null;
	private static SessionFactory sessionFactory = null;
	private static Session session = null;
	
	static {
		configuration = new Configuration().configure("com/lanou/bookstore/config/hibernate.cfg.xml");
	}
	
	//構建工廠
	public static SessionFactory getSessionFactory() {
		if(sessionFactory == null) {
			sessionFactory = configuration.buildSessionFactory();
		}
		return sessionFactory;
	}
	
	//得到session
	
	public static Session getSession() {
		sessionFactory = getSessionFactory();
		if(session==null) {
			session = sessionFactory.openSession();
		}
		return session;
	}
	
	//
	private static HibernateUtil hibernateUtil = null;
	
	//私有化構造方法,自己能自己new自己
	private HibernateUtil() {
		
	}
	
	//自己例項化
	public static HibernateUtil getInstance() {
		if(hibernateUtil==null) {
			hibernateUtil = new HibernateUtil();
		}
		return hibernateUtil;
	}
}

建立實現介面類(BookImpl)

package com.lanou.bookstore.impl;

import java.util.List;

import org.hibernate.Session;

import com.lanou.bookstore.dao.BookDao;
import com.lanou.bookstore.domain.Book;
import com.lanou.bookstore.util.HibernateUtil;

public class BookImpl implements BookDao{
	HibernateUtil hibernateUtil = HibernateUtil.getInstance();
	Session session = hibernateUtil.getSession();
	
	@Override
	public void saveBook(Book book) {
		session.save(book);
		session.beginTransaction().commit();
	}

	@Override
	public void updateBook(Book book) {
		session.update(book);
		session.beginTransaction().commit();
	}

	@Override
	public void deteteBook(Book book) {
		session.delete(book);
		session.beginTransaction().commit();
	}

	@Override
	public List<Book> getBooks() {
		return null;
	}
	
}

測試類 TestAB

package com.lanou.bookstore.test;

import com.lanou.bookstore.dao.BookDao;
import com.lanou.bookstore.domain.Book;
import com.lanou.bookstore.impl.BookImpl;
import com.lanou.bookstore.util.HibernateUtil;

public class TestAB {
	public static void main(String[] args) {
		
		Book book = new Book();
		book.setBookid("33333");
		book.setBookimg("33333");
		book.setBookname("3333");
		book.setBookprice(33.33);
		
		BookDao bookDao = new BookImpl();
		bookDao.saveBook(book);
		
	}
}

執行結果: