引言
學習,不是單純的理論,實踐與理論結合纔是最好的學習方式!——小編
今天做了hibernate的第一個小例子……下面來詳細說說。
Demo
1、首先要創建一個java項目
2、導入相關的依賴包
1)HIBERNATE_HOME/lib/*.jar
2)HIBERNATE_HOME/hibernate3.jar
3)加入數據庫驅動(mysql驅動)
3、建立hibernate.cfg.xml文件,完成基本的配置
<!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.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 配置數據庫連接信息 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<!--配置數據庫方言,適配器 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 配置控制檯顯示sql語句 -->
<property name="hibernate.show_sql">true</property>
<!--配置映射文件 -->
<mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、建立User.java實體類
package com.bjpowernode.hibernate;
import java.util.Date;
public class User {
private String id;
private String name;
private String password;
private Date createTime;
private Date expireTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getExpireTime() {
return expireTime;
}
public void setExpireTime(Date expireTime) {
this.expireTime = expireTime;
}
}
5、建立User.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>
<class name="com.bjpowernode.hibernate.User">
<id name="id">
<generator class="uuid"/>
</id>
<property name="name"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>
6、編寫工具類ExoprtDB.java
package com.bjpowernode.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* 將hbm生成ddl
* @author xingyu
*
*/
public class ExoprtDB {
/**
* @param args
*/
public static void main(String[] args) {
//默認讀取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
7、建立客戶端類Client,添加用戶數據到mysql
package com.bjpowernode.hibernate;
import java.util.Date;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;
public class Client {
public static void main(String[] args) {
//讀取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
//建立sessionfactory
SessionFactory factory = cfg.buildSessionFactory();
//取得session
Session session =null;
try{
session = factory.openSession();
//開啓事務
session.beginTransaction();
User user = new User();
user.setName("邢玉");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
//保存User對象(一個實體類的一個對象對應數據庫裏的一條記錄)
session.save(user);
//提交事務
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
//回滾事務
session.getTransaction().rollback();
}finally{
if(session!= null){
if(session.isOpen()){
//關閉session
session.close();
}
}
}
}
}
效果
首先建立一個名爲hibernate_first的數據庫,執行ExportDB.java,生成user表。
然後執行客戶端代碼,將數據添加到mysql中。
問題
數據庫建立好了之後,執行客戶端代碼的時候報錯,錯誤如下:
這一看,就知道是時間格式轉換的問題。
解決
方法1
更換mysql的驅動jar包。之前的版本太低了,換了個高版本的就行了
方法2
進行時間格式轉換。
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//該user實體中的時間格式是util.date格式
user.setCreateTime(formatter.parse( formatter.format(new Date())));
總結
遇到問題,解決問題,一舉拿下hibernate!!