基于annotation的ssh整合问题

按照网上的教程,做了个用户注册的小例子。

输入用户名、密码提交后,每次都提示注册成功,控制台也能输出新用户的id,但数据库中却没有记录。

之前在另一个项目中,将配置文件改为annotation时,也出现过这种问题,一直没有找到原因,不知道各位前辈有没有人知道原因?

 

注册页面index.jsp

<s:form action="register">
         <s:textfield label="用户名" name="username"/>
         <s:password label="密码" name="password"/>
         <s:submit value="注册"/>
</s:form>

 

Action:

 @Controller    
 @Scope("prototype")       
 @ParentPackage(value="struts-default")
 @Results({
     @Result(name="success",value="success.jsp"),
     @Result(name="input",value="index.jsp")
 })
  public class RegisterAction extends ActionSupport {
     
     @Resource(name="userService")
     private UserServiceBean userService;
     
     private String username;
     private String password;
 
     @Override
      public String execute() throws Exception {
         User user = new User();
         user.setUsername(username);
         user.setPassword(password);
         userService.save(user);
         return SUCCESS;
     }
}

 

Service:

@Service("userService")        
 public class UserServiceBean {
    @Autowired
    private UserDao userDao;

    public void save(User user){
        int id = userDao.save(user);
        System.out.println("id="+id);
    }
}

 

DAO:

 @Repository("userDao")         
 public class UserDaoBean extends MyHibernateDaoSupport implements UserDao {
     
      public int save(User user){
    Integer id = (Integer)super.getHibernateTemplate().save(user);    
             return id;
     }

 

MyHibernateDaoSupport .java:

public class MyHibernateDaoSupport extends HibernateDaoSupport {
     
     @Resource(name="sessionFactory")  
     //为父类HibernateDaoSupport注入sessionFactory的值
     public void setSuperSessionFactory(SessionFactory sessionFactory){
         super.setSessionFactory(sessionFactory);
     }
 
 }

 

 applicationContext.xml

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>

 

hibernate.cfg.xml

<session-factory>
    <property name="connection.username">root</property>
    <property name="connection.url">
        jdbc:mysql://localhost:3306/ssh_annotation
    </property>
    <property name="dialect">
        org.hibernate.dialect.MySQLDialect
    </property>
    <property name="myeclipse.connection.profile">
        ssh_anno_mysql
    </property>
    <property name="connection.password">ruihui</property>
    <property name="connection.driver_class">
        com.mysql.jdbc.Driver
    </property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
    <!-- 将Session扔到线程里去处理 -->
    <property name="current_session_context_class">thread</property>

    <mapping  class="com.rong.entity.User"/>
</session-factory>

 

 

6个回答

[code="java"]
@Service("userService")
[color=red]@Transactional[/color]
public class UserServiceBean {
@Autowired
private UserDao userDao;

public void save(User user){
    int id = userDao.save(user);
    System.out.println("id="+id);
}

}
[/code]
当然,如果你不是用的注解事务,这里不需要添加

[code="java"]
public class MyHibernateDaoSupport extends HibernateDaoSupport {

 @Resource(name="sessionFactory")  
 //为父类HibernateDaoSupport注入sessionFactory的值
 public void setSuperSessionFactory(SessionFactory sessionFactory){
     super.setSessionFactory(sessionFactory);
 }

}
[/code]
修改为
[code="java"]
public class MyHibernateDaoSupport extends HibernateDaoSupport {
@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;

@PostConstruct
public void setHibernateSessionFactory() {
super.setSessionFactory(sessionFactory);
}

}
[/code]

[code="java"]
public class MyHibernateDaoSupport extends HibernateDaoSupport {
@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;

@PostConstruct
public void setSessionFactory() {
super.setSessionFactory(sessionFactory);
}

}
[/code]

修改为这个

那你还改为
[code="java"]
public class MyHibernateDaoSupport extends HibernateDaoSupport {
@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;

@PostConstruct
public void setHibernateSessionFactory() {
super.setSessionFactory(sessionFactory);
}

}
[/code]
然后你确认下,保存的时候sessionFactory有值吗,是否是空的

还有你的配置文件中事务配置了吗?

例如这些代码
[code="java"]
class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="hibernateTransactionManager"
    proxy-target-class="true" />

[/code]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问