ssh(Spring,Struts2,Hibernate)注解版整合

这是一个用注解版整合的一个添加部门的简单例子,主要代码如下:

首先是我们都熟悉的entity实体层,这里我起的包名是bean,

Dept实体类:如下:

package bean;

import javax.persistence.*;

/**
 * Created by yu  fan on 2018/1/5.
 */
@Entity
@Table(name="DEPT")
public class Dept {
    public Integer getDeptno() {
        return deptno;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }

    public String getDeptname() {
        return deptname;
    }

    public void setDeptname(String deptname) {
        this.deptname = deptname;
    }

    @Id
    @GeneratedValue
   private Integer deptno;

    @Column
    private String deptname;

这个类中需要标注一些基本的注解,实体,表,主键,和表中其他字段...

2.接下来是dao层:

接口:IDeptDAO:

package dao;


import bean.Dept;

/**
 * Created by yu  fan on 2018/1/5.
 */
public interface IDeptDAO {

    public int addDept(Dept dept);
}

实现类:DeptDAOimpl:

package dao;


import bean.Dept;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * Created by yu  fan on 2018/1/5.
 */
@Repository("deptDAO")
public class DeptDAOimpl implements IDeptDAO {
    @Resource(name = "sessionFactory")
    private SessionFactory sessionFactory;

    //添加
    public int addDept(Dept dept){
        Serializable count=sessionFactory.getCurrentSession().save(dept);
        return (Integer) count;
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

}

这里需要标注两个地方,一个是dao层的识别@Repository,还有一个是对事务的识别@Resource

3.第三部就是service了:

部门服务的接口(IDeptService)跟dao层一样,这里就省略了...

DeptServiceimpl代码如下:

package service;

import bean.Dept;
import dao.IDeptDAO;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created by yu  fan on 2018/1/5.
 */
@Service("deptService")
public class DeptServiceimpl implements  IDeptService {
    @Resource(name = "deptDAO")
    private IDeptDAO dao;

    @Transactional
    public int addDept(Dept dept) {
        return dao.addDept(dept);
    }

    public IDeptDAO getDao() {
        return dao;
    }

    public void setDao(IDeptDAO dao) {
        this.dao = dao;
    }
}

这个类中标注了识别service的注解,dao层实现类的注解,还有一个@Transaction

4.接下来就是最重要的action层:

DeptAction:

package action;


import bean.Dept;
import com.opensymphony.xwork2.ActionSupport;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import service.IDeptService;

import javax.annotation.Resource;


@Controller
@ParentPackage("struts-default")
@Namespace("/")
@Scope("prototype")
public class DeptAction extends ActionSupport {
    private Dept dept;
    @Resource(name = "deptService")
    private IDeptService deptService;

  @Action(value = "/add",results = {@Result(name = "success",location ="/index.jsp" )})
    public String add(){
        deptService.addDept(dept);
        return SUCCESS;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public IDeptService getDeptService() {
        return deptService;
    }

    public void setDeptService(IDeptService deptService) {
        this.deptService = deptService;
    }

}

这个类相当于以前的处理器层,与页面交互的,首先标注@Controller,第二标注@RarentPackage,它里边的属性与普通ssh整合中的struts.xml中

    <package name="default" namespace="/" extends="struts-default">
        <action name="add" class="deptAction" method="add">
        <result>/index.jsp</result>
        </action>
    </package>

package标签中的属性extends的值一样,所以之后的

@Namespace,@Action就参照原版ssh整合中的配置来,在这里解释一下@action中的属性:value是你启动tomcat后访问的页面,
results代表(结果),中间的属性name="success",location="添加成功后指向的页面"

5.第五步applicationContext.xml
在这里写一下需要配的一些东西,就不粘代码了:
1>包扫描器
2>数据源
3>识别数据源的配置文件(jdbc.properties)
4>创建工厂(sessionFactory),里边包括:dataSourcce,是否显示sql,扫描小配置
5>事务
6>事务管理器

6.第六步:web.xml中的配置:
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <filter>
    <filter-name>struts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--监听器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

7.最后一步  jsp页面:insert.jsp:

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加</title>
</head>
<body>
<s:form name="form1" namespace="/" method="post" action="add">
    请输添加的名词:</br>  <s:textfield name="dept.deptname"/>
    <s:submit value="添加"></s:submit>
</s:form>
</body>
</html>

写完这些代码,你就启动tomcat试试吧.......................................



原文地址:https://www.cnblogs.com/yfyfyff/p/8509107.html