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());
}
}