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/frameworkjava

若是以爲個人文章或者代碼對您有幫助,能夠請我喝杯咖啡。您的支持將鼓勵我繼續創做!謝謝!
微信打賞
支付寶打賞git

相關文章
相關標籤/搜索