现在的位置: 首页 > 综合 > 正文

【Hibernate】helloworld项目建立

2013年12月07日 ⁄ 综合 ⁄ 共 3966字 ⁄ 字号 评论关闭

目的:本文只是记录项目添加hibernate的过程,以便于随时的使用

前提:为了运行方便我的这个项目用的数据库是hsqldb,开发工具eclipse

目标:用hibernate建立用户表(userTable),并向表中插入一条用户信息

下载

hibernate ---是一种“数据库-对象”映射的解决方案,就是你只要写一句SQL语句,它就自动把SQL语句的结果封装成对象
        版本:hibernate-release-4.0.1.Final       

        官网地址:​​http://www.hibernate.org/
       下载地址:
​​http://www.hibernate.org/downloads

建立项目

1、建立java project,命名为HibernateTest

2、 添加的jar文件是解压后hibernate-release-4.0.1.Final\lib\required目录下的jar文件

3、添加hsqldb.jar

4、src下建立hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
 
<!DOCTYPE hibernate-configuration 
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
        <!-- 是否将运行期生成的SQL输出到日志以供调试 --> 
        <property name="show_sql">true</property> 
        <!-- SQL方言,这里设定的是HSQL --> 
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
        <!-- JDBC驱动程序 --> 
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> 
        <!-- JDBC URL, "?useUnicode=true&characterEncoding=GBK" 表示使用GBK进行编码 --> 
        <property name="connection.url"> jdbc:hsqldb:file:./db/RWQTable
        </property> 
        <!-- 设置数据库中的表用hibernate自动建立 -->
   		<property name="hibernate.hbm2ddl.auto">create</property>
        <!-- 数据库用户名 --> 
        <property name="connection.username">sa</property> 
        <!-- 数据库密码 --> 
        <property name="connection.password"></property> 

        <!-- 指定User的映射文件   -->
        <mapping resource="com/rwq/test/UserTable.hbm.xml"/>         
    </session-factory> 
</hibernate-configuration> 

5、建立UserInfor实体

package com.rwq.test;

public class UserInfor {
	private long id;
	private String name;
	private boolean sex;
	private int age;
	private String address;
	private String pass;
	
	public UserInfor(){
		
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public boolean isSex() {
		return sex;
	}
	public void setSex(boolean sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
}

6、包com.rwq.test下建立UserInfor对应的建表文件UserTable.hbm.xml

<?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>
	<!-- 如果上面写成 hibernate-mapping package="com.rwq.test" 这下面的class中name直接写类名即可 -->
	<!-- class的name属性就是要映射的对象类,table数据库对应的表名,id用于唯一标识该 对象, name属性为类里的属性名;column是数据库里的对应字段。generator子元素用于指定主键生成方式 -->
	<class name="com.rwq.test.UserInfor" table="UserTable"><!-- 如果表名跟类名一个样的话,那么table属性可以不写 -->
		<id name="id" column="id">
			<!-- 定义主键生成策略 -->
			<generator class="increment"></generator>
		</id>
		<!-- 映射用户名属性 -->
		<property name="name" column="name" length="100" not-null="true"></property>
		<!-- 映射性别属性 -->
		<property name="sex" column="sex" not-null="true"></property>
		<!-- 映射用户年龄属性 -->
		<property name="age" column="age" not-null="true"></property>
		<!-- 映射用户地址属性 -->
		<property name="address" column="address"></property>
		<!-- 映射用户密码属性 -->
		<property name="pass" column="pass" not-null="true"></property>
	</class>
</hibernate-mapping>

7、建立service文件进行测试

package com.rwq.test;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class LoginService {

	public static void main(String[] args) {
		UserInfor user = new UserInfor();
		user.setId(1l);
		user.setName("admin");
		user.setAge(12);
		user.setAddress("xinjanlu");
		user.setPass("123456");
		user.setSex(false);
		try {
			Configuration configuration = new Configuration();
			configuration.configure();
			ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
					.applySettings(configuration.getProperties())
					.buildServiceRegistry();
			SessionFactory sf = configuration
					.buildSessionFactory(serviceRegistry);
			Session session = sf.openSession();
			session.beginTransaction();
			session.save(user);
			session.getTransaction().commit();
			session.close();
			sf.close();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
	}

}

8、成功即可

抱歉!评论已关闭.