SSH注解整合(spring+struts2+hibernate)

时间:2022-05-03 04:59:20

一.依赖

环境准备:idea+orcl数据库       实验:向orcl数据库中添加一条数据

SSH注解整合(spring+struts2+hibernate)

 

 <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.3</version>
            <scope>test</scope>
        </dependency>

        <!--spring配置-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>
        <!--aop使用的jar-->
        <dependency>
            <groupId> org.aspectj</groupId >
            <artifactId> aspectjweaver</artifactId >
            <version> 1.8.7</version >
        </dependency>

        <!--SpringWeb-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.8.RELEASE</version>
        </dependency>

        <!--JavaEE-->
        <dependency>
            <groupId>javaee</groupId>
            <artifactId>javaee-api</artifactId>
            <version>5</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>

        <!--c3p0-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>

        <!--hibernate jar包-->
        <!--jta的jar包-->
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.6.Final</version>
        </dependency>

        <!--Spring-ORM-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version> 4.2.2.RELEASE</version>
        </dependency>

        <!--Oracle驱动的jar-->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.struts.xwork</groupId>
            <artifactId>xwork-core</artifactId>
            <version>2.3.4.1</version>
        </dependency>

        <!--Struts整合Spring的jar包-->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.4.1</version>
        </dependency>

        <!--Struts2注解支持jar包-->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.4.1</version>
        </dependency>

二.配置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>

  <!--Spring配置
  1.上下文  识别applicationContext.xml
  -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  <!--Struts2配置  核心过滤器 -->
  <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>
<!--2.监听器 在Tomcat容器启动的时候,帮我创建Spring容器,并且放入application中-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>



</web-app>

三.spring核心配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">

    <context:component-scan base-package="cn.wy"></context:component-scan>
<!--
1.数据源  c3p0
-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--
 2.识别到jdbc.properties
-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <!--
 3.形成SessionFactory
-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <!--方言-->
              <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <!--是否打印sql-->
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
            </props>
        </property>
        <!--关联小配置-->
        <property name="packagesToScan" value="cn.wy.entity"></property>
    </bean>
    <!--
7.事务管理器
-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
<!--
8.事务真实配置
-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>


</beans>

 

四.书写entity层和dao层

@Entity    //表示该类为实体类
@Table(name = "Student")//表示数据库底层对应的表名
public class Student {
    @Id
    @GeneratedValue  //orcl的主键生成策略
    private Integer id;
    @Column
    private String name;


    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

 

@Repository("StudentDAO") //将该类声明成dao
public class StudentDAOImpl implements IStudentDAO {

    @Resource(name = "sessionFactory")//将配置文件中注册的sessionFactory注入
    SessionFactory sessionFactory;
    public int add(Student student) {
        Session session = sessionFactory.getCurrentSession();
        Serializable save = session.save(student);
        return (Integer)save;
    }
}

 

 五.Service层

@Service("StudentService")//将该类注册成service服务
public class StudentServiceImpl implements IStudentService {
    @Resource(name = "StudentDAO")//将到层注入
    private IStudentDAO dao;
    @Transactional
    public int add(Student student) {
        return dao.add(student);
    }

    public IStudentDAO getDao() {
        return dao;
    }

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

六.action层

@Controller    //将该类声明成控制器
@ParentPackage("struts-default")//表示status2的配置继承的是struts-default.xml
@Namespace("/")//命名空间
@Scope("prototype")//将spring管理该类改为多例的
public class StudentAction implements Action {
    private Student student;
    @Resource(name = "StudentService")//将Service注入
    private IStudentService studentService;
    //                                              请求路劲                                    请求成功               跳转的页面     
    @org.apache.struts2.convention.annotation.Action(value = "/add",results = {@Result(name = "success",location = "/jsp/index.jsp")})
    public String execute() throws Exception {
        studentService.add(student);
        return SUCCESS;
    }


    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public IStudentService getStudentService() {
        return studentService;
    }

    public void setStudentService(IStudentService studentService) {
        this.studentService = studentService;
    }
}

 

 

SSH注解整合(spring+struts2+hibernate)

SSH注解整合(spring+struts2+hibernate)