美文网首页
SSM(springmvc+spring+mybatis)整合配

SSM(springmvc+spring+mybatis)整合配

作者: tingshuo123 | 来源:发表于2018-07-27 23:09 被阅读169次

    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">
    
        <!-- ===================== SpringMVC配置 ===================== -->
        <!-- 配置SpringMVC前端控制器,负责处理所有请求 -->
        <servlet>
            <servlet-name>springDispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 加载配置文件 -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:config/springmvcConfig.xml</param-value>
            </init-param>
            <!-- 随容器启动 -->
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <!-- 将所有以 .action 结束请求,交给springMVC前端控制器处理 -->
        <servlet-mapping>
            <servlet-name>springDispatcherServlet</servlet-name>
            <url-pattern>*.action</url-pattern>
        </servlet-mapping>
    
    
    
    
        <!-- ===================== Spring配置 ===================== -->
        <!-- 配置spring环境 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:config/springConfig.xml</param-value>
        </context-param>
    
        <!-- 使用过滤器加载spring配置文件 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
    
        <!-- 注册编码过滤器 -->
        <filter>
            <filter-name>charset</filter-name>
            <filter-class>com.project.filter.CharsetFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>charset</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    </web-app>
    

    springConfig.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:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        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/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
            http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <!-- 开启自动扫描组件 -->
        <context:component-scan
            base-package="com.project.service"></context:component-scan>
    
        <!-- ===================整合Mybatis=================== -->
        <!-- 配置数据库环境 -->
        <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <!-- mysql url -->
            <property name="url"
                value="jdbc:mysql://localhost:3306/mybaits"></property>
            <!-- 驱动 -->
            <property name="driverClassName"
                value="com.mysql.jdbc.Driver"></property>
            <!-- 用户名密码 -->
            <property name="username" value="root"></property>
            <property name="password" value=""></property>
        </bean>
    
        <!-- 创建数据库映射器,扫描包下所有接口,并为其创建动态代理类 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.project.dao" />
        </bean>
    
        <!-- 配置 Mybatis 的 SqlSessionFactory -->
        <bean id="sqlSessionFactory"
            class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 指定数据库环境 -->
            <property name="dataSource" ref="dataSource"></property>
            <!-- 指定Mybatis配置文件 -->
            <property name="configLocation"
                value="classpath:config/mybatisConfig.xml"></property>
            <!-- 指定Mybatis映射文件,*.xml 会匹配所有xml文件 -->
            <!-- <property name="mapperLocations" value="classpath:com/project/mapper/*.xml"></property> -->
        </bean>
    
        <!-- 创建事务管理器:针对jdbc或Mybatis的事务管理器 -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!-- 指定环境, name的dataSource是固定写法,ref是前面创建的环境id -->
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    
    </beans>
    
    

    springmvcConfig.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:aop="http://www.springframework.org/schema/aop"
        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.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/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"
        > 
        
        <!-- spring包扫描功能 -->
        <context:component-scan base-package="com.project.action"/>
        
        <!-- 配置注解映射处理器 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
    
        <!-- 配置注解处理器适配器 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
    
        <!-- 配置视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 设置视图前缀、后缀 -->
            <property name="prefix" value="jsp/"></property>
        <!--    <property name="suffix" value=".jsp"></property> -->
        </bean>
    </beans>
    

    mabatisConfig.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    
        <settings>
            <!-- 配置log4j日志信息 -->
            <setting name="logImpl" value="LOG4J"></setting>
        </settings>
        
        <!-- 别名 -->
        <typeAliases>
            <typeAlias type="com.project.bean.UserBean" alias="UserBean" />
        </typeAliases>
    
        <!-- 加载映射文件,也可在 spring 配置文件中加载 -->
    <!--    <mappers>
            <mapper resource="com/project/mapper/UserMapper.xml" />
        </mappers> -->
    </configuration>
    

    相关文章

      网友评论

          本文标题:SSM(springmvc+spring+mybatis)整合配

          本文链接:https://www.haomeiwen.com/subject/qpdnmftx.html