Spring整合Struts2和Hibernate+Maven(二)之SSH的配置文件

上次讲到的是maven项目的创建以及pom.xml的配置。
这里推荐一个网站:maven整合jar包,这里可以查询并生成配置文件中jar包导入格式的文本,复制粘贴到pom.xml中即可由idea自动下载并导入项目。

resources文件夹

创建ssh的配置文件至上章图片中的resources文件夹中,分为三个文件
hibernate.cfg.xml、spring-hibernate.xml、struts.xml。

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:111/111?useUnicode=true&amp;characterEncoding=utf-8</property>
        <property name="connection.username">111</property>
        <property name="connection.password">111</property>
        <!--最大最小连接池-->
        <property name="hibernate.c3p0.max_size">20</property>
        <property name="hibernate.c3p0.min_size">1</property>
        <property name="hibernate.c3p0.timeout">5000</property>
        <property name="hbm2ddl.auto">update</property>
    </session-factory>
</hibernate-configuration>

配置文件连接时加characterEncoding=utf-8是解决编码问题,同时

<property name="hbm2ddl.auto">update</property>

这个操作启动的时候会去检查schema是否一致,如果不一致会做scheme更新。

加入该配置文件后,File->Project Structure ->Modules的项目名称下加入Spring 、 Hibernate和在Web下加入Struts2,结构如图所示: Modules结构
在配置好上面所说的3个配置文件后,idea会提示导入该配置,点击即可。

Spring整合Hibernate之spring-hibernate.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       ">
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation"
                  value="hibernate.cfg.xml">
        </property>
        <property name="hibernateProperties" >
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
                hibernate.hbm2ddl.auto=update
            </value>
        </property>
    </bean>
</beans>

sessionFactory:Spring配置文件的bean的名称。
org.springframework.orm.hibernate4.LocalSessionFactoryBean:class文件,之前已在pom.xml导入该class文件所在的jar包。
配置好后在以后需要使用Hibernate时只需在该工厂模式中‘拿’即可。

struts.xml

首先看配置文件

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

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
        "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
    <constant name="struts.devMode" value="false"/>
    <include file="struts-default.xml"/>
    <package name="applyRoom" extends="struts-default" >
        <action name="Login_*" class="" method="{1}">
            <result name="success">index.jsp</result>
            <result name="error">index.jsp</result>
        </action>
    </package>
</struts>

该配置文件并无其他,唯一区别即使用请求通配符。关于Spring整合Struts2之后会详细介绍。

猜你喜欢

转载自blog.csdn.net/pckonline/article/details/69948748