Struts2 Spring3 Hibernate3 菜鸟教程 集成注解版本

这篇博客介绍了Struts2、Spring3和Hibernate3的集成,特别是注解版本与XML配置的区别。在注解版本中,自定义的DAO、Service和Action类使用注解进行配置,而不推荐继承HibernateDaoSupport。同时,文章提到了必须提供事务支持,并给出了UserDAOImpl、UserServiceImpl、UserAction的示例,以及applicationContext.xml的部分内容。此外,还分享了其他关联项目的链接和源码地址。
摘要由CSDN通过智能技术生成

集成注解版本和xml版本所不同的地方

  • 如果dao,service,action类不是自己写的还是在xml里面进行配置
  • 只有是自己写的dao,service,action类,直接在类上面标注对应的注解
  • 注解版本不建议继承HibernateDaoSupport实现持久层操作
  • 必须要提供事务支持

UserDAOImpl

package com.jege.ssh.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jege.ssh.dao.UserDAO;
import com.jege.ssh.entity.User;

/**
 * @author JE哥
 * @email 1272434821@qq.com
 * @description:dao接口实现
 */
@Repository
public class UserDAOImpl implements UserDAO {
   

  @Autowired
  SessionFactory sessionFactory;

  private Session currentSession() {
    return sessionFactory.getCurrentSession();
  }

  @Override
  public void save(User user) {
    currentSession().save(user);
  }

  @Override
  public void update(User user) {
    currentSession().update(user);
  }

  @Override
  public void delete(Long id) {
    User user = findByKey(id);
    if (user != null) {
      currentSession().delete(user);
    }
  }

  @Override
  public User findByKey(Long id) {
    return (User) currentSession().get(User.class, id);
  }

  @Override
  public List<User> findAll() {
    return currentSession().createCriteria(User.class).list();
  }

}

UserServiceImpl

package com.jege.ssh.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jege.ssh.dao.UserDAO;
import com.jege.ssh.entity.User;
import com.jege.ssh.service.UserService;

/**
 * @author JE哥
 * @email 12724
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值