gpt4 book ai didi

java - 如何从 spring-dispatcher-servlet.xml 访问 SessionFactory 到 DAO

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:42 24 4
gpt4 key购买 nike

我正在 Hibernate 的帮助下创建一个 Spring Web 应用程序。我已经为所有配置创建了 spring-dipatcher-servlet.xml 。我想使用 hibernate 访问数据库而不创建 hibernate.cfg.xml 文件,因为我使用的是 spring。我在 DAO 中访问 session 工厂时遇到空指针异常。

以下是我到目前为止所做的事情的片段。

----web.xml----

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>quiz_mcq</display-name>
<welcome-file-list>
<welcome-file>welcome.htm</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

---spring-dispatcher-servlet---

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:component-scan base-package="com.quiz_mcq.controller" />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/quiz_mcq" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan" value="com.quiz_mcq.bean"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>

</bean>



<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>


</beans>

--- Controller ---

@Controller
public class QuizMcqController {

UserService userService;

@RequestMapping(value="/welcome.htm")
public ModelAndView redirectToLoginPage(){

ModelAndView modelAndView = new ModelAndView("login");
return modelAndView;

}

@RequestMapping(value="/AuthenticateUser.htm", method = RequestMethod.POST)
public ModelAndView authenticateUser(@RequestParam("username") String username, @RequestParam("password")String password){

userService = new UserService();
boolean flag = userService.authenticate(username,password);
if(flag){
ModelAndView modelAndView = new ModelAndView("login");
return modelAndView;
}
else{
ModelAndView modelAndView = new ModelAndView("wrong");
return modelAndView;
}


}


}

--- 服务 ---

public class UserService {

User user;
UserDao dao;

public boolean authenticate(String username, String password) {
user = new User();
user.setUsername(username);
user.setPassword(password.toCharArray());

if(dao.authenticateUser(user))
{
return true;
}

return false;

}
}

---DAO ---

@Repository
public class UserDao implements IUser {

@Autowired
SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

@Override
public boolean authenticateUser(User user) {
String username = user.getUsername();
char[] password = user.getPassword();
System.out.println(username +" <----> "+password);
String hql = "from User where username='username' and password ='password'";
Query query = getSessionFactory().openSession().createQuery(hql);
List list = new ArrayList();
list = query.list();
if (list.size() > 0 && list != null) {
return true;
}

return false;
}

}

我该如何解决我的问题,我做错了什么?

提前致谢。感谢您的帮助。

最佳答案

这里的问题是当前您的调度程序 servlet 不知道如何创建和设置 bean SessionFactory。 (如您所见,您的 setSessionFactory 位于 UserDao 类中)

您可能需要在 spring-dispatcher-servlet.xml 中声明 UserDao bean。

例如:

<bean id="userDAO" class="your.package.nameforUserDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

关于java - 如何从 spring-dispatcher-servlet.xml 访问 SessionFactory 到 DAO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41415230/

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