標籤:style blog class c code java
okay,瞭解了struts2後開始搭建hibernate環境
eclipse 4.3.2
jdk 1.7.0_45
hibernate 4.3.5
mysql 5.1.67
準備好上面的環境,安裝hibernate(拷貝hibernate所需lib-->requered-->jar包和mysql的java驅動jar包拷到lib裡面)
在資料庫建立一個資料庫hibernate,建立usertable表欄位有name,password
在eclipse中建立一個java project Hiernate
在src目錄下建立一個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.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> <property name="connection.username">root</property> <property name="connection.password"></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.internal.NoCacheProvider</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="com/gxf/beans/user.hbm.xml"/> </session-factory></hibernate-configuration>
在src目錄下建立一個bean,User.java
package com.gxf.beans;public class User { private String name; private String password; public User(String name, String password) { super(); this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
在與bean同目錄下建立一個user.hbm.xml(這裡可以在文檔裡面找到,根據自己的bean和資料庫表進行修改)
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 5 6 <hibernate-mapping package="com.gxf.beans"> 7 8 <class name="User" table="usertable"> 9 <id name="name"> 10 </id>11 <property name="password"/>12 </class>13 14 </hibernate-mapping>
在src目錄下建立一個測試類別,Test.java
1 package com.gxf.test; 2 3 import org.hibernate.HibernateException; 4 import org.hibernate.Session; 5 import org.hibernate.SessionFactory; 6 import org.hibernate.Transaction; 7 import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 8 import org.hibernate.cfg.Configuration; 9 import org.hibernate.service.ServiceRegistry;10 11 import com.gxf.beans.User;12 13 public class Test {14 15 public static void main(String[] args) {16 try {17 Configuration configuration = new Configuration().configure();18 ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();19 SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);20 Session session = sessionFactory.openSession();21 22 23 Transaction tx = session.beginTransaction();24 User userToSave = new User("lisi","luckyzhangsan");25 session.save(userToSave);26 tx.commit();27 session.close();28 } catch (HibernateException e) {29 e.printStackTrace();30 }31 }32 33 }
最後運行Test.java,控制台沒有報錯,病輸出對應的sql語句。到資料庫中執行尋找命令,插入成功
這裡只是為了測試hibernate環境,熟悉hibernate的工作流程,比較粗糙