配置s2sh框架

工作到现在都是用现成的框架在进行开发,首次自己搭框架,记录一下步骤。

一、添加jar包

添加struts2、spring、hibernate相应的jar到lib下,这一步没什么好说的,copy/paste一下就ok了,如果没有自动导入的话,就右键选择一下jar包- Build path - 添加一下

二、在web.xml中配置spring和struts2

Spring和Struts2需要在web.xml中配置,配置如下

<!-- struts2 -->
<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  监听器-->
<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/classes/applicationContext.xml,classpath*:com/*/*Context.xml,classpath*:com/*/*/*Context.xml</param-value>
</context-param>
<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>  

配置完后可以直接在src下创建struts和spring的配置文件,如
- struts.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
    "http://struts.apache.org/dtds/struts-2.0.dtd"> 
<struts> 
    <package name="test" namespace="/" extends="struts-default">
        <action name="openTestPage" class="com.vipmangement.function.test.action.functionTestAction"
            method="testPage">
            <result name="success">/WEB-INF/page/test.jsp</result>
        </action>
    </package>
</struts> 
  • 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-2.5.xsd
">

<bean id="functionTestAction" class="ccom.vipmangement.function.test.action.functionTestAction" scope="prototype">
            <property name="test" ref="test"></property>
    </bean>
</beans>  

需要注意的是关于类的路径不要配错了。

hibernate

hibernate需要配置数据源,如我使用的是mysql,需要加入mysql-connection-java.jar这个包,然后在hibernate中创建一个hibernate.xml来配置数据源

<!-- 指定Hibernate配置文件的DTD信息 -->
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="connection.url">
<!--localhost/数据库名 -->
            jdbc:mysql://localhost/salevipmanagementsystem
        </property>
        <property name="connection.username">jarvis</property>
        <property name="connection.password">jarvis</property>
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="myeclipse.connection.profile">mysql</property>
  <mapping resource="com/vipmangement/function/test/bean/UserBean.hbm.xml"/> 
    </session-factory>
</hibernate-configuration>
  • UserBean.hbm.xml的内容为
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
   table是表名,schema是数据库名 底下的内容是bean中的变量和数据库字段对应
-->
<hibernate-mapping>
    <class name="com.vipmangement.function.test.bean.UserBean" table="TABLE" schema="salevipmanagementsystem">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
        </id>
        <property name="userName" type="java.lang.String">
            <column name="userid" length="20" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="pwd" length="50" />
        </property>
    </class>
</hibernate-mapping>

使用方法是创建一个sessionFactory

package com.vipmangement.function.test.action;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
 * Configures and provides access to Hibernate sessions, tied to the
 * current thread of execution.  Follows the Thread Local Session
 * pattern, see {@link http://hibernate.org/42.html }.
 */
public class HibernateSessionFactory {

    /** 
     * Location of hibernate.cfg.xml file.
     * Location should be on the classpath as Hibernate uses  
     * #resourceAsStream style lookup for its configuration file. 
     * The default classpath location of the hibernate config file is 
     * in the default package. Use #setConfigFile() to update 
     * the location of the configuration file for the current session.   
     */
    private static String CONFIG_FILE_LOCATION = "/hibernate.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private  static Configuration configuration = new Configuration();    
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

static {
    try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
    }
    private HibernateSessionFactory() {
    }
/**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     *  @return Session
     *  @throws HibernateException
     */
    public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

        return session;
    }

/**
     *  Rebuild hibernate session factory
     *
     */
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}

/**
     *  Close the single hibernate session instance.
     *
     *  @throws HibernateException
     */
    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);

        if (session != null) {
            session.close();
        }
    }

/**
     *  return session factory
     *
     */
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}

/**
     *  return session factory
     *
     * session factory will be rebuilded in the next call
     */
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}

/**
     *  return hibernate configuration
     *
     */
public static Configuration getConfiguration() {
return configuration;
}

}

通过创建的sessionfactory来调用,如

    public static void main(String[] args) {
        Session session=HibernateSessionFactory.getSession();
        Transaction ts=session.beginTransaction();

        Query query=session.createQuery("from UserBean where id=1");
        List list=query.list();
        UserBean kc1=(UserBean)list.get(0);
        System.out.println(kc1.getUserName());
        HibernateSessionFactory.closeSession();
    }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值