Hibernate小程序关键代码

本文介绍了一个使用Hibernate框架的小程序实现过程,包括配置文件hibernate.cfg.xml的设置、映射文件User.hbm.xml的内容以及Java实体类User.java和客户端类Client.java的编写方式,最后通过ExportDB.java展示了数据库导出的方法。

l Hibernate小程序关键代码

u Hibernate.cfg.xml

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->

<property name="connection.url">jdbc:mysql://localhost/hibernate1</property>

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="connection.username">root</property>

<property name="connection.password">123</property>

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="hibernate.show_sql">true</property>//显示sql语句

<mapping resource="com/proper/user/User.hbm.xml"/>

</session-factory>

</hibernate-configuration>

u User.hbm.xml

<hibernate-mapping>

<class name="com.proper.user.User">

<id name="id" column="user_id">

<generator class="uuid"></generator>

</id>

<property name="name"></property>

<property name="password"></property>

<property name="createtime"></property>

<property name="expiretime"></property>

</class>

</hibernate-mapping>

u User.java

package com.proper.user;

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;

}

}

u Client.java

package com.proper;

import java.util.Date;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

import com.proper.user.User;

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

session.beginTransaction();

User user=new User();

user.setCreatetime(new Date());

user.setExpiretime(new Date());

user.setName("robin");

user.setPassword("123");

session.save(user);

//得到上下文,提交事务

session.getTransaction().commit();

} catch (Exception e) {

// 事务回滚

session.getTransaction().rollback();

}

finally{

if(session!=null)

{

if(session.isOpen())

{

session.close();

}

}

}

}

}

u ExportDB.java

package com.proper;

import org.hibernate.cfg.Configuration;

import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExportDB {

public static void main(String[] args) {

Configuration cfg=new Configuration().configure();

SchemaExport export=new SchemaExport(cfg);

export.create(true, true);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值