码迷,mamicode.com
首页 > 其他好文 > 详细

ssh框架整合之xml版

时间:2018-03-05 15:29:45      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:plugin   software   servlet   dao   constant   word   nts   ssh框架   actor   

一,引jar包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <parent>
        <artifactId>Hebirnate</artifactId>
        <groupId>cn.happy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>sshAnno</artifactId>
    <packaging>war</packaging>
    <name>sshAnno Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</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导入-->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.1.0</version>
        </dependency>

        <!--struts2-->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.4.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts.xwork</groupId>
            <artifactId>xwork-core</artifactId>
            <version>2.3.4.1 </version>
        </dependency>

        <!-- struts2整合spring -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.4.1</version>
        </dependency>

        <!-- struts注解核心包 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.4.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

二,搭建项目

技术分享图片

2.1 entity包下Dept类

public class Dept {
    private Integer deptno;
    private String deptname;
    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;
    }
}

2.1.1 entity包下Dept.hbm.xml

<?xml version="1.0"?>
        <!DOCTYPE hibernate-mapping PUBLIC
                "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.ssh.entity">
<!--实体 name=实体端的内容   column=DB端的内容-->
<class name="Dept" table="Dept" schema="zyx">
    <!--和底层数据表对应的主键   业务意义-->
    <id name="deptno" column="DEPTNO">
        <!--主键生成策略 :assigned:程序员手动给值-->
        <generator class="native"/>
    </id>
    <property name="deptname" column="DEPTNAME"></property>
    <!--   一对多 1个部门 多个员工-->
</class>
</hibernate-mapping>

2.2   DAO包下IDeptDao接口

public interface IDeptDao {
    public int addDept(Dept dept);
}

2.2.1 DAO包下DeptDaoImpl

public class DeptAction implements Action {
    private Dept dept;
    IDeptService service;

    public Dept getDept() {
        return dept;
    }

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

    public IDeptService getService() {
        return service;
    }

    public void setService(IDeptService service) {
        this.service = service;
    }

    public String execute() throws Exception {
        service.addDept(dept);
        return SUCCESS;

    }
}

2.3 Service包下IDeptService接口

public interface IDeptService {
    public int addDept(Dept dept);
}

2.3.1 Service包下DeptServiceImpl

public class DeptServiceImpl implements IDeptService{
    IDeptDao dao;

    public IDeptDao getDao() {
        return dao;
    }

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

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

2.4 action包下DeptAction

public class DeptAction implements Action {
    private Dept dept;
    IDeptService service;

    public String execute() throws Exception {
        service.addDept(dept);
        return SUCCESS;

    }
    public Dept getDept() {
        return dept;
    }

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

    public IDeptService getService() {
        return service;
    }

    public void setService(IDeptService service) {
        this.service = service;
    }

}

2.5 resources下applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
    <!--
    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.bookDAO    没有实现类-->
    <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="mappingDirectoryLocations" value="classpath:cn/ssh/entity"></property>
    </bean>
    <!--
        4.dao
        -->
    <bean id="deptDao" class="cn.ssh.dao.DeptDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!--
    5.service
    -->
    <bean id="deptService" class="cn.ssh.service.DeptServiceImpl">
        <property name="dao" ref="deptDao"></property>
    </bean>
    <!--
    6.action
    -->
    <bean id="deptAction" class="cn.ssh.action.DeptAction">
        <property name="service" ref="deptService"></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>

2.6 resources下jdbc.properties

jdbc.jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.username=zyx
jdbc.password=zyx

2.7  resources下struts.xml 

<?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE struts PUBLIC
                "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
                "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>


    <constant name="struts.devMode" value="true"/>
    <package name="default" namespace="/" extends="struts-default">
    <action name="add" class="deptAction">
        <result>/add.jsp</result>
    </action>
    </package>
</struts>

2.8 千万别忘了配置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>
  <!--配置struts-->
  <filter>
    <filter-name>struts1</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts1</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

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

三,配置web页面

3.1 index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>添加部门</head>
<body>
<form method="post" action="/add">
    部门名称:<input name="dept.deptname"/>
    <input type="submit" value="添加"/>
</form>
</body>
</html>

  

 

ssh框架整合之xml版

标签:plugin   software   servlet   dao   constant   word   nts   ssh框架   actor   

原文地址:https://www.cnblogs.com/spghs/p/8508790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!