一. 三大框架
Hibernate
1.安裝hibernate插件至ecilpse
2.進行配置
2.1 主配置文件
<?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="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="hibernate.connection.password">laoer123</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="hibernate.connection.username">test0816</property> <!-- 數據庫方案 --> <property name="hibernate.default_schema">TEST0816</property> <!-- 數據庫方言 --> <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- 調試 --> <property name="hibernate.format_sql">true</property> <property name="hibernate.show_sql">true</property> <!-- 自動建表方式 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 映射文件 --> <mapping resource="com/hanqi/entity/User.hbm.xml"/> <mapping resource="com/hanqi/entity/Student.hbm.xml"/> <mapping resource="com/hanqi/entity/Person.hbm.xml"/> <mapping resource="com/hanqi/entity/Course.hbm.xml"/> <mapping resource="com/hanqi/entity/Teacher.hbm.xml"/> </session-factory> </hibernate-configuration>
2.21 創建持久化類
package com.hanqi.entity; import java.util.Date; //實體化類 //不要使用final修飾 public class User { private Integer userID; private String userName; private Date birthday; private Double money; private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getUserID() { return userID; } public void setUserID(Integer userID) { this.userID = userID; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } // 必須提供無參的構造方法 //需要用到反射 public User() { super(); } public User(Integer userID, String userName) { super(); this.userID = userID; this.userName = userName; } @Override public String toString() { return "User [userID=" + userID + ", userName=" + userName + ", birthday=" + birthday + ", money=" + money + ", password=" + password + "]"; } }
2.22 創建持久化類的映射文件
創建方法 new - other-hibernate-Mapping file
<?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 2016-11-7 14:40:01 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.hanqi.entity.User" table="T_USER"> <id name="userID" type="int"> <column name="USER_ID" /> <generator class="native" /> </id> <property name="userName" type="java.lang.String"> <column name="USER_NAME" length="20" not-null="true" unique="true"/> </property> <property name="birthday" type="java.util.Date"> <column name="BIRTHDAY" sql-type="DATE"/> </property> <property name="money" type="java.lang.Double"> <column name="MONEY" sql-type="NUMBER" default="0" length="8" scale="2"/> </property> <property name="password" type="java.lang.String"> <column name="PASSWORD" length="10"></column> </property> </class> </hibernate-mapping>
3 測試用例的用法
package com.hanqi.test; import java.util.Date; //import java.util.List; import java.util.List; //import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.hanqi.entity.Course; import com.hanqi.entity.Student; import com.hanqi.entity.Teacher; import com.hanqi.entity.User; public class Test01 { private SessionFactory sf = null; private Session se=null; private Transaction ts=null; // 在測試用例方法被執行之前,自動執行的方法 // 一般用來初始化公用的對象 //前置方法 @Before public void init() { //1 獲取配置文件 Configuration cfg= new Configuration().configure(); //2 注冊配置 ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build(); //3 獲取SessionFactory(相當於JDBC的連接) sf=cfg.buildSessionFactory(sr); System.out.println(sf); //4 產生Session se= sf.openSession(); //5 啟動事務 ts=se.beginTransaction(); } //后置方法 //一般用來釋放資源 @After public void destory() { //7 提交事務 ts.commit(); //8 釋放資源 se.close(); sf.close(); } //測試Hibernate連接數據庫 @Test public void test() { //6 操作數據庫 //添加數據 //實例化的新對象,處於臨時狀態 User u1 = new User(); u1.setBirthday(new Date()); u1.setMoney(2000.0); u1.setPassword("123456"); u1.setUserName("測試1"); //u1.setUserID(4);//自然主鍵 assigned //保存數據 //通過save方法,把對象從臨時狀態轉成持久化狀態 se.save(u1); System.out.println(u1); } //測試查詢的方法 @Test public void test1() { // 查詢數據 // 提供2個參數 // 需要返回哪一個持久化類的實例 // 實例的標識(數據的主鍵值) //通過Session的get方法獲得的對象處於持久化狀態 User u2=(User)se.get(User.class, 3); u2.setUserName("修改的"); System.out.println(u2); //刪除 se.delete(u2); //使持久化對象進入刪除狀態 }
4 反向工程的用法
4.1 創建Hibernate Console Configuration
目的是創建數據庫連接一般選擇項目的配置文件hibernate.cfg.xml獲取連接信息
4.2 創建反向的配置文件 hibernate.reveng.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" > <hibernate-reverse-engineering> <table-filter match-name="STUDENT"/> <table-filter match-name="PERSON"/> <table-filter match-name="TEACHER"/> <table-filter match-name="COURSE"/> </hibernate-reverse-engineering>
4.3配置Code Generation 並運行
1.連接數據庫的Console Configuration
2.反向配置文件 .reveng.xml
3.要輸出的項目目錄
4.一般是項目的src目錄
5.要生成的包名
6.要導出的文件類型
7.持久化類
8.映射文件
4.4 不要忘記在主配置文件中去配置一下
5 HQL Hibernate Query Language 一種面向對象的查詢語句
public void test5() { //使用HQL //創建Query對象 // Query qu=se.createQuery("from User where userID < ? and userName = :uname order by userID desc"); // //設置占位符 // qu.setInteger(0, 6); // //按照參數名方式設置 // qu.setString("uname", "測試1"); // //執行查詢 // List<User>lu=qu.list(); // //方法鏈調用 // lu = se.createQuery("from User where userID < ?") // .setInteger(0, 6) // .list(); // for(User u:lu) // { // System.out.println(u); // } // se.createQuery("from User where userID < ? and userName = :uname order by userID desc") .setInteger(0, 6) .setString("uname", "測試1") .list() .stream() .forEach(System.out::println); } //測試分頁 @Test public void test6() { // 設置開始行號:頁碼 =2 // (頁碼 -1) * 每頁行數 @SuppressWarnings("unchecked") List<User> lu = se.createQuery("from User order by userID") .setMaxResults(5) .setFirstResult(5) .list(); for(User u:lu) { System.out.println(u); } } //分組查詢 @SuppressWarnings("unchecked") @Test //單列不需要數組 //多列用數組表示 public void test7() { List<Object[]>lo=se.createQuery("select userName,count(1) from User group by userName") .list(); for(Object[] o:lo) { System.out.println(o[0]+" "+o[1]); } } //測試投影查詢 @SuppressWarnings("unchecked") @Test public void test8() { List<Object[]>lo=se.createQuery("select userID,userName from User") .list(); for(Object[] obj :lo) { System.out.println(obj[0]+" "+obj[1]); } System.out.println("******************************"); // 返回持久化對象的集合 List<User> lu = se.createQuery("select new User(userID,userName) from User") .list(); for(User u:lu) { System.out.println(u); se.update(u); } } //測試新的類 @Test public void test9() { Student st = (Student)se.get(Student.class, "108"); System.out.println(st); } //測試多對一 @Test public void test10() { Course cs = (Course)se.get(Course.class, "9-888"); System.out.println(cs); } //測試一對多 @Test public void test11() { Teacher te=(Teacher)se.get(Teacher.class, "831"); System.out.println(te); se.delete(te); }