Struts2 Spring3 Hibernate3 集成注解版本

集成注解版本和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 1272434821@qq.com
 * @description:业务逻辑层
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
  @Autowired
  private UserDAO userDAO;

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

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

  @Override
  public void delete(Long id) {
    userDAO.delete(id);
  }

  @Override
  @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
  public User findByKey(Long id) {
    return userDAO.findByKey(id);
  }

  @Override
  @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
  public List<User> findAll() {
    return userDAO.findAll();
  }

}

UserAction

package com.jege.ssh.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jege.ssh.entity.User;
import com.jege.ssh.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author JE哥
 * @email 1272434821@qq.com
 * @description:控制器类
 */
@Controller
@Scope("prototype")
public class UserAction extends ActionSupport {
  @Autowired
  private UserService userService;
  private User user;

  @Override
  public String execute() throws Exception {
    ActionContext.getContext().put("users", userService.findAll());
    return SUCCESS;
  }

  @Override
  public String input() throws Exception {
    if (user != null && user.getId() != null) {
      user = userService.findByKey(user.getId());
    }
    return INPUT;
  }

  public String save() throws Exception {
    if (user == null) {
      return execute();
    }
    if (user.getId() == null) {
      userService.save(user);
    } else {
      userService.update(user);
    }
    return "redirect";
  }

  public String delete() throws Exception {
    if (user != null && user.getId() != null) {
      userService.delete(user.getId());
    }
    return "redirect";
  }

  public User getUser() {
    return user;
  }

  public void setUser(User user) {
    this.user = user;
  }

}

applicationContext.xml

<?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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.jege.ssh" />

    <context:property-placeholder location="classpath:jdbc.properties" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="url" value="${jdbc.url}" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 引入jpa的包 -->
        <property name="packagesToScan" value="com.jege.ssh.entity" />
        <!-- hibernate的其他配置信息,必须添加hibernate.前缀 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            </props>
        </property>
    </bean>

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

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

</beans>

User

package com.jege.ssh.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 * @author JE哥
 * @email 1272434821@qq.com
 * @description:单表
 */
@Entity
public class User {
  @Id
  @GeneratedValue
  private Long id;
  private String name;
  private Integer age;

  public User() {

  }

  public User(String name, Integer age) {
    this.name = name;
    this.age = age;
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }

  @Override
  public String toString() {
    return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
  }

}

其他关联项目

源码地址

https://github.com/je-ge/framework

如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。您的支持将鼓励我继续创作!谢谢!
微信打赏
支付宝打赏

原文地址:https://www.cnblogs.com/je-ge/p/6361567.html