学习:SSH整合CRUD实例

SSH整合前期内容,参考SSH整合思路
整合了Struts2、Hibernate和Spring,有简单的CRUD
完整源码:https://download.csdn.net/download/qq_35206244/10588865
失效联系我更新

环境:
Eclipse Oxygen
JDK:1.7
Tomcat:7.0
数据库:Oracle


一、数据库

1.1、建表


create table PERSON
(
  pid   NUMBER(5) not null,
  pname VARCHAR2(20),
  pwd   VARCHAR2(20)
)

1.2、建序列(配合触发器实现id自增)

create sequence SEQ_PERSON
minvalue 1
maxvalue 200
start with 1
increment by 1
nocache;

1.3、建触发器

CREATE OR REPLACE TRIGGER tri_person
BEFORE
insert on person
for each row
begin
select seq_person.nextval into :new.pid from dual;
end ;

二、建web项目

2.1、项目目录树

这里写图片描述

2.2、applicationContext.xml配置

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
    <property name="username" value="scott"></property>
    <property name="password" value="root"></property>    
  </bean>
  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="current_session_context_class">thread</prop>
        </props>
    </property>
    <property name="mappingResources">
        <list>
            <value>com/it/ssh/entity/Person.hbm.xml</value>
        </list>
    </property>
  </bean>
  <bean id="personDao" class="com.it.ssh.dao.impl.PersonDaoImpl">
    <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>
  <bean id="personService" class="com.it.ssh.service.impl.PersonServiceImpl">
    <property name="personDao" ref="personDao"></property>
  </bean>
  <bean name="PersonAction" class="com.it.ssh.action.PersonAction">
    <property name="personService" ref="personService"></property>
  </bean>

2.3、struts.xml配置

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="false" />

    <package name="default" namespace="/" extends="struts-default">

        <default-action-ref name="personAction" />
        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>

        <action name="personAction" class="PersonAction">
            <result name="success">show.jsp</result>
            <result name="input" type="redirect">login.jsp</result>
        </action>
    </package>

    <include file="example.xml"/>

    <!-- Add packages here -->

</struts>

2.4、web.xml配置

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

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

三、效果

这里写图片描述

后记:简单搭建了SSH、整合了一个简单的CRUD,可供新手参考。水平有限,欢迎纠错。

猜你喜欢

转载自blog.csdn.net/qq_35206244/article/details/81367316