IDEA搭建struts2+hibernate5+spring4

一:新建工程:  选择Spring,Web Application, Struts2, Application Server(选择tomcat就可以了),Hibernate;等待IDEA下载依赖包

二:spring的引入与测试:

测试的包:src下面新建test包

TestService.java

package test;
public class TestService {  
    private String name;  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public String getName() {  
  
        return name;  
    }  
  
    public void hello()  
    {  
        System.out.println("hello "+ getName());  
    }  
}  

 第一个配置文件(applicationContext.xml)web/Web-INF目录下新建spring的配置文件applicationContext.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 class="test.TestService" id="testService">  
                  <property name="name" value="spring"></property>  
           </bean>  
    </beans>  

 Test.java

package test;  
  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.FileSystemXmlApplicationContext;  
  
public class Test {  
    public static void main(String[] args)  
    {  
        ApplicationContext ac = new FileSystemXmlApplicationContext("web/WEB-INF/applicationContext.xml");  
        TestService ts = (TestService)ac.getBean("testService");  
        ts.hello();  
    }  

 直接测试Test的main函数,操作是右击编辑区,点击Run "Test.main()",正常的话,应该会出现如下效果:

三:Hibernate的引入与测试:

1>要额外引入一些依赖:mysql-connector-java-5.1.26,commons-dbcp2-2.1,commons-pool2-2.4.1;直接使用IDEA的maven功能下载依赖包,然后直接引入:File->project Structure->Libraries->点击右侧的加号->From maven; 然后输入对应的包名就可以了;

2>使用IDEA的persistence下的OR映射功能;       (a)建立数据库

    (b)需要先配置applicationContext.xml,让其接管Hibernate的配置;如果没有进行这一步的话,在Persistence界面是不会出现sessionFactory的。

配置内容:

    <!-- data connection -->  
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">  
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
        <property name="url" value="jdbc:mysql://localhost:3306/TESTDB"></property>  
        <property name="username" value="root"/>  
        <property name="password" value="root"/>  
    </bean>  
      
      
    <!-- session factory -->  
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
        <property name="dataSource" ref="dataSource"></property>  
        <property name="hibernateProperties">  
            <props>  
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
                <prop key="hibernate.show_sql">true</prop>  
                <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/TESTDB</prop>  
                <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>  
            </props>  
        </property>  
    </bean>  

    (c)进行持久化类生成:右击sessionFactory->Generate Persistence Mapping

选择我们建好的数据库:

  (d)新建dao层和daoImpl层:

  

package dao
public interface UserDao {  
    public void addUser(User user);  
}  

   sessionFactory注入dao层

    package dao;  
      
      
    import bean.User;  
    import org.hibernate.Session;  
    import org.hibernate.SessionFactory;  
    import org.hibernate.Transaction;  
    import org.springframework.beans.factory.annotation.Autowired;  
    import org.springframework.beans.factory.annotation.Qualifier;  
      

    public class UserDaoImpl implements UserDao {  
      
        @Qualifier("sessionFactory")  
        @Autowired  
        private SessionFactory sessionFactory;  
      
        @Override  
        public void addUser(User user) {  
            Session s = sessionFactory.openSession();  
            Transaction tx= s.beginTransaction();  
            s.save(user);  
            tx.commit();  
        }  
      
        public void setSessionFactory(SessionFactory sessionFactory) {  
            this.sessionFactory = sessionFactory;  
        }  
    }  

 (e)新建service层和serviceImpl层:

  

package service

public interface UserService{ 
public void addUser(User user);

}

 dao层注入service层:

package service;

public class UserServiceImpl implements UserService{

private UserDao userDao;

public void setUserDao(UserDao userDao){
  this.userDao = userDao;
}

@override
 
public void addUser(User user){
  
 userDao.addUser(user);
}

}

 让Spring接管Hibernate的配置文件:

    <?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 class="test.TestService" id="testService">  
            <property name="name" value="spring"></property>  
        </bean>  
      
        <!--配置数据源 -->  
        <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">  
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
            <property name="url" value="jdbc:mysql://localhost:3306/user"></property>  
            <property name="username" value="root"/>  
            <property name="password" value="root"/>  
        </bean>  
      
      
        <!-- sessionFactory -->  
        <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.MySQLDialect</prop>  
                    <prop key="hibernate.show_sql">true</prop>  
                    <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/user</prop>  
                    <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>  
                </props>  
            </property>
           <!--配置映射文件-->  
            <property name="mappingLocations">  
                <list>  
                    <value>classpath:bean/User.hbm.xml</value>  
                </list>  
            </property>  
        </bean>  
      
      
        <!--hibernate测试加入-->  
        <bean id="userDao" class="dao.UserDaoImpl">  
            <property name="sessionFactory" ref="sessionFactory"></property>  
        </bean>  
      
    </beans>  

 进行测试:Test.java

public class Test {  
    public static void main(String[] args)  
    {  
        ApplicationContext ac = new FileSystemXmlApplicationContext("web/WEB-INF/applicationContext.xml");  
        UserDao userDao = (UserDaoImpl)ac.getBean("userDao");  
        User user = new User();  
        user.setName("kaka");  
        user.setPhone("123456123");  
        userDao.addUser(user);  
    }  
}  

 运行:

查看MySQL数据库,看是否增加了一条记录

三:struts2的引入与测试:

引入两个依赖:struts2-spring-plugin-2.3.24,spring-web-3.0.5和log4j-1.2.14.jar;选择手工加入;先如下下载好,"Download to "不要打勾:

下载struts2-spring-plugin-2.3.24会附带下载好spring-web-3.0.5; 下载完后查看下下载的目录, 只是查看下下载目录就可以了如下:

直接复制这两个包,放到项目的lib文件下,之后右击一下lib下的刚引入的这两个jar包,"Add as Library";对于log4j-1.2.14就相对简单,直接打勾Download to 就可以了.相关截图如下:

 第三个配置文件:修改web/WEB-INF/web.xml文件,引入struts和spring:

<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>  
  
    <!--spring的监听器配置开始-->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>WEB-INF/applicationContext.xml</param-value>  
    </context-param>  

 新建Action在struts.xml中配置:

第四个配置文件: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>  
    <!-- struts的action配置文件 -->  
    <!-- 将action托管给spring -->  
    <constant name="struts.objectFactory" value="spring" />  
  
    <!-- 所有的action都应该放在对应的package下 -->  
    <!--action的class属性不再是具体的类,而是spring配置文件配置的bean id-->  
    <package name="user" namespace="/user" extends="struts-default">  
        <action name="add" class="userAction" method="add">  
            <result name="success">/success.jsp</result>  
        </action>  
    </package>  
</struts>

最后!!!

所有类的实例都由spring去管理(applicationContext.xml)

 

<bean id="myIndexAction" class="ssh.action.UserAction" scope="prototype">
    <property name="userService" ref="userService"/>
 </bean>

<bean id="userService" class="ssh.service.UserServiceImpl" scope="prototype">
    <property name="userDao" ref="userDao"/>
</bean>

<bean id="userDao" class="ssh.dao.UserDaoImpl" scope="prototype">
       <property name="sessionFactory" ref="sessionFactory" />
   </bean>
<!--前面已经注册过sessionFactory-->

 配置流程详见:http://blog.csdn.net/sysushui/article/details/68937005

 请求传递过程分析详见:http://www.cnblogs.com/laibin/p/5847111.html

猜你喜欢

转载自xiaotiger.iteye.com/blog/2374232