对Hibernate配置文件的加载

package com.hz.ssh;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Test {
  static String page = "1";
  static String pageSize = "5";
	public static void main(String[] args) {
		//只进行Hibernate测试
//		Configuration conf = new Configuration().configure();
//		SessionFactory sf = conf.buildSessionFactory();
//	    Session s = sf.openSession();
//	    
//	    s.beginTransaction();
//	    Student stu = new Student();
//	    stu.setStu_id(8);
//	    stu.setStu_name("111");
//	    stu.setStu_sex("boy");
	    //s.save(stu);  增
	    //s.delete(stu);  删除
	   //s.update(stu);  改
	    //查询

 

<?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="com.hz.ssh">
		  
		  <class name="Student" dynamic-insert="true" table="STUDENT">
		     <id name="stu_id">
		         <column name="STU_ID"></column>
		     </id>		  
		    
		     <property name="stu_name">
		        <column name="STU_NAME"></column>
		     </property>
		     
		     <property name="stu_sex">
		        <column name="STU_SEX"></column>
		     </property>
		     
		     
		  
		  </class>
		  	  
		</hibernate-mapping>

 

 ApplicationContext ac = new FileSystemXmlApplicationContext("src/applicationContext.xml");
	    StudentDAO sd = (StudentDAO)ac.getBean("studentDAO");
	    int i = sd.queryDataAccount();
	    System.out.println(i);
//        List<Student> list = sd.queryByPage(Integer.parseInt(page),Integer.parseInt(pageSize) );
//        for(Student stu:list){
//        	System.out.println(stu.getStu_id() + stu.getStu_name() + stu.getStu_sex());
//        }
//	    Student stu = new Student();
//	    stu.setStu_id(11);
//	    stu.setStu_name("56987");
//	    stu.setStu_sex("boy");
//	    sd.save(stu);
//	    sd.delete(stu);
//	    stu = sd.queryById2(2);
//
//        	System.out.println(stu.getStu_id() + stu.getStu_name() + stu.getStu_sex());
  
	   
	}
}

 

<?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="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
		        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property>
		        <property name="connection.username">scott</property>
		        <property name="connection.password">tiger</property>
		        <property name="dialect">org.hibernate.dialect.OracleDialect</property>
        
                <!-- 关联映射文件 -->
 
                <mapping resource="com/hz/ssh/student.hbm.xml"/> 
            
            </session-factory>       
         </hibernate-configuration>  

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hibernate.cfg.xml</value>  <!-- 连接的Hibernate的名字 -->
		</property>
	</bean>
	
	<bean id="studentDAO" class="com.hz.ssh.StudentDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	</bean>
	
</beans>

 

package com.hz.ssh;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Order;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class StudentDAO extends HibernateDaoSupport implements IStudentDAO {
	
	public static int account = 0;
    public static int lastpage = 0;
	
	//入库操作
	public void save(Student stu){
	   	Configuration conf = new Configuration().configure();
	   	SessionFactory sf = conf.buildSessionFactory();
	   	Session s = sf.openSession();
	   	s.beginTransaction();
	   	s.save(stu);
	   	s.getTransaction().commit();
	   	s.close();
	}
	
	//删除操作
	public void delete(Student stu){
	   	Configuration conf = new Configuration().configure();
	   	SessionFactory sf = conf.buildSessionFactory();
	   	Session s = sf.openSession();
	   	s.beginTransaction();
	   	s.delete(stu);
	   	s.getTransaction().commit();
	   	s.close();
	}
	
	//更新操作
	public void update(Student stu){
		Configuration conf = new Configuration().configure();
	   	SessionFactory sf = conf.buildSessionFactory();
	   	Session s = sf.openSession();
	   	s.beginTransaction();
	   	s.update(stu);
	   	s.getTransaction().commit();
	   	s.close();
	}
	
	//查询最后一一页数据!
	public int queryDataAccount(){
		List<Student> list = null;
	  	Configuration conf = new Configuration().configure();
	   	SessionFactory sf = conf.buildSessionFactory();
	   	Session s = sf.openSession();
	   	s.beginTransaction();
	   	String hql = "from Student order by stu_id";   //查询语句  HQL
	   	list = s.createQuery(hql).list();
	   	account = list.size();
	   	lastpage= (int)Math.ceil((double)account/5);
	   	System.out.println(lastpage);
		return lastpage;
	}
	
	//根据ID查询
	public List<Student> queryById(int id){
		List<Student> list = null;
		Session s = this.getHibernateTemplate().getSessionFactory().openSession();
		String hql = "from Student where stu_id = :stu_id";
		Query query = s.createQuery(hql);
		list = query.setInteger("stu_id", id).list();
		return list;
	}	
	
	//查询两个数之间的数据
	public List<Student> queryByTT(int id1, int id2){
		List<Student> list = null ;
		Session s = this.getHibernateTemplate().getSessionFactory().openSession();
		String hql = "from Student as s where stu_id between :stu_id1 and :stu_id2 ";
		Query query = s.createQuery(hql);
		query.setInteger("stu_id1", id1);
		query.setInteger("stu_id2", id2);
		list = query.list();
		return list;
	}
	
	//数据分页
	public List<Student> queryByPage(int page,int pagesize){
		List<Student> list = null ;
		Session s = this.getHibernateTemplate().getSessionFactory().openSession();
		list = s.createCriteria(Student.class).setFirstResult(pagesize*(page-1))
		       .setMaxResults(pagesize).addOrder(Order.asc("stu_id")).list();
		
		return list;
	}
}

 

你可能感兴趣的:(spring,oracle,bean,Hibernate,ssh)