標籤:style blog http io ar os 使用 sp java
使用hibernate串連mysql資料庫
1:項目搭建好之後,在lib包中添加必要的jar包,和mysql資料庫驅動jar包:
jar包可以在hibernate的下載包(hibernate3.3.2.GA)中找到,這裡所需要的jar包是:
hibernate3.jar,lib/required目錄下的所有jar包;
串連資料庫所需要的jar包:mysql-connector-java-5.1.7-bin.jar;
:
2:jar包引入後編寫實體類及對應檔:
實體類是xx.java檔案;對應檔為xx.hbm.xml檔案,檔案名稱需要相同;
.java檔案內容此處省略,
.hbm.xml檔案測試代碼:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.yim.entity">
<class name="user" table="user">
<id name="userId" column="USERID">
<generator class="uuid" />
</id>
<property name="userName" type="java.lang.String" column="USERNAME"
length="20" />
</class>
</hibernate-mapping>
3:添加hibernate.cfg.xml及hibernate設定檔
該檔案在hibernate的下載包中可以找到模板:
設定檔範本:hibernate3.3.2.GA\project\tutorials\web\src\main\resources\hibernate.cfg.xml
將該檔案內容做調整即可;
此處為設定檔代碼:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/yim</property>
<property name="connection.username">root</property>
<property name="connection.password">mysql</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate‘s current session context -->
<property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- 對應檔聲明 -->
<mapping resource="com/yim/entity/user.hbm.xml" />
</session-factory>
</hibernate-configuration>
4:上面的步驟都配置後.可以進行資料庫連接測試:
測試類別代碼:
package com.yim.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.yim.entity.user;
/**
* 測試mysql資料庫連接
*
* @author Administrator
*
*/
public class loginDao {
private Session session = null;
private Transaction tran = null;
public loginDao() {
Configuration configure = new Configuration().configure();
SessionFactory factory = configure.buildSessionFactory();
this.session = factory.openSession();
}
public void save(user user) {
try {
tran = this.session.beginTransaction();
this.session.save(user);
tran.commit();
System.out.println("資訊儲存");
} catch (Exception e) {
// TODO: handle exception
} finally {
this.session.close();
}
}
public static void main(String[] args) {
user user = new user();
user.setUserName("使用者名稱稱");
new loginDao().save(user);
}
}
Run as -->java application執行測試類別就可以了;
當控制台輸出所執行的sql語句:
Hibernate: insert into user (USERNAME, USERID) values (?, ?)
資訊儲存
表示資料庫連接成功!
hibernate簡單串連mysql資料庫配置