Struts2.3.4+Hibernate3.6+Spring3.0.5框架整合(新手看看)

 

工欲善其事,必先利其器,相关的下载就不多说,数据库是MySql5.1

新建一个WEB工程,将如下的包导入工程的WEB-INF/lib目录下,

Struts2.3.4+Hibernate3.6+Spring3.0.5框架整合(新手看看)

导入包的说明:struts2.3.4中因为使用c3p0连接数据库所以导入了c3p0.jar,其它的就是struts2.3.4的基本的包的导入,上图中的slf4j-nop-1.6.1.jar导不导入影响不大,然后导入Hibernate的核心包,以及Spring中dist目录下的所有包,去掉重复的包,保留较高的版本,结果如上图所示,如果你想使用struts2,Hibernate以及Spring的更多的功能,请导入相关的包。

下面是web.xml文件的配置

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>

 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

 3   <display-name>SSHFramework</display-name>

 4   

 5   <!-- 加载Spring的配置文件 -->

 6   <context-param>

 7       <param-name>contextConfigLocation</param-name>

 8       <param-value>/WEB-INF/config/applicationContext.xml</param-value>

 9   </context-param>

10   <!-- 加载spring的监听器 -->

11   <listener>

12     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

13   </listener>

14  

15   <!-- struts2的配置 -->

16   <filter>

17       <filter-name>struts2</filter-name>

18       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

19       <init-param>

20           <param-name>config</param-name>

21           <param-value>struts-default.xml,struts-plugin.xml,../config/struts.xml</param-value>

22       </init-param>

23   </filter>

24   <filter-mapping>

25       <filter-name>struts2</filter-name>

