colejie
colejie
2010-12-22 16:24

基于annotation的ssh整合问题

  • it技术
  • 互联网问答
  • IT行业问题
  • 编程语言问答
  • 计算机技术

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

输入用户名、密码提交后,每次都提示注册成功,控制台也能输出新用户的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条回答

为你推荐