使用hibernate插入数据的例子

数据库创建脚本:

drop table person

create table person(
	id 			varchar(32) 		not null primary key,
	name 		varchar(20)			not null,
	password 	varchar(20) 		not null,
	sex 		varchar(20) 		,
	email		varchar(30)			
)


Person类

package org.lz.hibernate;

public class Person { //POJO类
	private String id;
	private String name;
	private String password;
	private String sex;
	private String email;
	
	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 String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}

}


PersonOperator类:

package org.lz.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class PersonOperator {
	private Session session = null;
	public PersonOperator() {
		Configuration config = new Configuration();
		SessionFactory factory = config.configure().buildSessionFactory();
		this.session = factory.openSession();
	}
	// 所有的操作都是通过session来完成的
	public void PersonInsert(Person p) {
		Transaction tran = this.session.beginTransaction();//开始事物
		this.session.save(p);//执行
		tran.commit();//提交
	}
}

测试方法:

package org.lz.hibernate;

public class TestPO {

	
	public static void main(String[] args) {
		
		Person p = new Person();
		p.setId("1");
		p.setName("刘召");
		p.setPassword("123456");
		p.setSex("男");
		p.setEmail("123@143.com");
		
		//通过实例化的Person对象插入数据
		PersonOperator po = new PersonOperator();
		po.PersonInsert(p);

	}

}

配置文件:

person.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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="org.lz.hibernate.Person" table="PERSON" schema="SCOTT">
        <id name="id" type="java.lang.String">
            <column name="ID" length="32" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" length="20" not-null="true" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="PASSWORD" length="20" not-null="true" />
        </property>
        <property name="sex" type="java.lang.String">
            <column name="SEX" length="20" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="EMAIL" length="30" />
        </property>
    </class>
</hibernate-mapping>

hibernate.cfg.xml:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
	<property name="dialect">
		org.hibernate.dialect.Oracle9Dialect
	</property>
	<property name="connection.url">
		jdbc:oracle:thin:@127.0.0.1:1521:ORCL
	</property>
	<property name="connection.username">scott</property>
	<property name="connection.password">tiger</property>
	<property name="connection.driver_class">
		oracle.jdbc.driver.OracleDriver
	</property>
	<property name="myeclipse.connection.profile">
		OracleDriver
	</property>
	<property name="show_sql">true</property>
	<mapping resource="org/lz/hibernate/Person.hbm.xml" />

</session-factory>

</hibernate-configuration>



阅读更多
个人分类: Java
想对作者说点什么? 我来说一句

Hibernate数据表插入操作

2015年04月13日 493KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