SSH 框架搭建 xml 配置版

本贴最后更新于 2281 天前,其中的信息可能已经时移俗易

分层

父模块:
erp_parent
子模块:

  • erp_entity
  • erp_dao
  • erp_biz
  • erp_web

pom.xml 文件配置

pom.xml 如下: 注意解决依赖冲突的问题

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>xin.liuyishi.erp</groupId>
    <artifactId>erp_parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>erp_entity</module>
        <module>erp_dao</module>
        <module>erp_biz</module>
        <module>erp_web</module>
    </modules>
    <packaging>pom</packaging>

    <name>erp_parent</name>
    <url>http://maven.apache.org</url>

    <properties>
        <hibernate.version>5.0.7.Final</hibernate.version>
        <spring.version>4.2.4.RELEASE</spring.version>
        <struts.version>2.3.24</struts.version>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>${struts.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>javassist</groupId>
                    <artifactId>javassist</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>${struts.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-beans</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-web</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-core</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-context</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>log4j</artifactId>
                    <groupId>log4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- log end -->

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.37</version>
        </dependency>

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

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <!-- 指定端口 -->
                    <port>8080</port>
                    <!-- 请求路径 -->
                    <path>/erp</path>
                    <!-- 页面请求提交的编码 -->
                    <uriEncoding>utf-8</uriEncoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

erp_entity 模块

在 erp_entity 模块中添加 Dep 实体类 和 dep.hbm.xml 映射文件
dep 实体类

public class Dep {

	private Long uuid;
	private String name;
	private String tele;
	
	
	public Long getUuid() {
		return uuid;
	}
	public void setUuid(Long uuid) {
		this.uuid = uuid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTele() {
		return tele;
	}
	public void setTele(String tele) {
		this.tele = tele;
	}
}

dep.hbm.xml 映射文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="xin.liuyishi.erp.entity.Dep" table="dep">
        <id name="id">
            <column name="id"/>
            <generator class="native"/>
        </id>
        <property name="name"/>
        <property name="tele"/>
    </class>
</hibernate-mapping>

erp_dao 模块

DepDapImpl 类

package xin.liuyishi.erp.dao.impl;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import xin.liuyishi.erp.dao.DepDao;
import xin.liuyishi.erp.entity.Dep;
import java.util.List;

@SuppressWarnings("all")
public class DepDaoImpl extends HibernateDaoSupport implements DepDao {
    
    public List<Dep> getList() {
        return (List<Dep>) getHibernateTemplate().find("from Dep");
    }
}

dao 配置文件

applicationContext_dao.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="depDao" class="xin.liuyishi.erp.dao.impl.DepDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
</beans>

applicationContext_datasource.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/erp?characterEncoding=utf-8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <property name="mappingLocations">
            <value>classpath:*.hbm.xml</value>
        </property>
    </bean>
</beans>

测试类:

package xin.liuyishi.erp;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import xin.liuyishi.erp.dao.DepDao;

public class DaoModelTest {

    @Test
    public void Mytest() {
        ApplicationContext ct = new ClassPathXmlApplicationContext("classpath*:applicationContext_*.xml");
        DepDao depDao = (DepDao) ct.getBean("depDao");

        System.out.println(depDao.getList().size());
    }
}

erp_biz 模块

DepBizImpl 类

package xin.liuyishi.erp.biz.impl;

import xin.liuyishi.erp.biz.DepBiz;
import xin.liuyishi.erp.dao.DepDao;
import xin.liuyishi.erp.entity.Dep;

import java.util.List;

public class DepBizImpl implements DepBiz {

    private DepDao depDao;

    public void setDepDao(DepDao depDao) {
        this.depDao = depDao;
    }

    public List<Dep> getList() {
        return depDao.getList();
    }
}

applicationContext_biz.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="depBiz" class="xin.liuyishi.erp.biz.impl.DepBizImpl">
        <property name="depDao" ref="depDao"/>
    </bean>

</beans>

applicationContext_tx.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:aop="http://www.springframework.org/schema/aop"
       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/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">

    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:advice id="advice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="do*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="serviceMethod" expression="execution(* xin.liuyishi.erp.biz.impl.*.*(..))"/>
        <aop:advisor pointcut-ref="serviceMethod" advice-ref="advice" />
    </aop:config>

</beans>

测试类:

package xin.liuyishi.erp;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import xin.liuyishi.erp.biz.DepBiz;

public class BizModelTest {

    @Test
    public void MyTest() {

        ApplicationContext ct = new ClassPathXmlApplicationContext("classpath*:applicationContext_*.xml");
        DepBiz depBiz = (DepBiz) ct.getBean("depBiz");

        System.out.println(depBiz.getList().size());
    }

}

web 模块

DepAction 类

package xin.liuyishi.erp.action;

import com.alibaba.fastjson.JSON;
import org.apache.struts2.ServletActionContext;
import xin.liuyishi.erp.biz.DepBiz;
import xin.liuyishi.erp.entity.Dep;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class DepAction {

    private DepBiz depBiz;

    public void setDepBiz(DepBiz depBiz) {
        this.depBiz = depBiz;
    }


    public void list() {
        List<Dep> list = depBiz.getList();
        String jsonString = JSON.toJSONString(list);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/html;charset=utf-8");
        try {
            response.getWriter().write(jsonString);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

applicationContext_action.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="depAction" class="xin.liuyishi.erp.action.DepAction" scope="prototype">
        <property name="depBiz" ref="depBiz"/>
    </bean>

</beans>

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>

    <package name="default" namespace="/" extends="struts-default">
        <action name="dep_*" class="depAction" method="{1}"/>
    </package>

</struts>

浏览器访问: http://localhost:8080/erp/dep_list

结果如下:

[
    {
        "id": 7,
        "name": "工程部",
        "tele": "0766-789"
    },
    {
        "id": 8,
        "name": "研发部",
        "tele": "0755-456"
    }
]

  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3168 引用 • 8207 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...
  • gyzh

    有没有现成的文件
    发一下连接
    想要一个样本