想使用Hibernate框架,在網上看了一個Hibernate學習視頻,試着作了一個小小的Java鏈接數據庫的操做,Java初學者一個,你們多多包涵java
開發環境:mysql
1.安裝MySql,sql
2.安裝了Eclipse+插件hibernatetools-Update-4.1.2插件數據庫
3.java包導入1.hibernate-core 2.Junit4.0 3.mysqlsession
思路:app
1.配置Hibernate配置文件框架
<?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="connection.username">root</property> <!-- 數據庫用戶名 --> <property name="connection.password"></property> <!-- 數據庫密碼 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&charactertEncoding=UTF-8</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <!-- SQL輸出到控制檯 --> <property name="format_sql" >true</property> <!-- 格式化SQL --> <property name="hbm2ddl.auto">update</property> <!-- create|update|create-drop|validate:檢查數據表結構是否相同 --> <mapping resource="Students.hbm.xml" /> <!-- 學生配置文件 --> </session-factory> </hibernate-configuration>
2.定義持久話學生類ide
//學生類 public class Students { // 1.公有的類 // 2.提供公有的不帶參數的默認的構造方法 // 3.屬性私有 // 4.屬性setter/getter封裝 private int sid; private String sname; private String gender; private String address; public Students() { } public Students(int sid, String sname, String gender, String address) { // super(); this.sid = sid; this.sname = sname; this.gender = gender; this.address = address; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", address=" + address + "]"; } }
3.配置Students.hbm.xml文件,這個是由Hibernate插件工具自動完成的,Src右鍵-Other-Hibernate-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"> <!-- Generated 2015-6-25 19:45:24 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="Students" table="STUDENTS"> <id name="sid" type="int"> <column name="SID" /> <generator class="assigned" /> </id> <property name="sname" type="java.lang.String"> <column name="SNAME" /> </property> <property name="gender" type="java.lang.String"> <column name="GENDER" /> </property> <property name="address" type="java.lang.String"> <column name="ADDRESS" /> </property> </class> </hibernate-mapping>
4.定義一個StudentsTest測試類學習
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; //測試類 //@SuppressWarnings("deprecation") public class StudentsTest { private SessionFactory sessionFactory; //會話工廠 private Session session; //會話 private Transaction transaction; @Before //以前 public void init() //初始化 { //建立配置對象 Configuration config=new Configuration().configure(); //建立服務註冊對象 ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); //建立會話工廠對象 sessionFactory=config.buildSessionFactory(serviceRegistry); //會話對象 session=sessionFactory.openSession(); //開啓事務 transaction=session.beginTransaction(); } @After //以後 public void destory() { transaction.commit(); //提交事務 session.close(); //關閉會話 sessionFactory.close(); //關閉會話工廠 } @Test //測試 public void testSaveStudents() { Students s=new Students(3,"張三丰","男","武當山"); session.save(s); Students s1=new Students(4,"shexunyu","男","咸寧"); session.save(s1); //保存對象進入數據庫 } }
5.到這就算完成了,在把工程的目錄結構截圖給你們
6.你們能夠右鍵StudentsTest右鍵Runas-JUnit Test進行測試吧,好啦,代碼我上傳了,點這裏下載
http://files.cnblogs.com/files/shexunyu/Java-Hibernate_002.zip
User:shexunyu Date:2015-06-26 Contact: