当前位置: 代码迷 >> Java Web开发 >> Spring Hibernate整合有关问题,求大神帮忙~
  详细解决方案

Spring Hibernate整合有关问题,求大神帮忙~

热度:113   发布时间:2016-04-14 19:01:41.0
Spring Hibernate整合问题,求大神帮忙~~~~~~~
SSH整合,全部采用注解配置。

Struts已经整合好了,将Hibernate整合到Spring时,出现了空指针
2015-05-07 10:10:58,495 DEBUG (com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor:84) - Error calling method through OGNL: object: [com.gmc.action.user.LoginAction@433a316a] method: [execute] args: [[]]
java.lang.NullPointerException
at com.gmc.dao.BaseDaoHibernate4.get(BaseDaoHibernate4.java:27)
at com.gmc.service.UserServiceImpl.checkUser(UserServiceImpl.java:37)
at com.gmc.action.user.LoginAction.execute(LoginAction.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:870)
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1293)
at ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
2015-05-07 10:10:58,497 DEBUG (com.opensymphony.xwork2.DefaultActionInvocation:76) - No result returned for action com.gmc.action.user.LoginAction at null
2015-05-07 10:10:58,498 DEBUG (com.opensymphony.xwork2.interceptor.I18nInterceptor:80) - after Locale=zh_CN
2015-05-07 10:10:58,498 DEBUG (com.opensymphony.xwork2.interceptor.I18nInterceptor:76) - intercept } 


文件目录:

具体代码如下:
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
 
 
 
 
 
  <!-- 定义数据源Bean,使用C3P0数据源实现,并注入数据源的必要信息 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="com.mysql.jdbc.Driver"
p:jdbcUrl="jdbc:mysql://127.0.0.1:3306/gmc"
p:user="root"
p:password="root"
p:maxPoolSize="40"
p:minPoolSize="2"
p:initialPoolSize="2"
p:maxIdleTime="30"/>

<!-- 定义Hibernate的SessionFactory,SessionFactory需要依赖数据源,注入dataSource -->
<bean id="sessionFactory"  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" p:dataSource-ref="dataSource">
<!-- annotatedClasses用来列出全部持久化类 -->
<property name="annotatedClasses">
<list>
<!-- 以下用来列出所有的PO类-->
<value>com.gmc.entity.User</value>
<value>com.gmc.entity.Article</value>
</list>
</property>
<!-- 定义Hibernate SessionFactory的属性 -->
<property name="hibernateProperties">
<props>
<!-- 指定Hibernate的连接方言 -->
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<!--是否根据Hiberante映射创建数据表 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>


<context:component-scan base-package="com.gmc.*"/>
<!-- <context:component-scan base-package="com.gmc.service"/>
<context:component-scan base-package="com.gmc.dao"/>
<context:component-scan base-package="com.gmc.entity"/>
<context:component-scan base-package="com.gmc.action.*"/>  -->
</beans>

LoginAction:
package com.gmc.action.user;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.gmc.service.UserService;
import com.gmc.service.UserServiceImpl;
import com.opensymphony.xwork2.ActionSupport;



@Namespace("/user")
@Controller
public class LoginAction extends ActionSupport{
private UserServiceImpl userServiceImpl;

@Autowired
private void setUserService(UserServiceImpl userServiceImpl){
this.userServiceImpl = userServiceImpl;
}

public String execute()  {


System.out.println("LoginAction");
if (userServiceImpl.checkUser("ccc", "aaa")) {//
System.out.println("##############");
}else {
System.out.println("@@@@@@@@@@@@@@@");
}


return SUCCESS;
}
}



UserServiceInpl:
package com.gmc.service;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gmc.dao.UserDao;
import com.gmc.dao.UserDaoHibernate4;
import com.gmc.entity.User;

@Service
public class UserServiceImpl implements UserService {

private UserDaoHibernate4 userDaoHibernate4;

@Autowired
public void serUserDao(UserDaoHibernate4 userDaoHibernate4) {
this.userDaoHibernate4 = userDaoHibernate4;
}

@Override
public boolean checkUser(String user_name, String user_pass) {

User user = userDaoHibernate4.get(User.class, 1);
System.out.println(user.getUserName());


System.out.println("22222222222222222222");


return true;
}

}



------解决思路----------------------

??
@Repository
public?class?BaseDaoHibernate4<T> extends HibernateDao?implements?BaseDao<T>
{

}
然后下面的什么sessonFactory 不用写, spring帮你注入了

 * 通过主键查询数据
 */
public T get(final Pk id) {
return (T) this.getSession().get(this.entityClass, id);
}
直接这样 就可以 一定要继承hibernateDao
  相关解决方案