hibernate 学习笔记

第一步 :创建一个,新的项目 在项目的WebContent/WEB-INF/lib目录下加入 hibernate的jar包大概需要以下                jar包:






第二步: 

             在本项目中的src文件夹下创建hibernate.cfg.xml  配置文件。由于我的数据库是mysql  以下为具体的配置信息:
<?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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 链接数据库  后面chengs是数据库的名字 -->
<property name="hibernate.connection.url">jdbc:mysql://192.168.99.100:3306/chengs</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库账号密码 -->
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>

<!-- 根据schema更新数据表的工具 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 显示SQL -->

<property name="hibernate.show_sql">true</property>
<!-- 数据表映射配置文件 -->
<mapping resource="com/model/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>


第三步:
    
    创建实体类 User.java
public class User {

		 String id;
		 String name;
		 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;
		}
		String password;
		public void setCreateTime(Date date) {
			// TODO Auto-generated method stub
			
		}
		public void setExpireTime(Date date) {
			// TODO Auto-generated method stub
			
		}
		

	

}


第四步:
   建立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="hibernate">
<class name="User" table="t_user">

	<id name="id" column="id">
		<generator class="uuid"/>
	</id>
	<property name="name" column="name"/>
	<property name="password"/>
	 
</class>
</hibernate-mapping>


第五步:

    建立testSave 运行测试保存一个对象
package hibernate;



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

public class testSave1 {

	public static void main(String[] args) {
		Session session = null; 
		Transaction tx = null; 
		User user = null;
		
			//创建Configuration对象:对应hibernate的基本配置信息和对象关系映射
		Configuration conf=new Configuration().configure();
		//创建一个SessionFactory;
		
		SessionFactory sessionFactory=conf.buildSessionFactory();
		//创建一个Session对象
		session=sessionFactory.openSession();
		//开启事务
		tx = session.beginTransaction();
				
			//Transient״̬
			user = new User(); 
			user.setName("chengs"); 
			user.setPassword("123");
			
			//保存user
			session.save(user);
			//提交事务
			tx.commit();
		 //关闭session
		 session.close();
		 //关闭sessionFactory对象
		 sessionFactory.close();

	}

}

然后去查看数据库,如果有数据  chengs  和123,就说明hibernate创建成功了

猜你喜欢

转载自776903769.iteye.com/blog/2375024