我,我是春天新手,我想做的是创建一个简单的应用程序,执行以下操作:
1-一个简单的oracle表上的简单crud,名为Dummy,有两个字段:id,numeric和descripcion,一个字符串
2 - 应用程序只有一个servlet。通过调用它,将打印所有虚拟行的列表。
那就是它。问题是在servlet中,我的存储库实例始终为null。
由于
这是我的代码:
PersistenceContext
package jpa_spring.configuracion;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
@EnableJpaRepositories(basePackages = {"jpa_spring.dominio"})
@EnableTransactionManagement
public class PersistenceContext {
@Bean(destroyMethod="close")
DataSource dataSource(Environment env) {
HikariConfig dataSourceConfig = new HikariConfig();
dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver"));
dataSourceConfig.setJdbcUrl(env.getRequiredProperty("db.url"));
dataSourceConfig.setUsername(env.getRequiredProperty("db.username"));
dataSourceConfig.setPassword(env.getRequiredProperty("db.password"));
return new HikariDataSource(dataSourceConfig);
}
@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, Environment env) {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactoryBean.setPackagesToScan("jpa_spring.dominio");
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
jpaProperties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("hibernate.hbm2ddl.auto"));
jpaProperties.put("hibernate.ejb.naming_strategy", env.getRequiredProperty("hibernate.ejb.naming_strategy") );
jpaProperties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql"));
jpaProperties.put("hibernate.format_sql", env.getRequiredProperty("hibernate.format_sql") );
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
@Bean
JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
}
虚拟
package jpa_spring.dominio;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="DUMMY")
public class Dummy {
private Long id;
private String descripcion;
public String getDescripcion() {
return descripcion;
}
public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}
@Id
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
RepositorioDummy
package jpa_spring.dominio;
import org.springframework.data.repository.CrudRepository;
public interface RepositorioDummy extends CrudRepository<Dummy, Long> {}
JPASpringServlet
package jpa_spring.servlet;
import java.io.IOException;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import jpa_spring.dominio.Dummy;
import jpa_spring.dominio.RepositorioDummy;
@Component
public class JPASpringServlet extends HttpServlet {
@Autowired
private RepositorioDummy repositorio;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Iterator<Dummy> it = repositorio.findAll().iterator();
while (it.hasNext()) {
Dummy d = it.next();
System.out.println(d.getDescripcion());
}
}
}
答案 0 :(得分:1)
如果您正在使用Spring MVC(并且很可能是您),Spring已经公开了一个servlet,它将为您处理所有http请求。由于它的作用是分派请求,因此称为DispatcherServlet。您可以在reference documentation中详细了解相关信息。然后,该servlet将使用HandlerMapping
将传入请求映射到应该调用的方法。
您应该创建controller而不是创建servlet。然后,您可以使用@RequestMapping
提取的RequestMappingHandlerMapping
注释,HandlerMapping
的实现。