常用框架配置文件

SSM(Spring+SpringMVC+MyBatis)的配置文件:

springmvc.xml文件配置(解析html页面):

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
       default-autowire="byName">

    <context:component-scan base-package="com.accp"/>

    <!-- 使用thymeleaf解析   Begin-->
    <bean id="templateResolver"
          class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
        <property name="prefix" value="/WEB-INF/html/"/>
        <property name="suffix" value=".html"/>
        <property name="templateMode" value="HTML"/>
        <property name="cacheable" value="false"/>
        <!--解决中文乱码-->
        <property name="characterEncoding" value="UTF-8"/>
    </bean>
    <bean id="templateEngine"
          class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver"/>
    </bean>
    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine"/>
        <!--解决中文乱码-->
        <property name="characterEncoding" value="UTF-8"/>
    </bean>
    <!-- 使用thymeleaf解析   end-->

    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>

    <mvc:resources mapping="/statics/css/**" location="/statics/css/"/>
    <mvc:resources mapping="/statics/fonts/**" location="/statics/fonts/"/>
    <mvc:resources mapping="/statics/images/**" location="/statics/images/"/>
    <mvc:resources mapping="/statics/js/**" location="/statics/js/"/>
    <mvc:resources mapping="/statics/localcss/**" location="/statics/localcss/"/>
    <mvc:resources mapping="/statics/localjs/**" location="/statics/localjs/"/>

    <import resource="spring-dao.xml"/>

    <!--文件上件必备2-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="5000000"/> <!--单位字节-->
        <property name="maxInMemorySize" value="4096"/>
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>

    <!--外部数据连接池-->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:db.properties"/>
    </bean>

    <!--数据库连接-->
    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${db_url}"/>
        <property name="driverClassName" value="${db_driverClassName}"/>
        <property name="username" value="${db_username}"/>
        <property name="password" value="${db_password}"/>
    </bean>

    <!--映射xml-->
    <bean name="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:typeAliasesPackage="com.accp.entity">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:xml/*.xml"/>
        <property name="configLocation" value="classpath:mybatis.xml"/>
    </bean>

    <!--相当于StudentinfoDao studentinfoDao = session.getMapper(StudentinfoDao.class) 获取-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
        <property name="basePackage" value="com.accp.dao"/>
    </bean>

    <!--声明式事务-->
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="update*"/>
            <tx:method name="delete*"/>
            <tx:method name="query*" propagation="NOT_SUPPORTED"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.accp.service..*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
    </aop:config>

</beans>

