SpringMVC+Spring+MyBatis 整合与图片上传简单示例(4)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
           
           
            ">

<!-- 配置service扫描 -->
    <context:component-scan base-package="com.cenobitor.service"/>

</beans>

3、applicationContext_trans.xml(事务)的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
           
           
           
           
            ">

<!-- 事务管理器 -->
    <bean>
        <!--数据源-->
        <property ref="dataSource"/>
    </bean>
    <!-- 通知 -->
    <tx:advice transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method propagation="REQUIRED"/>
            <tx:method propagation="REQUIRED"/>
            <tx:method propagation="REQUIRED"/>
            <tx:method propagation="REQUIRED"/>
            <tx:method propagation="SUPPORTS" read-only="true"/>
            <tx:method propagation="SUPPORTS" read-only="true"/>
            <tx:method propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>

<!-- 切面 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.cenobitor.service.*.*(..))"/>
    </aop:config>
</beans>

(三)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
            "
        version="2.5">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

<!-- spring配置文件路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/spring/applicationContext_*.xml</param-value>
    </context-param>

<!-- 解决post乱码问题 -->
    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!-- 设置编码参是UTF8 -->
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

<!-- 配置SpringMvc 前端控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--指定SpringMvc 配置文件-->
        <!-- springmvc的配置文件默认路径是 /WEB-INF/${servlet-name}_servlet.xml -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

</web-app>

(四)MyBatis的配置文件,实体类和对应的映射文件由通用Mapper生成,在此采用手动创建方式(详细见下文件图片上传案例)

此处建文件,配置mybatis分页插件即可:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/26187803e665d2ebf6502bb16764c25c.html