Hibernate SQLQuery 返回List<Bean>结果集

本文介绍如何在Hibernate中使用原生SQL进行查询,并展示了如何映射查询结果到自定义Java对象,即使这些对象与数据库表结构不完全匹配。

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

Hibernate hql查询语句:

list=getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql).list();
Hibernate sql查询语句:

getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(sql).list();

Hibernate框架中hql查询简化了许多写sql语句的方法,但在实际应用中仍然有许多不方便的地方,用的最多的仍然是sql语句查询,可能查询出来的结果集并不是和数据库表的数据一一对应,在不想使用一对一、多对一、一对多等关系修改Hibernate配置文件的时候,仍然可以查询出我们需要的结果,代码如下:

List<Operationmodule> list=getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(sql)
		.addScalar("subMenu", Hibernate.INTEGER)
		.addScalar("orerationName", Hibernate.STRING)
		.addScalar("isChecked", Hibernate.INTEGER)
		.addScalar("operationNo", Hibernate.STRING)
		.addScalar("operationmoduleId", Hibernate.STRING)
		.addScalar("nodeoprationid", Hibernate.STRING)
		.addScalar("ismenu", Hibernate.STRING)
		.setResultTransformer(Transformers.aliasToBean(Operationmodule.class)).list();
其中的变量sql为

select sub_menu as subMenu,oreration_name as orerationName,isChecked,<pre name="code" class="java">operationNo,operationmoduleId,nodeoprationid,ismenu
from operation_module where 1=1 and  ...


需要注意的是:

1. sql语句中返回的值的列名(subMenu,orerationName,isChecked,operationNo,operationmoduleId,nodeoprationid,ismenu)应该和类 Operationmodule的属性一致

2. addScalar("orerationName", Hibernate.STRING) ,这个方法 中的第二个参数用来将查询出来的数据类型转换成类 Operationmodule属性的类型

3. 如果Operationmodule类中有10个属性,上面的代码中只有7个 addScalar()方法,查出来的结果只有上面的7个属性有值,其他3个将没有值

4. Operationmodule.java 和Operationmodule.hbm.xml文件不一定要一致,xml文件只需要和数据库中的字段对应即可,而java文件可以手动添加需要的字段


<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd " > <context:property-placeholder location="classpath:application.properties"></context:property-placeholder> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="com.aiearth.drone.dao,com.aiearth.pojo" /> <property name="hibernateProperties"> <props> <!-- <prop key="hibernate.hbm2ddl.auto"> update </prop> --> <prop key="hibernate.dialect"> <!-- org.hibernate.spatial.dialect.postgis.PostgisDialect--> org.hibernate.spatial.dialect.postgis.PostgisPG10Dialect <!-- org.hibernate.dialect.PostgreSQLDialect--> </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.use_sql_comments">false</prop> <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> <prop key="hibernate.query.escape_like_parameter">false</prop> <prop key="hibernate.query.sql.escape-identifier">false</prop> <prop key="hibernate.logger.org.hibernate.mapping">DEBUG</prop> </props> </property> </bean> <bean id="dataSource" class="com.aiearth.drone.security.DataSource.UmspscDataSource"> <property name="driverClassName" value="org.postgresql.Driver"/> <property name="jdbcUrl" value="${drone.jdbc.url}?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&stringtype=unspecified"/> <property name="username" value="${drone.jdbc.username}"/> <property name="password" value="${drone.jdbc.password}"/> <property name="connectionTimeout" value="30000"/> <property name="idleTimeout" value="180000"/> <property name="maximumPoolSize" value="30"/> <property name="minimumIdle" value="10"/> <property name="maxLifetime" value="1200000"/> <property name="connectionTestQuery" value="SELECT 1"/> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" primary="true"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" propagation="REQUIRED" /> <tx:method name="count*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" /> </tx:attributes> </tx:advice> <aop:config expose-proxy="true"> <!-- 只对业务逻辑层实施事务 --> <aop:pointcut id="txPointcut" expression="execution(* com.aiearth..*.service..*Service.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" /> </aop:config> </beans>这样配对吗,如果不对请给我完整的修改后配置
最新发布
07-31
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值