您好,登錄后才能下訂單哦!
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>
<!-- 配置連接數據庫信息 -->
<!-- 加載數據庫的URL -->
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
<!-- 數據庫連接名 -->
<property name="connection.username">root</property>
<!-- 數據庫的連接密碼 -->
<property name="connection.password">123456</property>
<!-- 加載數據庫驅動 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 配置hibernate屬性信息 -->
<!-- 設置數據庫方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 后臺是否顯示SQL語句 -->
<property name="show_sql">true</property>
<!-- 是否格式化SQL語句 -->
<property name="format_sql">true</property>
<!-- 設置數據庫生成策略默認為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對應實體類,table對應數據庫中的表 -->
<class name="com.edu.bean.User"table="user">
<!-- name對應實體類中屬性名,column對應表中字段,type為表中字段類型-->
<id name="id"column="id">
<!-- 設置主鍵生成策略 -->
<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");
//創建SessionFactory工廠
SessionFactorysf=cfg.buildSessionFactory();
//獲取session
Sessionsession=sf.openSession();
//創建事務
Transactiontc=session.beginTransaction();
//創建User對象
Useruser=new User("張三", "123456");
//持久化對象
try{
//保存數據
session.save(user);
//提交事務(不可少)
tc.commit();
}catch(Exception e){
//數據操作失敗,回滾事務
tc.rollback();
}
//關閉session
session.close();
}
}
package com.edu.dbconn;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateGetSession {
//保證SessionFactory工廠只創建一次
private staticSessionFactory sf;
static{
if(sf==null){
//讀取hibernate.cfg.xml主配置文件
Configurationcfg=new Configuration().configure("hibernate.cfg.xml");
//創建SessionFactory工廠
sf=cfg.buildSessionFactory();
}
}
public staticSession getSession(){
//創建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();
//創建事務
Transactiontc=session.beginTransaction();
//創建User對象
Useruser=new User("李四", "123456");
//持久化對象
try{
//保存數據
session.save(user);
//提交事務(不可少)
tc.commit();
}catch(Exception e){
//數據操作失敗,回滾事務
tc.rollback();
}
//關閉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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。