- 環境:win7,Intellij IDEA
 - postgreSQL的安裝: 
          
- 安裝包下載:https://www.postgresql.org/download/ 去官網下載postgresql-9.1.3-1-windows.exe(46M)
 - 傻瓜式安裝,跟着安裝引導走,一路next,用戶名默認為 postgres,密碼*****,端口默認5432
 - 啟動服務,打開services.msc,如果postgre沒有啟動則手動啟動
 
 - postgreSQL客戶的工具的安裝:目前有多種客戶工具,我用的是navicat for postgreSQL,去官網下載https://www.navicat.com/download/navicat-for-postgresql 
          
- 解壓壓縮包,點擊navicat.ext啟動
 - 雙擊連接->填寫用戶名,密碼,端口,連接名,創建連接
 - 右鍵點擊連接名稱,創建數據庫
 - 創建模式,(postgre的數據結構多了一層模式的結構,數據庫>模式>表>字段),數據庫創建時有默認的模式為public,右鍵數據庫名,創建模式myschema
 - 創建表,右鍵表,創建表,填寫字段(user_id,username,password);
 
 - 搭建hibernate環境: 
          
- 用Intellij 新建javaWeb項目
 - 引入jar包: 
            
-   hibernate jar包 在pom.xml寫依賴: 
<!--hibernate--> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.1.2.Final</version> </dependency> -  
postgreSQL jdbc jar包引入 在pom.xml中寫依賴:
<dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.1-901-1.jdbc4</version> </dependency> 
 -   hibernate jar包 在pom.xml寫依賴: 
 - 配置hibernate配置文件,項目由maven管理,在resources目錄下加入hibernate.cfg.xml文件,該配置文件主要記錄了數據庫的有戶名,ip,密碼,端口,所用jdbc驅動等信息內容如下: 
            
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "src/resource/schema/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <!-- Database connection settings --> <property name="connection.driver_class"> org.postgresql.Driver </property> <property name="connection.url"> jdbc:postgresql://10.21.132.19:5432/test </property> <property name="connection.username">postgres</property> <property name="connection.password">88075998</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect"> org.hibernate.dialect.PostgreSQLDialect </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">false</property> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">update</property> --> <!-- <mapping resource="com/hik/gss/sys/domain/User.hbm.xml" />--> <mapping class="model.User"></mapping> </session-factory> </hibernate-configuration>
 
 - hibernate的查詢語句,hibernate的操作有兩種1.xml文件配置2.注解,這里用的是注解法,圖個方便. 
          
- 實體類的定義,如下: 
            
package model; import org.hibernate.annotations.GenericGenerator; import javax.persistence.*; @Entity @Table(name="public.user") public class User { private Integer userId; private String userName; private String passWord; @Id @GeneratedValue(generator="increment") @GenericGenerator(name="increment", strategy = "increment") @Column(name = "user_id") public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } @Column(name = "username") public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Column(name = "password") public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } }
注解說明:@Entity實體類標注, @Table標注表名,注意表名前面要寫模式名(public),被坑過. @Id 表示主鍵 @Column列名 - 查詢數據庫代碼Demo: 
            
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure() .build(); SessionFactory sessionFactory = null; try { sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory(); } catch (Exception e) { // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory // so destroy it manually. StandardServiceRegistryBuilder.destroy( registry ); } Session session = sessionFactory.openSession(); session.beginTransaction(); List result = session.createQuery( "from model.User" ).list(); for ( User user : (List<User>) result ) { System.out.println( "User (" + user.getUserName() + ") : " + user.getPassWord() ); if (this.passWord.equals(user.getPassWord()) && this.userName.equals(user.getUserName())) { return "SUCCESS"; } } session.getTransaction().commit(); session.close();
總結一下查詢的步驟:- 注冊
 - 創建會話工廠
 - 會話工廠生產會話
 - 創建查詢語句
 - 會話執行查詢語句
 - 獲取結果
 
 
 - 實體類的定義,如下: 
            
 
