我是Web开发的新手。我正在尝试使用spring-mvc,jpa / hibrenate,oracle db和tomcat v7.0服务器创建非常简单的应用程序。应用程序只会提取一些数据并显示在页面上。问题是当我尝试提取数据时,页面上没有结果。我不知道我在做什么错。
实体类。
@Entity
@Table(name="Product")
public class Product {
@Id
@Column(name="productId")
private String productId;
@Column(name="name")
private String name;
@Column(name="unitPrice")
private int unitPrice;
@Column(name="description")
private String description;
@Column(name="manufacturer")
private String manufacturer;
@Column(name="category")
private String category;
@Column(name="unitsInStock")
private int unitsInStock;
@Column(name="unitsInOrder")
private long unitsInOrder;
@Column(name="condition")
private String condition;
//constructors getters and setters
.xml文件与配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.11.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.packt.webstore" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
<property name="URL" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="user" value="crossing" />
<property name="password" value="123" />
</bean>
<bean id="hibernateJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="persistence-webstore" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="persistence-webstore"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.dialect"value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
<property name="hibernate.connection.username" value="crossing" />
<property name="hibernate.connection.password" value="123" />
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
<property name="hibernate.connection.charSet" value="UTF-8" />
</properties>
</persistence-unit>
</persistence>
服务等级
@Service
public class ProductService {
@PersistenceContext
private EntityManager em;
@Transactional
public List<Product> getAll() {
List<Product> result = new ArrayList<Product>();
result = em.createQuery("SELECT e FROM Product e", Product.class)
.getResultList();
return result;
}
}
我的.jsp文件
//....
<c:forEach items="${products}" var="product">
<div class="col-sm-6 col-md-3" style="padding-bottom: 15px">
<div class="thumbnail">
<div class="caption">
<h3>${product.name}</h3>
<p>${product.description}</p>
<p>${product.unitPrice}PLN</p>
<p>Units in stock: ${product.unitsInStock}</p>
</div>
</div>
</div>
</c:forEach>
//..
和控制器
@Controller
public class ProductController {
@Autowired
public ProductService proService;
@RequestMapping("/")
public String list(Model model) {
model.addAttribute("products", proService.findAll());
return "products";
}
}
**编辑**
我做了一些改变
添加了产品存储库
public interface ProductRepository extends Repository<Product, String>{
public List<Product> findAll();
}
更改服务等级
@Service
public class ProductService implements ProductRepository{
@PersistenceContext
private EntityManager em;
@Override
@Transactional
public List<Product> findAll() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("persistence-webstore");
em = factory.createEntityManager();
List<Product> listPersons = em.createQuery("SELECT p FROM Product p",Product.class).getResultList();
if (listPersons.isEmpty()) {
System.out.println("List contains no data");
}
return listPersons;
}
这次没有NPE或任何其他错误,只是我仍然无法将这些元素列出来。我不知道,查询或配置中肯定有一些东西。
编辑
Hibernate的show_sql结果在下面,但是为什么这样看?这应该是吗?
Hibernate:
select
product0_.productId as productId1_0_,
product0_.category as category2_0_,
product0_.condition as condition3_0_,
product0_.description as description4_0_,
product0_.manufacturer as manufacturer5_0_,
product0_.name as name6_0_,
product0_.unitPrice as unitPrice7_0_,
product0_.unitsInOrder as unitsInOrder8_0_,
product0_.unitsInStock as unitsInStock9_0_
from
Product product0_
**编辑**
终于可以了。令我惊讶的是,一个简单的更新Maven项目提供了帮助。
最佳答案
拉维乌斯
我认为您需要添加存储库类。
资料库
public interface DocumentRepository extends PagingAndSortingRepository<Document, Integer> {
}
服务-> daoImpl
@Override
public List<Document> findAllDocument() {
return documentRepository.findAll();
}
.xml
<jpa:repositories base-package="com.test.faas.repositoryex"
entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="dataSource" ref="dataSource" />
<property name="jpaProperties">
<props>
<!-- <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.default_schema">test</prop> -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.connection.pool_size">4</prop>
<prop key="hibernate.connection.shutdown">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">create</prop> <prop key="hibernate.ddl_auto">auto</prop> -->
</props>
</property>
<!-- entity define class package -->
<property name="packagesToScan" value="com.text.faas.dtoex" />
</bean>
你想工作。
如果不行。
我需要您错误信息。
关于java - Spring MVC/JPA。从数据库中提取数据后无结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37142738/