maven项目里的ssh框架整合

1、先整合struts和hibernate

导入相关的jar包

编写hibernate的配置文件

<!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="dialect">org.hibernate.dialect.MySQLDialect</property>
        
       <property name="show_sql">true</property> <mapping class="com.blb.entity.User"/> </session-factory> </hibernate-configuration>

在web.xml文件里为struts配置过滤器

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

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

编写log4j2的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="com.opensymphony.xwork2" level="info"/>
        <Logger name="org.apache.struts2" level="debug"/>
        <Root level="warn">
            <AppenderRef ref="STDOUT"/>
        </Root>
    </Loggers>
</Configuration>

 

编写struts配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>

    <constant name="struts.devMode" value="true" />
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    
    <package name="basicstruts2" extends="struts-default,json-default" strict-method-invocation="false">
        <action name="test" class="com.blb.action.TestAction">
            <result name="success">/index.jsp</result>
        </action>
    </package>
</struts>

写一个User实体类用来测试

编写实体类的hibernate映射文件

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
<hibernate-mapping>
    <class name="com.blb.entity.User" table="t_user">
        <id name="id">
            <generator class="uuid"></generator>
        </id>
        
        <property name="username"></property>
        <property name="password"></property>
    </class>
</hibernate-mapping>

 

2、整合spring

导入相关的jar包

 

因为struts2-spring-plugin的jar包里含有了spring和struts的jar包,所以不需要再导入spring的jar包,前面导入的struts的jar包也可以删除了。

编写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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        https://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx.xsd
                        http://www.springframework.org/schema/aop 
                         https://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <context:component-scan base-package="com.blb" />
            
        <!-- Hikari Datasource 数据源-->
     <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" >
      <!-- <property name="driverClassName" value="${db.driverClass}" /> --> <!-- 无需指定,除非系统无法自动识别 -->
      <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/menu?useUnicode=true&amp;characterEncoding=UTF-8" />
      <property name="username" value="root" />
      <property name="password" value="root" />
       <!-- 连接只读数据库时配置为true, 保证安全 -->
      <property name="readOnly" value="false" />
      <!-- 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30秒 -->
      <property name="connectionTimeout" value="30000" />
      <!-- 一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟 -->
      <property name="idleTimeout" value="600000" />
      <!-- 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL wait_timeout参数(show variables like '%timeout%';) -->
      <property name="maxLifetime" value="1800000" />
      <!-- 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count) -->
      <property name="maximumPoolSize" value="15" />
     </bean>
    
     <bean id="mySessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocations" value="hibernate.cfg.xml"/>
    </bean>
</beans>

使用了Hikari Datasource 数据源,所以导入相关的jar包

然后在web.xml文件里配置spring监听器

  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

最后编写测试代码

public String select(){
        Session session = HibernateUtil.getSession();
        Transaction transaction = session.beginTransaction();
        
        Query<User> query = session.createQuery("from User");
        List<User> list = query.list();
        for (User user : list) {
            System.out.println(user);
        }
        return SUCCESS;
    }

运行成功后会打印出数据库menu里t_user表的数据

 

posted @ 2019-07-03 20:00  Mi1udeer  阅读(254)  评论(0编辑  收藏  举报