码迷,mamicode.com
首页 > 系统相关 > 详细

hibernate入门

时间:2014-07-20 22:02:07      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:blog   http   java   os   文件   数据   

新建一 java 项目

<!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">com.mysql.jdbc.Driver</property>
		<!-- mysql数据库名称 -->
		<property name="hibernate.connection.url">jdbc:mysql://10.170.0.47:3306/test</property>
		<!-- 数据库的登陆用户名 -->
		<property name="hibernate.connection.username">root</property>
		<!-- 数据库的登陆密码 -->
		<property name="hibernate.connection.password">root</property>
		<!-- 方言:为每一种数据库提供适配器,方便转换 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
		<mapping resource="com/example/hibernate/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

  配之为上

public class User {
private String id;
private String username;
private String password;
private Date createTime;
private Date expireTime;

这是 user 类 

<?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.User">
		<id name="id">
			<generator class="uuid"/>
		</id>
		<property name="username"/>
		<property name="password"/>
		<property name="createTime"/>
		<property name="expireTime"/>
	</class>
</hibernate-mapping>

  配置 User.hbm.xml

package com.example.hibernate;

import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class ExportDB {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	        //默认读取hibernate.cfg.xml文件  
	        Configuration cfr = new Configuration().configure();  
	        SchemaExport export = new SchemaExport(cfr);  
	        export.create(true, true); 

	}

}

  生成表 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();
			
			User user = new User();
			user.setUsername("用户名");
			user.setPassword("123");
			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();
				}
			}
		}
	}
}

  测试 main cls

hibernate入门,布布扣,bubuko.com

hibernate入门

标签:blog   http   java   os   文件   数据   

原文地址:http://www.cnblogs.com/zhengfengshaw/p/3856590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!