SSH环境搭建问题

J2EE 码拜 8年前 (2016-03-17) 940次浏览
本人这两天在使用三大框架的搭建上遇到各种出错。到现在还有点迷糊,所以讨教高手帮看下~~
例如:注解与xml方式注入的两种区别的环境搭建,时常也会出现sessionFactory  & HibernateTemplate is required 的错误。
事务管理器也有多中方式:
<!– 配置一个事务管理器 –>
<bean id=”transactionManager” class=”org.springframework.orm.hibernate4.HibernateTransactionManager”>
<property name=”sessionFactory” ref=”sessionFactory”/>
</bean>
<!– 配置事务,使用代理的方式 –>
<bean id=”transactionProxy” class=”org.springframework.transaction.interceptor.TransactionProxyFactoryBean” abstract=”true”>
<property name=”transactionManager” ref=”transactionManager”></property>
<property name=”transactionAttributes”>
<props>
<prop key=”add*”>PROPAGATION_REQUIRED,-Exception</prop>
<prop key=”modify*”>PROPAGATION_REQUIRED,-myException</prop>
<prop key=”del*”>PROPAGATION_REQUIRED</prop>
<prop key=”*”>PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!– 定义事务管理 –>
<bean id=”transactionManager”
class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
<property name=”sessionFactory” ref=”sessionFactory” />
</bean>
<!– 定义事务管理拦截器 –>
<bean id=”transactionInterceptor”
class=”org.springframework.transaction.interceptor.TransactionInterceptor”>
<property name=”transactionManager” ref=”transactionManager” />
<property name=”transactionAttributes”>
<props>
<prop key=”get*”>PROPAGATION_REQUIRED,readOnly</prop>
<prop key=”*”>PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!– 定义代理自动管理事务 –>
<bean id=”ProxyCreator” class=”org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator”>
<!– 指定需要Spring管理事务的Bean –>
<property name=”beanNames”>
<list>
<value>adminService</value>
<value>voteService</value>
<value>voteContextService</value>
<value>voterService</value>
</list>
</property>
<!– 调用事务管理拦截器 –>
<property name=”interceptorNames”>
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
怎么样才能正确理清这几种情况SSH环境搭建问题特别是注解与xml方式配置的区别!
遇到的错误:Error creating bean with name “userController”: Injection of autowired dependencies failed;
解决方案

2

看下本人转载的博客吧,里面有详细的spring事务的配置

5

木有什么区别,是实现问题的两种方式而已

2

用xml方式映射,xml文件太多,对工程而言,有点臃肿,建议用注解映射,轻巧方便,改起来也容易,我们现在做项目都用注解。

2

引用:
Quote: 引用:

木有什么区别,是实现问题的两种方式而已

能帮本人看下为何到达数据库之后,好像是获取不到sessionFactory,然后得不到数据库的数据呢~
<?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:aop=”http://www.springframework.org/schema/aop”
xmlns:tx=”http://www.springframework.org/schema/tx”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd”>
<!– 配置数据源 –>
<bean id=”dataSource” class=”org.springframework.jdbc.datasource.DriverManagerDataSource” >
<property name=”driverClassName” value=”com.mysql.jdbc.Driver”></property>
<property name=”url” value=”jdbc:mysql://localhost/test_ssh”></property>
<property name=”username” value=”root”></property>
<property name=”password” value=”root”></property>
</bean>
<!– 配置SessionFactory –>
<bean id=”sessionFactory” class=”org.springframework.orm.hibernate4.LocalSessionFactoryBean”>
<property name=”dataSource” ref=”dataSource” />
<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>
<prop key=”hibernate.format_sql”>true</prop>
</props>
</property>
<property name=”annotatedClasses”>
<list>
<value>com.tgb.entity.User</value>
</list>
</property>
</bean>

<bean id=”transactionManager” class=”org.springframework.orm.hibernate4.HibernateTransactionManager”>
<property name=”sessionFactory” ref=”sessionFactory”/>
<property name=”dataSource” ref=”dataSource” />
</bean>

<tx:annotation-driven transaction-manager=”transactionManager” />

