您好,登錄后才能下訂單哦!
http://pan.baidu.com/s/1eRQ19C2
<?xml version='1.0'encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generatedby MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- 配置連接數(shù)據(jù)庫信息 -->
<!-- 加載數(shù)據(jù)庫的URL -->
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
<!-- 數(shù)據(jù)庫連接名 -->
<property name="connection.username">root</property>
<!-- 數(shù)據(jù)庫的連接密碼 -->
<property name="connection.password">123456</property>
<!-- 加載數(shù)據(jù)庫驅(qū)動(dòng) -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 配置hibernate屬性信息 -->
<!-- 設(shè)置數(shù)據(jù)庫方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 后臺(tái)是否顯示SQL語句 -->
<property name="show_sql">true</property>
<!-- 是否格式化SQL語句 -->
<property name="format_sql">true</property>
<!-- 設(shè)置數(shù)據(jù)庫生成策略默認(rèn)為update-->
<property name="hbm2ddl.auto">update</property>
<!-- 加載hibernate映射文件 -->
<mapping resource="com/edu/bean/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
eg:
package com.edu.bean;
public class User {
private intid;
private String username;
private String password;
public intgetId() {
return id;
}
public voidsetId(int id) {
this.id =id;
}
public String getUsername() {
return username;
}
public voidsetUsername(String username) {
this.username= username;
}
public String getPassword() {
return password;
}
public voidsetPassword(String password) {
this.password= password;
}
public User(Stringusername, String password) {
super();
this.username= username;
this.password= password;
}
public User() {
// TODO Auto-generated constructor stub
}
}
eg:
<?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>
<!-- name對應(yīng)實(shí)體類,table對應(yīng)數(shù)據(jù)庫中的表 -->
<class name="com.edu.bean.User"table="user">
<!-- name對應(yīng)實(shí)體類中屬性名,column對應(yīng)表中字段,type為表中字段類型-->
<id name="id"column="id">
<!-- 設(shè)置主鍵生成策略 -->
<generator class="identity"></generator>
</id>
<property name="username"type="string"column="username"></property>
<property name="password"type="string"column="password"></property>
</class>
</hibernate-mapping>
<!-- 加載hibernate映射文件 -->
<mapping resource="com/edu/bean/User.hbm.xml"/>
eg:
package com.edu.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.edu.bean.User;
public class HibernateTest {
@Test
public voidtest(){
//讀取hibernate.cfg.xml主配置文件
Configurationcfg=new Configuration().configure("hibernate.cfg.xml");
//創(chuàng)建SessionFactory工廠
SessionFactorysf=cfg.buildSessionFactory();
//獲取session
Sessionsession=sf.openSession();
//創(chuàng)建事務(wù)
Transactiontc=session.beginTransaction();
//創(chuàng)建User對象
Useruser=new User("張三", "123456");
//持久化對象
try{
//保存數(shù)據(jù)
session.save(user);
//提交事務(wù)(不可少)
tc.commit();
}catch(Exception e){
//數(shù)據(jù)操作失敗,回滾事務(wù)
tc.rollback();
}
//關(guān)閉session
session.close();
}
}
package com.edu.dbconn;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateGetSession {
//保證SessionFactory工廠只創(chuàng)建一次
private staticSessionFactory sf;
static{
if(sf==null){
//讀取hibernate.cfg.xml主配置文件
Configurationcfg=new Configuration().configure("hibernate.cfg.xml");
//創(chuàng)建SessionFactory工廠
sf=cfg.buildSessionFactory();
}
}
public staticSession getSession(){
//創(chuàng)建session
Sessionsession=sf.openSession();
return session;
}
}
package com.edu.dao;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.edu.bean.User;
import com.edu.dbconn.HibernateGetSession;
public class UserDao {
public voidsaveUser(){
//獲取session
Sessionsession=HibernateGetSession.getSession();
//創(chuàng)建事務(wù)
Transactiontc=session.beginTransaction();
//創(chuàng)建User對象
Useruser=new User("李四", "123456");
//持久化對象
try{
//保存數(shù)據(jù)
session.save(user);
//提交事務(wù)(不可少)
tc.commit();
}catch(Exception e){
//數(shù)據(jù)操作失敗,回滾事務(wù)
tc.rollback();
}
//關(guān)閉session
session.close();
}
}
package com.edu.test;
import org.junit.Test;
import com.edu.dao.UserDao;
public class HibernateTest {
@Test
public voidtest(){
UserDaoud=new UserDao();
ud.saveUser();
}
}
http://pan.baidu.com/s/1eRPLFJO
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。