26       <url-pattern>/*</url-pattern>

27   </filter-mapping>

28   <welcome-file-list>

29     <welcome-file>index.jsp</welcome-file>

30   </welcome-file-list>

31 </web-app>

接着是spring的配置文件applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>   

 2 <beans xmlns="http://www.springframework.org/schema/beans"  

 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

 4        xsi:schemaLocation="http://www.springframework.org/schema/beans   

 5                           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 6                         

 7     

 8     <!-- 定义数据源 ,使用c3p0数据源实现-->

 9     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

10         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>

11         <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>

12         <property name="user" value="root"></property>

13         <property name="password" value="123456"></property>

14     </bean>

15     <!-- Hibernate sessionFactory创建 -->

16     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

17         <property name="dataSource" ref="dataSource"></property>

18         <!-- Hibernate属性 -->

19         <property name="hibernateProperties">

20             <props>

21                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

22                 <prop key="hibernate.format_sql">true</prop>

23                 <prop key="hibernate.hbm2ddl.auto">create</prop> 

24             </props>

25         </property>

26         <!-- 映射文件 -->

27         <property name="mappingResources">

28             <list>

29                 <!-- 统一到WEB-INF/config下管理配置文件 -->

30                 <value>../config/User.hbm.xml</value>

31             </list>

32         </property>

33     </bean>

34     <bean id="userDao" class="cn.zj.qiao.spring.dao.userimpl.UserDaoImpl" scope="prototype">

35         <property name="sessionFactory" ref="sessionFactory"></property>

36     </bean>

37     <bean id="registerAction" class="cn.zj.qiao.actions.RegisterAction" scope="prototype">

38         <property name="ud" ref="userDao"></property>

39     </bean>

40 </beans>

Hibernate的映射文件User.hbm.xml

 1 <?xml version="1.0" encoding="UTF-8"?>

 2 <!DOCTYPE hibernate-mapping  PUBLIC

 3                              "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

 4                              "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 5 <hibernate-mapping>

 6     <class name="cn.zj.qiao.entity.vo.User" table="user_table">

 7         <id name="id" column="uid">

 8             <generator class="identity"></generator>

 9         </id>

10         <property name="uname"></property>

11         <property name="upass"></property>

12     </class>

13 </hibernate-mapping>

strusts.xml文件

 1 <?xml version="1.0" encoding="UTF-8" ?>

 2 <!DOCTYPE struts PUBLIC

 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

 4     "http://struts.apache.org/dtds/struts-2.3.dtd">

 5 <struts>

 6     <package name="default" namespace="/" extends="struts-default">

 7         <action name="register" class="registerAction">

 8             <result name="success">/success.jsp</result>

 9             <result name="error">/error.jsp</result>

10         </action>

11     </package>

12 </struts>

用户注册Action:RegisterAction

 1 package cn.zj.qiao.actions;

 2 

 3 import cn.zj.qiao.entity.vo.User;

 4 import cn.zj.qiao.interfaces.UserDao;

 5 

 6 import com.opensymphony.xwork2.Action;

 7 

 8 

 9 public class RegisterAction implements Action{

10     private String uname;

11     private String upass;

12     private UserDao ud;

13     

14     public UserDao getUd(){

15         return ud;

16     }

17 

18     public void setUd(UserDao ud){

19         this.ud = ud;

20     }

21 

22     public String getUname() {

23         return uname;

24     }

25 

26 

27     public void setUname(String uname) {

28         this.uname = uname;

29     }

30 

31 

32     public String getUpass() {

33         return upass;

34     }

35 

36 

37     public void setUpass(String upass) {

38         this.upass = upass;

39     }

40 

41 

42     @Override

43     public String execute() throws Exception {

44         User user = new User();

45         user.setUname(uname);

46         user.setUpass(upass);

47         ud.addUser(user);

48         return SUCCESS;

49     }

50 

51 }

用户Bean:User

 1 package cn.zj.qiao.entity.vo;

 2 public class User {

 3     

 4     private Long id;

 5     private String uname;

 6     private String upass;

 7     

 8     public User() {

 9         super();

10     }

11     public User(Long id, String uname, String upass) {

12         super();

13         this.id = id;

14         this.uname = uname;

15         this.upass = upass;

16     }

17     public Long getId() {

18         return id;

19     }

20     public void setId(Long id) {

21         this.id = id;

22     }

23     public String getUname() {

24         return uname;

25     }

26     public void setUname(String uname) {

27         this.uname = uname;

28     }

29     public String getUpass() {

30         return upass;

31     }

32     public void setUpass(String upass) {

33         this.upass = upass;

34     }

35     

36 }

数据库Dao 接口:UserDao

package cn.zj.qiao.interfaces;



import cn.zj.qiao.entity.vo.User;





public interface UserDao {

    public Long addUser(User user);



}

Dao的实现类:UserDaoimpl

package cn.zj.qiao.spring.dao.userimpl;



import org.hibernate.SessionFactory;

import org.springframework.orm.hibernate3.HibernateTemplate;



import cn.zj.qiao.entity.vo.User;

import cn.zj.qiao.interfaces.UserDao;



public class UserDaoImpl implements UserDao {

    

    private HibernateTemplate ht = null;

    private SessionFactory sessionFactory;

    

    public void setSessionFactory(SessionFactory sessionFactory){

        this.sessionFactory = sessionFactory;

    }

    public SessionFactory getSessionFactory(){

        return sessionFactory;

    }

    

    public HibernateTemplate getHibernateTemplate(){

        if(ht == null){

            ht = new HibernateTemplate(sessionFactory);

        }

        return ht;

    }

    @Override

    public Long addUser(User user) {

        return (Long)getHibernateTemplate().save(user);

    }

}

相关的jsp页面;

index.jsp:用户注册

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>用户注册</title>

</head>

<body>

    <form action="register.action" method="post">

        用户名:<input type="text" name="uname"/>&nbsp;&nbsp;码:<input type="password" name="upass"/>

        <input type="submit" value="注册"/>

    </form>

</body>

</html>

显示结果的页面:success.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"

 2     pageEncoding="UTF-8"%>

 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

 4 <html>

 5 <head>

 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

 7 <title>success</title>

 8 </head>

 9 <body>

10 注册成功

11 </body>

12 </html>

 

最后说明:包的导入情况没有细说,如果出现异常,可以根据异常信息自己百度神马的酌情加入或者删去一些包,此工程的配置文件除web.xml外,其它均放在WEB-INF/config目录下,此工程只是简单的测试一下框架的整合情况,所以只是前台一个jsp填写信息,然后写入数据库这样简单的逻辑。

最后希望路过的各位大神,给予宝贵的学习SSH框架的意见及建议。。。

你可能感兴趣的:(hibernate3)