<tx:advice id=”txAdvice” transaction-manager=”transactionManager”>
<tx:attributes>
<tx:method name=”add*” propagation=”REQUIRED”/>
<tx:method name=”del*” propagation=”REQUIRED”/>
<tx:method name=”modify*” propagation=”REQUIRED”/>
<tx:method name=”*” read-only=”true”/>
</tx:attributes>
</tx:advice>
</beans>
UserDaoImpl.java :
@Repository
public class UserDaoImpl implements UserDao{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public User getUser(String id) {
String hql = “from User u where u.id=?”;
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setString(0, id);
return (User)query.uniqueResult();
}
@Override
public List<User> getAllUser() {
String hql = “from User”;
Query query = sessionFactory.getCurrentSession().createQuery(hql);
return query.list();
}
}
UserManagerImpl.java:
@Service
@Transactional
public class UserManagerImpl implements UserManager {
@Autowired
private UserDao userDao;
@Override
@Transactional(readOnly = true)
public User getUser(String id) {
return userDao.getUser(id);
}
@Override
@Transactional(readOnly = true)
public List<User> getAllUser() {
return userDao.getAllUser();
}
}
UserController.java:
@Controller
@RequestMapping(“/user”)
public class UserController {
@Autowired
private UserManager userManager;
@RequestMapping(“/getAllUser”)
public String getAllUser(HttpServletRequest request){
request.setAttribute(“userList”, userManager.getAllUser());
return “/index”;
}
@RequestMapping(“/getUser”)
public String getUser(String id,HttpServletRequest request){
request.setAttribute(“user”, userManager.getUser(id));
return “/editUser”;
}
}
最后的错误是:java.lang.NullPointerException
at com.tgb.dao.UserDaoImpl.getAllUser(UserDaoImpl.java:35)
at com.tgb.manager.UserManagerImpl.getAllUser(UserManagerImpl.java:35)
at com.tgb.web.UserController.getAllUser(UserController.java:30)
空指针异常,本人是哪里配置错误了呢

sessionFactory没注入进去,dao层没继承hibernate的话,你在sessionfactory上加个注解试试

1

建议你一步步来,先把sping mvc(个人认为他就是相似struts2,一个入口的配置) ,环境搭起来,再加入sping (他可以说是与数据打交道的,本人是这个觉得的),然后再加入Hibernate或mybatis

2

sessionFactory.getCurrentSession().save(user);
boolean flag = sessionFactory.getCurrentSession().save(user) != null;
你这里执行了两遍保存是什么鬼,两外看一下你传进去的user有没有值

1

引用:
Quote: 引用:

sessionFactory.getCurrentSession().save(user);
boolean flag = sessionFactory.getCurrentSession().save(user) != null;
你这里执行了两遍保存是什么鬼,两外看一下你传进去的user有没有值

执行一遍啊!传值了、一个是判断能否添加成功啊

那你的意思是你的判断没有执行操作喽,没操作怎么可能会给你返回执行状态,哈哈哈哈,另外你要是新增的话是不是要用insert操作

1

恩,看看你hibernate配置的主键策略

2

引用:
Quote: 引用:

恩,看看你hibernate配置的主键策略

跟主键策略有关系?本人是用注解的方式
package com.tgb.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.transaction.annotation.Transactional;
@Entity
@Table(name=”T_USER”)
public class User {
@Id
@GeneratedValue(generator=”system-uuid”)
@GenericGenerator(name = “system-uuid”,strategy=”uuid”)
@Column(length=32)
private String id;
@Column(length=32)
private String userName;
@Column(length=32)
private String age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}

不清楚你这目前是什么问题,只是在猜测,执行了保存没有报错,猜想看是不是原因是主键的问题它做的是更新的操作。目前本人市想不出什么了,你可以在调试一下看看

2

在你的 application-context里面  把 你的 action写进去 就ok例如:
<bean id=”categoryAction” class=”com.dh.action.CategoryAction” scope=”prototype”>
<!–这里具体 你配的啥本人写–>
<property name=”categoryService” ref=”categoryService”></property>
</bean>

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明SSH环境搭建问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)