1. 程式人生 > >hibernate的簡單配置和使用

hibernate的簡單配置和使用

Hibernate的作用:

在這裡插入圖片描述 這個是Hibernate的一個簡單結構。

Hibernate是一款持久化框架(ORM)即Object Relationship Mapping,對JDBC進行了輕量級的封裝,他能夠自動根據我們的需求,能夠將我們的實體類對映到資料庫表中而進行儲存。 這就是它的作用。

我們需要配置兩個配置檔案: ①/TestHibernate/src/hibernate.cfg.xml ②/TestHibernate/src/entity/Student.hbm.xml 這兩個檔案所在的位置大家也看到了,hibernate.cfg.xml在src下,Student.hbm.xml和實體類放在同一個包下。 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>
			<!-- 1. 資料庫相關 -->
			<property name="connection.username">root</property>
			<property name="connection.password">123</property>
			<property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8</property>
			<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
			<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
	
			<!-- 配置本地事務(No CurrentSessionContext configured!) -->
			<property name="hibernate.current_session_context_class">thread</property>
			
			<!-- 2. 除錯相關   顯示sql  格式化sql-->
			<property name="show_sql">true</property>
			<property name="format_sql">true</property>
			
			<!-- hibernate需要管理的資料庫表對應的實體類對映檔案 -->
			<mapping resource="com/zking/test/entity/Student.hbm.xml"/>
			
		</session-factory>
	</hibernate-configuration>

Student.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    <!-- 
    	table:實體類所對應的表
    	name:實體類的全類名
     -->
    	<class table="t_struts_student" name="com.zking.test.entity.Student" >
    	<!-- 
    		name:實體類的屬性
    		type:實體類的屬性型別
    		column:指的是資料庫表的主鍵列
    	 -->
    		<id name="sid" type="java.lang.Integer" column="sid">
    			
    		</id>
    		<!-- id標籤打頭,代表配置的是資料庫表與實體類的關係,其他property配置的是資料庫表的普通列 -->
    		<property name="sname" type="java.lang.String" column="sname"></property>	
    		<property name="spin" type="java.lang.String" column="spin"></property>	
    		<property name="sex" type="java.lang.String" column="sex"></property>	
    		<property name="mark" type="java.lang.String" column="mark"></property>
    		<property name="cid" type="java.lang.Integer" column="cid"></property>	
    	
    		<property name="fileFileName" type="java.lang.String" column="fileFileName"></property>
    		<property name="fileContentType" type="java.lang.String" column="fileContentType"></property>
    	</class>
    </hibernate-mapping>

有兩個配置檔案的dtd約束在我們我Hibernate核心jar包裡面有:

在這裡插入圖片描述 接下來需要我們再entity包下建立對應的實體類:

package com.zking.one.entity;

import java.io.Serializable;

public class User implements Serializable {

	private static final long serialVersionUID = -3363120264501521428L;

	private Integer id;
	private String userName;
	private String userPwd;

	private String realName;
	private String sex;
	private java.sql.Date birthday;

	private java.sql.Timestamp createDatetime;
	private String remark;

	public User() {
		super();
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public java.sql.Date getBirthday() {
		return birthday;
	}

	public void setBirthday(java.sql.Date birthday) {
		this.birthday = birthday;
	}

	public java.sql.Timestamp getCreateDatetime() {
		return createDatetime;
	}

	public void setCreateDatetime(java.sql.Timestamp createDatetime) {
		this.createDatetime = createDatetime;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", userName=" + userName + ", userPwd=" + userPwd + ", realName=" + realName
				+ ", sex=" + sex + ", birthday=" + birthday + ", createDatetime=" + createDatetime + ", remark="
				+ remark + "]";
	}

}

接下來使我們的測試檔案:

package com.zking.one.test;

import java.util.Map;

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

import com.zking.one.entity.User;

public class demo1 {
		public static void main(String[] args) {
			/**
			 * 1,讀取hibernate.cfg.xml檔案
			 * 2,通過xml建模得到sessionfactory工廠
			 * 3,獲取資料庫操作的當前會話
			 * 4,開啟事務
			 * 5,具體的資料庫操作
			 * 6,提交事務
			 * 7,將會話(session)關閉
			 */
			
			Configuration configure = new Configuration().configure("hibernate.cfg.xml");
			SessionFactory SessionFactory = configure.buildSessionFactory();
			Session session = SessionFactory.openSession();
			Transaction transaction = session.beginTransaction();
			
			 User  user =  new User();
			 user.setUserName("祥");
			 user.setUserPwd("123");
			 user.setRemark("keng");
			 user.setSex("男");
			 user.setRemark("xxx00");
			 session.save(user);
			 
			 transaction.commit();
			 session.close();
			
		}
}