Hibernate的實現(基於Maven)(Hibernate學習二)
阿新 • • 發佈:2018-12-15
一、建立Maven專案
二、新增hibernate所需的包
在pom.xml檔案標籤當中新增如下依賴:
<!-- 新增Hibernate依賴 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.10.Final</version> </dependency> <!-- mysql資料庫的驅動包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!-- 新增javassist --> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.12.0.GA</version> </dependency>
三、建立資料庫及其表
在mysql中建立hibernate資料庫和建立user表,具體建立如下:
create databse hibernate ;
use hibernate
create table user(
`id` INT(10) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(10) NOT NULL,
`password` VARCHAR(20) NOT NULL,
PRIMARY KEY (`id`) USING BTREE
);
四、配置hibernate.cfg.xml檔案
該檔案配置在resource下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="hibernate.bytecode.use_reflection_optimizer">false</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="person">true</property> </session-factory> </hibernate-configuration>
五、書寫java實體類和配置對映檔案
書寫需要的實現序列化介面和get和set方法,配置對應對映檔案如下:
<?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="org.qww.entity.User" table="USER" catalog="hibernate">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="NAME" length="10" not-null="true" unique="true" />
</property>
<property name="password" type="string">
<column name="PASSWORD" length="20" not-null="true" unique="true" />
</property>
</class>
</hibernate-mapping>
將對映檔案放到hibernate.cfg.xml檔案中進行載入,即在 中新增如下配置:
<mapping resource="org.qww.hbm/User.hbm.xml"></mapping>
六、書寫工具類,載入配置檔案,獲取SessionFactory
具體實現如下:
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml,configure()有多種形式,可指明檔案的載入檔案的路徑
return new Configuration().configure().buildSessionFactory() ;
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
七、書寫測試類測試程式碼
測試類如下:
public class Test {
private static Transaction tx;
public static void main( String[] args )
{
System.out.println("Maven + Hibernate + MySQL");
Session session = HibernateUtil.getSessionFactory().openSession();
User user = new User() ;
user.setName("hello");
user.setPassword("word");
tx = session.beginTransaction() ;
session.save(user) ;
tx.commit();
}
}
執行測試類,查詢資料庫,資料成功儲存到資料庫中。