集成注解版本和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