gpt4 book ai didi

java - 通过hibernate从数据库读取数据

转载 作者:行者123 更新时间:2023-12-01 22:47:31 24 4
gpt4 key购买 nike

我在问一个概念性问题。我只想从数据库中读取数据。为此,我在 hibernate 和 spring 中实现了数据库连接。

我的applicationContext.xml是这样指定的:

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

<context:annotation-config />

<context:component-scan base-package="com.TestProj"
annotation-config="true" />

<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />

<bean id="mainGUI" class="com.TestProj.gui.MainWindow" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url"
value="jdbc:oracle:thin:@test:1234:test" />
<property name="username" value="test" />
<property name="password" value="testdb" />
<property name="maxActive" value="20" />
</bean>

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="jpaData" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="entityManager"
class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

</beans>

如您所见,我想使用实体管理器从数据库中读取数据。但是,为此,我需要定义我在域中连接的整个数据库的完整表结构。不过,我只需要对数据库进行sql查询。如何使用 hibernate 来完成此操作?

我很欣赏你的“概念性”回答!

最佳答案

  1. 您不需要定义整个数据库的结构,只需定义您需要读取的表
  2. 如果您只需要对数据库进行 sql 查询,那么最好使用更轻量级的东西,例如 SpringJDBC 甚至原始 JDBC

关于java - 通过hibernate从数据库读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25119045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com