hibernate 框架 简单小实例,添加用户

1.准备工作myeclipse工具,sqlserver2000驱动的jar包

2.工程架构:建一个web工程名为hiberSec

                  在src文件夹下建两个包:sec.test和second包

                  package second下建User.java

3.User.java代码

package second;

public class User {
	private int uid;
	private String uname;
	private String upass;
	public int getUid() {
		return uid;
	}
	public void setUid(int uid) {
		this.uid = uid;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getUpass() {
		return upass;
	}
	public void setUpass(String upass) {
		this.upass = upass;
	}
	

}

 2.在工程中引入hibernate核心包hibernate.cfg.xml

  在工程名上右键单击选择myeclipse->add hibernate annotations...

  则自动产生hibernate.cfg.xml文件和HibernateSessionFactory.java

 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>
	</session-factory>
</hibernate-configuration>

这时候需要给<session-factory>之间加上属性值

<property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
		<property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=news</property>
		<property name="connection.username">sa</property>
		<property name="connection.password">water</property>
		<property name="show_sql">true</property>
		<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
		<mapping resource="second/User.hbm.xml" />

   3.给实体类User.java需要添加映射文件User.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 package="second">
<class name="User" table="tbl_user">
<id name="uid" type="java.lang.Integer">
<column name="uid"/>
<generator class="native"/>
</id>
<property name="uname" type="java.lang.String">
<column name="uname" not-null="true"/>
</property>
<property name="upass" type="java.lang.String">
<column name="upass" not-null="true"/>
</property>
</class>

</hibernate-mapping>

 4.框架的配置文件和类对象的映射配置文件做完后该测试了

    package sec.test下面创建测试java类Demo1.java

package sec.test;

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

import second.User;

public class Demo1 {
	public static void main(String[] args){
		Configuration cfg=new Configuration().configure();//读取并解析配置文件
		SessionFactory sf=cfg.buildSessionFactory();  //读取并解析映射信息
		Session s=sf.openSession(); //打开session
		Transaction tr=s.beginTransaction(); //打开事务
		User u=new User();
		u.setUname("feiniao");
		u.setUpass("123456");
		s.save(u);  //增加u对象
		tr.commit(); //事务提交
		s.close();   //关闭session
		
		
		
		
		
	}
}

 

 

 

你可能感兴趣的:(Hibernate)