Hibernate_Eclipse
来源:互联网 发布:软件开发风险管理 编辑:程序博客网 时间:2024/05/16 01:53
一、结构
二、代码
1.Client.java:
package com.example.hibernate;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Client {
public static void main(String[] args) {
//读取配置文件
Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();
Session session = null;
try{
session = factory.openSession();
//开启事务
session.beginTransaction();
Dog user = new Dog();
user.setUsername("hj");
user.setPassword("777");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
session.save(user);
//提交事务
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
//回滚事务
session.getTransaction().rollback();
}finally{
if(session != null){
if(session.isOpen()){
//关闭session
session.close();
}
}
}
}
}
2.Dog.java:
package com.example.hibernate;
import java.util.Date;
public class Dog {
private String id;
private String username;
private String password;
private Date createTime;
private Date expireTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String userName) {
this.username = userName;
}
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;
}
}
3.ExportDB.java
package com.example.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDB {
public static void main(String[] args) {
//默认读取hibernate.cfg.xml文件
Configuration cfr = new Configuration().configure();
SchemaExport export = new SchemaExport(cfr);
export.create(true, true);
}
}
4.Dog.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.example.hibernate.Dog">
<id name="id">
<generator class="uuid"/>
</id>
<property name="username"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>
<!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.example.hibernate.Dog">
<id name="id">
<generator class="uuid"/>
</id>
<property name="username"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>
5.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 >
<!-- mysql数据库驱动 -->
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!-- mysql数据库名称 -->
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property>
<!-- 数据库的登陆用户名 -->
<property name="hibernate.connection.username">sys as sysdba</property>
<!-- 数据库的登陆密码 -->
<property name="hibernate.connection.password">111111</property>
<!-- 方言:为每一种数据库提供适配器,方便转换 -->
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<mapping resource="com/example/hibernate/Dog.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6.log4j.properties:
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=firestorm.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.logger.com.codefutures=DEBUG
0 0
- Hibernate_Eclipse
- 深入探索spring技术内幕(一): spring概述
- sizeof和strlen
- Linux入门--系统命令
- R 语言生成eps,latex文件
- 欢迎使用CSDN-markdown编辑器
- Hibernate_Eclipse
- cf375b_模拟_每个状态结束时,要及时清零标识变量和累积变量
- C语言中省略号使用
- 深入探索spring技术内幕(二): 剖析spring管理Bean的原理与配置
- 神马聊天——一个基与mina2.0仿qq的聊天小系统(效果展示)(javase)
- CDOJ 卿学姐与公主 【分块 入门题】
- thinkPHP 连接数据库
- DOM加强特效篇之关机动画
- Visual C++ 之 MFC异常宏和异常类