您的位置:首页 > 编程语言 > Java开发

springmybatisspringmvc整合配置文件

2017-09-10 10:20 465 查看
放在src项目根目录

如图:


代码如下:

spring和mybatis整合:applicationContext-mybatis.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:context="http://www.springframework.org/schema/context"

    xmlns:aop="http://www.springframework.org/schema/aop"

      xmlns:tx="http://www.springframework.org/schema/tx"

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

        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byName">

        <!-- 1. 扫描包  @Service @Resource-->

        <context:component-scan base-package="com.yh.*.service.impl"></context:component-scan>

        <!-- 2. 加载属性文件 -->

        <context:property-placeholder location="classpath:db.properties"/>

        <!-- 3. 获取数据源 -->

        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="${jdbc.driver}"></property>

        <property name="url" value="${jdbc.url}"></property>

        <property name="password" value="${jdbc.password}"></property>

        <property name="username" value="${jdbc.username}"></property>

        </bean>

        <!-- 4. SQLSessionFactory实例 -->

        <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"></bean>

        <!-- 5. Mapper扫描器 -->

        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <property name="basePackage" value="com.yh.*.mapper"></property>

        <property name="sqlSessionFactoryBeanName" value="factory"></property>

        </bean>

        <!-- 6.事务管理器 -->

        <bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"></bean>

        <!-- 7.声明式事务 -->

        <tx:advice id="txAdvice" transaction-manager="txManage">

        <tx:attributes>

        <tx:method name="ins*"/>

        <tx:method name="save*"/>

        <tx:method name="upd*"/>

        <tx:method name="del*"/>

        <tx:method name="*" read-only="true"/>

        </tx:attributes>

        </tx:advice>

        <!-- 8.配置aop -->

        <aop:config>

        <aop:pointcut expression="execution(* com.yh.*.service.impl.*.*(..))" id="mypoint"/>

        <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>

        </aop:config>

</beans>
springmvc子容器:springmvc.xml  支持freemark
<beans xmlns="http://www.springframework.org/schema/beans"

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

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

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

        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 1.告诉SpringMVC的注解在哪里 -->

    <!-- 扫描控制器包,如果有spring扫描controller会导致父子容器问题,出现声明式事务无效,事务无法回滚 -->

    <context:component-scan base-package="com.yh.*.controller"></context:component-scan>

    <!-- 2.注解驱动,让注解生效 -->

    <mvc:annotation-driven/>

    <!-- 3.配置视图解析器,负责解析出真正的物理视图 ,分布式开发,需要写ID

        后台返回逻辑视图:index

        解析出真正的物理视图:前缀+逻辑视图+后缀===/WEB-INF/jsp/index+后缀.jsp-->

        <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        前缀

        <property name="prefix" value="/WEB-INF/jsp/"></property>

        后缀

        <property name="suffix" value=".jsp"></property>

        </bean> -->

      <!-- 配置freemarker模板文件前缀,模板文件编码 -->  

    <bean id="freeMarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/jsp/"></property>
<property name="defaultEncoding" value="UTF-8"></property>
<property name="freemarkerSettings">
<props>

            <prop key="template_update_delay">0</prop>  

            <prop key="default_encoding">UTF-8</prop>  

            <prop key="number_format">0.##########</prop>  

            <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>  

            <prop key="classic_compatible">true</prop>  

            <prop key="template_exception_handler">ignore</prop>  
</props>
</property>

</bean>
<!-- 配置freemakrer视图解析后缀,页面显示视图编码 -->
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="suffix" value=".ftl"></property>
<property name="contentType" value="text/html;charset=utf-8"></property>
</bean>

        

    <!-- 4.配置文件上传解析器Multipart解析器 -->

     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

      <property name="defaultEncoding" value="UTF-8"></property>

      </bean> 

    <!-- 如果碰见请求中/js/xx格式请求,去location配置的/js中去寻找, -->

    <!-- 如果没有配置mvc:resources去寻找@requestmapping -->

    <mvc:resources location="/WEB-INF/js/" mapping="/js/**"></mvc:resources>

    <mvc:resources location="/WEB-INF/images/" mapping="/images/**"></mvc:resources>

    <mvc:resources location="/WEB-INF/css/" mapping="/css/**"></mvc:resources>

    <mvc:resources location="/WEB-INF/jsp/" mapping="/jsp/**"></mvc:resources>

    <mvc:resources location="/WEB-INF/pic/" mapping="/pic/**"></mvc:resources>

    <!--5.拦截器 -->
<mvc:interceptors>
<!--多个拦截器,顺序执行 -->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.yh.filter.interceptor.HandlerInterceptor"></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.yh.filter.interceptor.HandlerInterceptor2"></bean>
</mvc:interceptor>
</mvc:interceptors>

    

</beans>
数据库连接文件:db.properties
jdbc.driver = com.mysql.jdbc.Driver

jdbc.url = jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8

jdbc.username = root

jdbc.password =root

#db.driver=oracle.jdbc.driver.OracleDriver

#db.url=jdbc:oracle:thin:@localhost:1521:orcl

#db.username=scott

#db.password=orcl
最后一个是日志支持文件:log4j.properties

# Set root category priority to INFO and its only appender to CONSOLE.

log4j.rootCategory=ERROR,LOGFILE

#log4j.rootCategory=INFO, CONSOLE, LOGFILE

log4j.logger.com.yh.spring.aop.MyBeforeLog=INFO

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

log4j.appender.CONSOLE.layout.ConversionPattern= %C %d{YYYY-MM-dd hh:mm:ss} %m %l %n

# LOGFILE is set to be a File appender using a PatternLayout.

log4j.appender.LOGFILE=org.apache.log4j.FileAppender

log4j.appender.LOGFILE.File=E:/login.log

log4j.appender.LOGFILE.Append=true

log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout

log4j.appender.LOGFILE.layout.ConversionPattern=%m %d{yyyy-MM-dd HH:mm:ss} %n
web.xml配置:

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

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

  <display-name>freemark</display-name>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

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

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

  <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>

  <servlet>

    <servlet-name>springmvc</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>

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

      <param-value>classpath:springmvc.xml</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet-mapping>

    <servlet-name>springmvc</servlet-name>

    <url-pattern>/</url-pattern>

  </servlet-mapping>

  <filter>

    <filter-name>charactesrEncodingFilter</filter-name>

    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

    <init-param>

      <param-name>encoding</param-name>

      <param-value>UTF-8</param-value>

    </init-param>

  </filter>

  <filter-mapping>

    <filter-name>charactesrEncodingFilter</filter-name>

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

  </filter-mapping>

</web-app>
jar包图:





ps:如果有不明白的,可以微信交流:FreeAndEasyYH
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息