ssh整合(spring + struts2 + hibernate)xml版

Posted 会飞的豆子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssh整合(spring + struts2 + hibernate)xml版相关的知识,希望对你有一定的参考价值。

1.1分层

 

1.2jar节点

<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>




        <!--mysql数据库-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.31</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>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

2.1. DeptAction 

复制代码
public class DeptAction extends ActionSupport {
    private Dept dept;
    private IDeptService deptService;
    public String add(){
        System.out.println(dept);
        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;
    }
}
复制代码

2.2.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.3.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.bdqn.bean">
    <class name="Dept" table="Dept">
        <id name="deptno" column="deptno">
            <generator class="native"></generator>
        </id>
            <property name="deptname" />
    </class>
</hibernate-mapping>
复制代码

 

 2.4. IDeptDAO 

public interface IDeptDAO {
    //添加部门的方法
    public int addDept(Dept dept);

}

 

 2.5.DeptDAOImpl 

复制代码
public class DeptDAOImpl implements IDeptDAO{
    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;
    }

    public List<Dept> findAll() {
        return sessionFactory.getCurrentSession().createQuery("from Dept").list();
    }

}
复制代码

 

 2.6.IDeptService

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

2.7 DeptServiceImpl 

复制代码
public class DeptServiceImpl implements IDeptService{
    private IDeptDAO deptDAO;
    @Transactional
    public void addDept(Dept dept) {
        deptDAO.addDept(dept);
    }

    public IDeptDAO getDeptDAO() {
        return deptDAO;
    }

    public void setDeptDAO(IDeptDAO deptDAO) {
        this.deptDAO = deptDAO;
    }
}
复制代码

 

以上是关于ssh整合(spring + struts2 + hibernate)xml版的主要内容,如果未能解决你的问题,请参考以下文章

ssh整合

SSH(struts2+spring+hibernate)三大框架整合

SSH整合

SSH(Spring+struts2+hibernate)整合版详解

SSH(Spring+Struts2+hibernate)整合基于注解开发的详解

SSH Struts2+hiberante+Spring整合