spring_hibernate_in one and find more than 1 rs

本文通过一个具体的示例展示了如何使用Spring框架整合Hibernate,包括配置数据源、Session工厂及Hibernate模板,并提供了查询操作的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.test.go;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class Test
{
	public static void main(String[] args)
	{
		ApplicationContext ctx = new FileSystemXmlApplicationContext(
				"src/applicationContext.xml");
		HibernateTemplate hibernateTemplate = (HibernateTemplate) ctx
				.getBean("hibernateTemplate");
		TestRS t1 = (TestRS)hibernateTemplate.find("select new " + TestRS.PATH+ "(t3.VAge3,t3.VId3) from Test1 t1,Test2 t2,Test3 t3 where t1.test2.VId2 = t2.VId2 and t2.test3.VId3 = t3.VId3").get(0);
		System.err.println(t1.getInt1() + " <>>> " + t1.getInt2());

	}
}

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<!-- 配置数据源 -->
	<bean id="SQLCOOL"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
		</property>
		<property name="url"
			value="jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=test">
		</property>
		<property name="username" value="sa"></property>
		<property name="password" value="sa123"></property>
	</bean>

	<!-- 配置session工厂 -->
	<bean id="CoolSessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="SQLCOOL" />
		</property>
		<property name="mappingResources">  
			<list>  
			<value>com/test/Test3.hbm.xml</value>  
			<value>com/test/Test2.hbm.xml</value>  
			<value>com/test/Test1.hbm.xml</value>  
			</list>  
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.SQLServerDialect
				</prop>
			</props>
		</property>
	</bean>

	<!-- 配置Hibernate模板类 -->
	<bean id="hibernateTemplate"
		class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref bean="CoolSessionFactory" />
		</property>
		<property name="allowCreate">
			<value>true</value>
		</property>
	</bean>

	<bean id="Test1DAO" class="com.test.Test1DAO">
		<property name="sessionFactory">
			<ref bean="CoolSessionFactory" />
		</property>
	</bean>
	<bean id="Test2DAO" class="com.test.Test2DAO">
		<property name="sessionFactory">
			<ref bean="CoolSessionFactory" />
		</property>
	</bean>
	<bean id="Test3DAO" class="com.test.Test3DAO">
		<property name="sessionFactory">
			<ref bean="CoolSessionFactory" />
		</property>
	</bean></beans>

 

package com.test.go;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.test.Test1;

public class Test
{
	public static void main(String[] args)
	{
		ApplicationContext ctx = new FileSystemXmlApplicationContext(
				"src/applicationContext.xml");
		HibernateTemplate hibernateTemplate = (HibernateTemplate) ctx
				.getBean("hibernateTemplate");
		Test1 t1 = (Test1)hibernateTemplate.findByNamedParam("select t1 from Test1 t1 where t1.VId1 =:testId or t1.VAge1 =:testName",new String[]{"testId","testName"},new Object[]{62,78}).get(0);
		System.err.println(t1.getVId1() + " <>>> " + t1.getVAge1());
		
		String hql = "select new " + TestRS.PATH+ "(t3.VAge3,t3.VId3) from Test1 t1,Test2 t2,Test3 t3 where t1.test2.VId2 = t2.VId2 and t2.test3.VId3 = t3.VId3 and t1.VId1 =:testId or t1.VAge1 =:testName";
		TestRS rs = (TestRS)hibernateTemplate.findByNamedParam(hql, new String[]{"testId","testName"},new Object[]{62,78}).get(0);
		System.err.println(rs.getInt1() + " <>>> " + rs.getInt2());   

	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值