少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

package com.abin.lee.bean;

/**
 * Userbean entity. @author MyEclipse Persistence Tools
 */

public class UserBean implements java.io.Serializable {

 // Fields

 private String id;
 private String username;
 private String password;

 // Constructors

 /** default constructor */
 public UserBean() {
 }

 /** minimal constructor */
 public UserBean(String id) {
  this.id = id;
 }

 /** full constructor */
 public UserBean(String id, String username, String password) {
  this.id = id;
  this.username = username;
  this.password = password;
 }

 // Property accessors

 public String getId() {
  return this.id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public String getUsername() {
  return this.username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getPassword() {
  return this.password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

}





<?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="com.abin.lee.bean.UserBean" table="USERBEAN" schema="ABING">
        <id name="id" type="string">
            <column name="ID" length="80" />
            <generator class="assigned" />
        </id>
        <property name="username" type="string">
            <column name="USERNAME" length="80" />
        </property>
        <property name="password" type="string">
            <column name="PASSWORD" length="80" />
        </property>
    </class>
</hibernate-mapping>






package com.abin.lee.dao;

import java.sql.SQLException;

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

public class UserDao {
 private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
 private static SessionFactory sessionFactory;
 static {
  sessionFactory=new Configuration().configure().buildSessionFactory();
 }
 public static Session GetSession(){
  Session session=(Session)threadLocal.get();
  if(null == session|| !session.isOpen()){
   if(null==sessionFactory){
    sessionFactory=new Configuration().configure().buildSessionFactory();
   }
   session=(sessionFactory!=null)?sessionFactory.openSession():null;
   threadLocal.set(session);
  }
  return session;
 }
 
 public static int insert(String id,String usr,String pwd) throws SQLException{
  UserDao UserDao=new UserDao();
  Session session=UserDao.GetSession();
  Transaction tx=session.beginTransaction();
  SQLQuery sqlQuery = session.createSQLQuery("{call insertObj(?,?,?)}");
  sqlQuery.setParameter(0, id);
  sqlQuery.setParameter(1, usr);
  sqlQuery.setParameter(2, pwd);
  int result=sqlQuery.executeUpdate();
  System.out.println("result="+result);
  return result;
 }
 
}






package com.abin.lee.test;

import java.sql.SQLException;

import junit.framework.TestCase;

import com.abin.lee.dao.UserDao;

public class UserTest extends TestCase{
 public void test() throws SQLException{
  UserDao user=new UserDao();
  int success=user.insert("abin","abing","bing");
  System.out.println("success="+success);
 }
}








<?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.OracleDialect
  </property>
  <property name="connection.url">
   jdbc:oracle:thin:@localhost:1521:xe
  </property>
  <property name="connection.username">abing</property>
  <property name="connection.password">abing</property>
  <property name="connection.driver_class">
   oracle.jdbc.driver.OracleDriver
  </property>
  <property name="myeclipse.connection.profile">
   OracleConnection
  </property>
  <mapping resource="com/abin/lee/bean/UserBean.hbm.xml" />
   
    </session-factory>

</hibernate-configuration>








posted on 2012-09-06 23:21 abin 阅读(2128) 评论(0)  编辑  收藏 所属分类: hibernate

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问