三大框架的整合struts2.2+hibernate3.2+spring2.5

三大框架的整合,直接的上手:

一、新建立一个web项目,在项目中进行我们的导包的操作:

struts2.2的基本的包(13个):

asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.3.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.3.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
log4j-1.2.17.jar
ognl-3.0.6.jar
struts2-core-2.3.15.1.jar
xwork-core-2.3.15.1.jar

spring2.5的基本的包(5个):

aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
commons-logging.jar
spring.jar

hibernate3.2的基本的包(15个):

antlr-2.7.6.jar
asm-attrs.jar
asm.jar

aspectjrt.jar(切面)
aspectjweaver.jar(切面)


cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
ehcache-1.2.jar
ejb3-persistence.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate3.jar
jta.jar

数据库mysql的连接包(1个):


mysql-connector-java-3.1.10-bin.jar

谷歌的gson类和json的转换包(1个):

gson-2.2.4.jar

下面是我自己导入的30个包

:(

antlr-2.7.6.jar
asm-attrs.jar
asm.jar
aspectjrt.jar
aspectjweaver.jar
cglib-2.1.3.jar
cglib-nodep-2.1_3.jar
commons-collections-2.1.1.jar
commons-dbcp.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.0.4.jar
commons-logging.jar
commons-pool.jar
dom4j-1.6.1.jar
ehcache-1.2.jar
ejb3-persistence.jar
freemarker-2.3.16.jar
gson-2.2.4.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate3.jar
javassist-3.7.ga.jar
jta.jar
mysql-connector-java-5.1.13.jar
ognl-3.0.jar
spring.jar
struts2-core-2.2.1.1.jar
struts2-spring-plugin-2.2.1.1.jar
xwork-core-2.2.1.1.jar

接下来是一个快乐的配置文件了:

web.xml中间配置的是我们的struts的过滤和spring的IoC的容器的放置的操作,如下的:

web.xml


http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
   

   
   
        contextConfigLocation
        classpath*:applicationContext*.xml
   

   
   
        org.springframework.web.context.ContextLoaderListener
   

   
   
        struts2
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
   

   
        struts2
        /*
   

   
   
        index.jsp
   
   
   

struts.xml中的配置是对于struts的action的配置,需要注意的是,接下来的操作中,我们的action是由spring进行代理操作生成的,那么其中的action对于的class的属性就需要做一个相应的转变了:

struts.xml


    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">


   
       


   
       
   


applicationContext.xml的配置是我们的配置中最繁琐的一个文件,其中的配置信息:

applicationContext.xml:


http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
   
   

   
   

   
   
       
       
       
       
       
   

   
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

       
       
           
                org.hibernate.dialect.MySQLDialect
               
                update
                true
                true
           
       


       
       

 


       
       
           
                com/fww/po/User.hbm.xml
                com/fww/po/Stu.hbm.xml
           
       

   

基本的javabean的类:

User和Stu类,以及其对于的hbm.xml:

user:

package com.fww.po;

public class User {

    private Integer id;
   
    private String name;
   
    private String pwd;

    public User() {
        super();
        // TODO Auto-generated constructor stub
    }

    public User(Integer id, String name, String pwd) {
        super();
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

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

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
   
}

user.hbm.xml:


    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

   

       
           
       
       
       


   

Stu是类似的操作,然后我们启动自己的项目之后,就可以在我们的数据库中观察到我们的数据库的表已经创建了。

 

 

如果使用的是hibernate的注解的操作的时候,struts.xml和web.xml适合上述的一直的。需要的applicationContext的配置的操作如下:

applicationContext.xml


http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
>
   
   

   
   

   
   
       
       
       
       
       
   

   
            class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

       
       
           
                org.hibernate.dialect.MySQLDialect
               
                update
                true
                true
           
       


       
       

 


       
       
       
       
           
                com.fww.po
           
       
       

   
   

   
       
   

我们的po类的创建如下:User

package com.fww.dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.fww.po.User;
@Component
public class UserDao {
    @Resource
    private HibernateTemplate hibernateTemplate;
   
   
    public void add(User user){
        hibernateTemplate.save(user);
    }
   
    public void del(int id){
        hibernateTemplate.delete(hibernateTemplate.get(User.class, id));
       
    }
   
    public List findAll(){
        return hibernateTemplate.find("from User");
    }
}

UserDao:

package com.fww.dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.fww.po.User;
@Component
public class UserDao {
    @Resource
    private HibernateTemplate hibernateTemplate;
   
   
    public void add(User user){
        hibernateTemplate.save(user);
    }
   
    public void del(int id){
        hibernateTemplate.delete(hibernateTemplate.get(User.class, id));
       
    }
   
    public List findAll(){
        return hibernateTemplate.find("from User");
    }
}

UserService:

package com.fww.service;

import java.util.List;

import com.fww.po.User;

public interface UserService {

    void add(User user);
   
    List findAll();
}

UserServiceImpl:

package com.fww.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.fww.dao.UserDao;
import com.fww.po.User;
import com.fww.service.UserService;
@Component
public class UserServiceImpl implements UserService{
   
    @Resource
    private UserDao userDao;
   
    public void add(User user){
        this.userDao.add(user);
    }

    @Override
    public List findAll() {
        return this.userDao.findAll();
    }
   
}

 

UserAction

package com.fww.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fww.po.User;
import com.fww.service.UserService;

@Component
@Scope("prototype")
public class UserAction {

    @Resource
    private UserService service;
    private User user;
    private List users ;
    public String add(){
        this.service.add(user);
        return "listAll";
    }

   
    public String findAll(){
        this.users = this.service.findAll();
        return "read";
    }
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }


    public List getUsers() {
        return users;
    }


    public void setUsers(List users) {
        this.users = users;
    }
}

可以进行一个适当的整理测试的操作:

package com.fww.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.fww.dao.UserDao;
import com.fww.po.User;

public class Test {
    public static void main(String[] args) {
        ApplicationContext ac =
                new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
        UserDao userDao = (UserDao) ac.getBean("userDao");
        User user = new User(1, "fff", "123");
        userDao.add(user);
       
    }
}

如此一个基本的三大框架的整合就完成了,在基本的数据的逻辑操作中,我们的action调用的是一个service的类,而我么的service需要使用其实现类serviceimpl进行一个调用,在serviceImpl中使用的是一个dao接口,我们的dao接口的实现是需要借助其daoImpl,daoImpl,在进行的就是调用到hibernate进行一个数据库的数据的调用,如此就是我们的一个三大框架的数据的流程。

你可能感兴趣的:(spring,Hibernate,struts2)