Hibernate基於Maven入門例項,與MyBatis比對

oscar999發表於2020-04-06

此篇承接上一篇: MyBatis基於Maven入門例項


準備部分
1. 進入http://mvnrepository.com/ , 搜尋 Hibernate ORM, 找到相應的版本後, 可以看到配置


2. 將這一段Copy 到pom.xml 中


3. 在eclipse中, 右鍵單擊pom.xml, 點Run As --> Maven build


在Goals 中輸入 clean install。


在命令端執行一下: mvn eclipse:eclipse


程式碼部分:

4. 實體類 User.java

/**
 * @Title: User.java
 * @Package com.oscar999
 * @Description: TODO
 * @author oscar999
 * @date May 3, 2018 4:05:49 PM
 * @version V1.0
 */

package com.oscar999;

/**
 * @ClassName: User
 * @Description: TODO
 * @author oscar999
 */

public class User {

	private String id;
	private String userId;
	private String userName;

	public String getId() {
		return id;
	}

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

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

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

	@Override
	public String toString() {
		return "User[id=" + id + ",userId=" + userId + ",userName=" + userName + "]";
	}

}
5. hibernate 配置檔案 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>
		<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
		<property name="hibernate.connection.url">jdbc:oracle:thin:@172.26.141.90:1521:orcl</property>
		<property name="hibernate.connection.username">esdmbrn</property>
		<property name="hibernate.connection.password">oracle</property>
		<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
		<mapping resource="User.hbm.xml" />
	</session-factory>
</hibernate-configuration>



6. 類配置檔案 User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
		 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
		 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.oscar999.User" table="t_user">
		<id name="Id" type="java.lang.String">
			<column name="Id" />
			<generator class="assigned" />
		</id>
		<property name="userId" type="java.lang.String">
			<column name="userId" />
		</property>
		<property name="userName" type="java.lang.String">
			<column name="userName" />
		</property>
	</class>
</hibernate-mapping>

7. 測試類

/**
 * @Title: HibernateTest.java
 * @Package com.oscar999
 * @Description: TODO
 * @author oscar999
 * @date May 4, 2018 3:43:57 PM
 * @version V1.0
 */

package com.oscar999;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.junit.Test;

/**
 * @ClassName: HibernateTest
 * @Description: TODO
 * @author oscar999
 */

public class HibernateTest {
	@Test
	public void test() {
		Configuration configuration = new Configuration().configure();
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		String hql = "from User where id=?";

		@SuppressWarnings("unchecked")
		Query<User> query = session.createQuery(hql);
		query.setParameter(0, "1");
		List<User> list = query.list();
		if (list != null && list.size() > 0) {
			User user = list.get(0);
			System.out.println(user);
		}
		transaction.commit();
		session.close();
		sessionFactory.close();

	}
}

專案路徑:




比較:

以查詢一筆資料的例子來說。


1. 實體類基本沒差別。
2. 核心配置檔案, 基本上都會配置資料庫連線資訊和需要對映的類。
hibernate直接配置和類對應的配置, 很多工具可以做到類和xml的相互轉換, 乃至表和這兩個的自動轉換。
MyBatis 配置的是方法及對應SQL , 已經返回的型別(實體類)。
3. 對映類。
hibernate ,Class 和 XML 一一對應.
MyBatis, XXMapper, 對應SQL和方法

相關文章