springmvc.xml文件配置(解析jsp页面):

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
       default-autowire="byName">

    <context:component-scan base-package="com.accp"/>

    <!--视图解析-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--文件上件必备2-->
    <bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="5000000" /> <!--单位字节-->
        <property name="defaultEncoding" value="UTF-8" />
    </bean>


    <mvc:annotation-driven/>

    <!--静态页面放过-->
    <!--<mvc:default-servlet-handler/>-->

    <mvc:resources mapping="/calendar/**" location="/calendar/"/>
    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/images/**" location="/images/"/>
    <mvc:resources mapping="/js/**" location="/js/"/>

    <import resource="spring-dao.xml"/>

    <!--拦截器配置-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/UserMain/JumpLogin"/>
            <mvc:exclude-mapping path="/UserMain/OutLogin"/>
            <mvc:exclude-mapping path="/UserMain/Login"/>
            <bean class="com.accp.interceptor.MyInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>


    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:db.properties"/>
    </bean>

    <!--数据库连接-->
    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${db_url}"/>
        <property name="driverClassName" value="${db_driverClassName}"/>
        <property name="username" value="${db_username}"/>
        <property name="password" value="${db_password}"/>
    </bean>

    <!--映射xml-->
    <bean name="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:typeAliasesPackage="com.accp.entity">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:xml/*.xml"/>
        <property name="configLocation" value="classpath:mybatis.xml"/>
    </bean>

    <!--相当于StudentinfoDao studentinfoDao = session.getMapper(StudentinfoDao.class) 获取-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
        <property name="basePackage" value="com.accp.dao"/>
    </bean>

    <!--声明式事务-->
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="update*"/>
            <tx:method name="delete*"/>
            <tx:method name="query*" propagation="NOT_SUPPORTED"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.accp.service..*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
    </aop:config>

</beans>

db.properties文件配置:

#连接数据库的地址
db_url=jdbc:mysql://localhost:3306/appinfodb?useUnicode=true&characterEncoding=utf-8
#驱动类
db_driverClassName=com.mysql.jdbc.Driver
#数据库用户名
db_username=root
#数据库密码
db_password=994994

SSH(Spring+Struts2+Hibernate)的配置文件:

XXX.hbm.xml(hibernate映射文件)文件配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.accp.entity">
    <class name="BizCheckResult" table="BIZ_CHECK_RESULT">
        <!--主键,用序列增-->
        <id name="id">
            <generator class="sequence">
                <param name="sequence">SEQ_CHECK_RESULT</param>
            </generator>
        </id>
        
        <!--字段属性-->
        <property name="claimId" column="CLAIM_ID"/>
        <property name="checkTime" column="CHECK_TIME"/>
        <property name="checkerSn" column="CHECKER_SN"/>
        <property name="result"/>
        <property name="comm"/>
    </class>
</hibernate-mapping>

Hibernate.cfg.xml(Hibernate核心配置文件)文件配置:

<?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="dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.username">scott</property>
        <property name="connection.password">orcl</property>
        <property name="show_sql">true</property>
        <property name="current_session_context_class">thread</property>

        <!--配置需要映射的文件-->
        <mapping resource="xml/BizCheckResult.hbm.xml"/>
        <mapping resource="xml/BizClaimVoucher.hbm.xml"/>
        <mapping resource="xml/BizClaimVoucherDetail.hbm.xml"/>
        <mapping resource="xml/SysDepartment.hbm.xml"/>
        <mapping resource="xml/SysEmployee.hbm.xml"/>
        <mapping resource="xml/SysPosition.hbm.xml"/>
        <mapping resource="xml/BizLeave.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Struts.xml(Struts 2核心配置文件)文件配置:

<?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>
    <constant name="struts.ui.theme" value="simple" />
    <package name="web" extends="struts-default">

        <!--拦截器-->
        <interceptors>
            <interceptor name="myInteceptor" class="com.accp.action.MyInteceptor"/>
            <interceptor-stack name="myStack">
                <interceptor-ref name="myInteceptor"/>
                <interceptor-ref name="defaultStack" />
            </interceptor-stack>
        </interceptors>

        <!--配置action跳转页面-->
        <action name="login" class="com.accp.action.SysEmployeeAction" method="login">
            <result name="success">index.jsp</result>
        </action>

        <action name="addClaimVoucher" class="com.accp.action.BizClaimVoucherAction" method="addBizClaimVoucher">
            <result name="success" type="redirectAction">queryClaimVoucher.action</result>
            <result name="input">index.jsp</result>
        </action>

        
    </package>

</struts>

WEB-INF下web.xml(配置web项目启动时加载的信息)文件配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <!--延迟加载操作-->
    <filter>
        <filter-name>openSessionInView</filter-name>
        <filter-class>com.accp.util.MyFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInView</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>

    <!--struts过滤器-->
    <filter>
        <filter-name>strut2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>strut2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--项目启动首页面-->
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
</web-app>

HibernateUtil(工具类,避免了一直创建重复代码,提高了代码复用性)文件配置:

package com.accp.util;

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

public class HibernateUtil {
    private static SessionFactory sessionFactory ;

    static {
        Configuration conf = new Configuration().configure("hibernate.cfg.xml");
        sessionFactory = conf.buildSessionFactory();
    }

    public static Session openSession(){
        return sessionFactory.getCurrentSession();
    }
}

MyFilter文件(过滤器拦截事务)配置:

package com.accp.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import javax.servlet.*;
import java.io.IOException;


public class MyFilter implements Filter {


    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");

        Session session = HibernateUtil.openSession();
        Transaction transaction = session.beginTransaction();
        try {
            chain.doFilter(request,response);
            transaction.commit();
        }catch (Exception e){
            transaction.rollback();
        }

    }

    public void destroy() {

    }
}

SpringBoot的配置文件:

application.yml文件配置:

spring:
  #连接数据库
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
    username: root
    password: 994994
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

#端口号
server:
  port: 8080

#映射文件配置,取别名
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.accp.springbootdemo1.entity

猜你喜欢

转载自blog.csdn.net/weixin_41595700/article/details/85640654
今日推荐