SSH2搭建篇02——hibernate

看了上一篇SSH2搭建篇01——strusts2          之后继续搭建hibernate(感觉相对比较简单点)

首先将hibernate所需的包引入(MyEclipe加下hibernate框架把jar拿出来再自己手动加上,或者下载.)

1:创建hibernate.cfg.xml   (放在src根目录下)

<?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.     
  集成struts2   hibernate  时用到,集成spring时就可以不用了,使用spring 配置更方便      
-->
<hibernate-configuration>

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

2:写一个javabean测试 user.java

package com.yeshun.bean;


public class User{

	private String userid;
	private String username;
	private String password;
	
	public String getUsername() {
		return username;
	}
	public String getUserid() {
		return userid;
	}
	public void setUserid(String userid) {
		this.userid = userid;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

3: 配置上user对应hibernate文件  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>
    <class name="com.yeshun.bean.User"
    	table="user_table">
    	<id name="userid" type="string">
    		<column name="user_id" length="32"/>
            <generator class="sequence">
            <param name="sequence">USERSEQ</param>
            </generator>
    	</id>

    	<property name="username" type="string">
    		<column name="user_name" length="30" not-null="false" />
    	</property>
    	
    	<property name="password" type="string">
    		<column name="password" length="40" not-null="false"/>
    	</property>
    	
    </class>
</hibernate-mapping>

这样就可以了,然后写一个测试类看看结果

package com.yeshun.util;




import java.util.Date;
import java.util.List;

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

import com.yeshun.bean.User;

public class HibernateTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Configuration cfg = new Configuration().configure();
		SessionFactory factory = cfg.buildSessionFactory();
		session.beginTransaction();
        User user = new User();
        user.setUsername("admin");
        user.setPassword("123456");
        session.save(user);
        session.getTransaction().commit();
        
        String hql = "from User where 1 = 1";  
        Query query = session.createQuery(hql);  
        List<User> list = (List) query.list();
        for (User u : list) {
            System.out.println(u.getUsername()+"--"+u.getPassword());
        }
		
	}

}

那么hibernate 基本配置成功!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值