ssm 各层的配置文件

1、spring与mybatis整合的配置文件为springmvc-dao.xml,内容如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. https://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  12. <!--注解扫描包路径-->
  13. <context:component-scan base-package="com.test.dao"/>
  14. <!--开启注解配置-->
  15. <context:annotation-config/>
  16. <!--关联数据库文件-->
  17. <context:property-placeholder location="classpath:dbconfig.properties"/>
  18. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  19. <property name="driverClassName" value="${driverClass}"/>
  20. <property name="url" value="${jdbcUrl}"/>
  21. <property name="username" value="${username}"/>
  22. <property name="password" value="${password}"/>
  23. <!--c3p0连接池的私有属性-->
  24. <!--<property name="maxPoolSize" value="10"/>-->
  25. <!--<property name="minPoolSize" value="1"/>-->
  26. </bean>
  27. <!-- 3.配置SqlSessionFactory对象 -->
  28. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  29. <!-- 注入数据库连接池 -->
  30. <property name="dataSource" ref="dataSource"/>
  31. <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
  32. <property name="configLocation" value="classpath:mybatisConfig.xml"/>
  33. </bean>
  34. <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
  35. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  36. <!-- 注入sqlSessionFactory -->
  37. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  38. <!-- 给出需要扫描Dao接口包 -->
  39. <property name="basePackage" value="com.test.dao"/>
  40. </bean>
  41. </beans>

2、spring与service整合的配置文件:springmvc-service.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <!-- 扫描service相关的bean -->
  10. <context:component-scan base-package="com.test.service" />
  11. <!--<!– 配置事务管理器 –>-->
  12. <!--<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">-->
  13. <!--<!– 注入数据库连接池 –>-->
  14. <!--<property name="dataSource" ref="dataSource" />-->
  15. <!--</bean>-->
  16. </beans>

3、spring与controller整合的配置文件springmvc-servlet.xml

备注:

注意事项1:添加如下信息时,执行报如下500的错,将如下信息注释掉,访问正常,故先注释掉

注意事项2: 前缀WEB-INF/jsp/ 前一定要加 “/” ,,否则会报找不到文件404

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. https://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  12. <!--注解扫描包路径-->
  13. <context:component-scan base-package="com.jin.controller"/>
  14. <!--使用注解驱动,配置请求控制器和适配器-->
  15. <mvc:annotation-driven>
  16. <!--<mvc:message-converters register-defaults="true">-->
  17. <!--<bean class="org.springframework.http.converter.StringHttpMessageConverter">-->
  18. <!--<constructor-arg value="UTF-8"/>-->
  19. <!--</bean>-->
  20. <!--<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">-->
  21. <!--<property name="objectMapper">-->
  22. <!--<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">-->
  23. <!--<property name="failOnEmptyBeans" value="false"/>-->
  24. <!--</bean>-->
  25. <!--</property>-->
  26. <!--</bean>-->
  27. <!--</mvc:message-converters>-->
  28. </mvc:annotation-driven>
  29. <!--配置静态处理 css-->
  30. <mvc:default-servlet-handler/>
  31. <!--配置视图解析器-->
  32. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  33. <!--前缀-->
  34. <property name="prefix" value="/WEB-INF/jsp/" />
  35. <!--后缀-->
  36. <property name="suffix" value=".jsp" />
  37. </bean>
  38. </beans>

错误1:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xqajEyM18_size_16_color_FFFFFF_t_70

  1. <mvc:annotation-driven>
  2. <!--<mvc:message-converters register-defaults="true">--> <!--<bean class="org.springframework.http.converter.StringHttpMessageConverter">--> <!--<constructor-arg value="UTF-8"/>--> <!--</bean>--> <!--<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">--> <!--<property name="objectMapper">--> <!--<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">--> <!--<property name="failOnEmptyBeans" value="false"/>--> <!--</bean>--> <!--</property>--> <!--</bean>--> <!--</mvc:message-converters>-->
  3. </mvc:annotation-driven>

错误2:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xqajEyM18_size_16_color_FFFFFF_t_70 1


#

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. https://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  12. <!--注解扫描包路径-->
  13. <context:component-scan base-package="com.test.controller"/>
  14. <!--使用注解驱动,配置请求控制器和适配器-->
  15. <mvc:annotation-driven>
  16. <mvc:message-converters register-defaults="true">
  17. <bean class="org.springframework.http.converter.StringHttpMessageConverter">
  18. <constructor-arg value="UTF-8"/>
  19. </bean>
  20. <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  21. <property name="objectMapper">
  22. <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
  23. <property name="failOnEmptyBeans" value="false"/>
  24. </bean>
  25. </property>
  26. </bean>
  27. </mvc:message-converters>
  28. </mvc:annotation-driven>
  29. <!--配置静态处理 css-->
  30. <mvc:default-servlet-handler/>
  31. <!--配置视图解析器-->
  32. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  33. <!--前缀-->
  34. <property name="prefix" value="WEB-INF/jsp/" />
  35. <!--后缀-->
  36. <property name="suffix" value=".jsp" />
  37. </bean>
  38. </beans>

4、应用程序上下文:appllicationConfig.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. https://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  12. <import resource="classpath:springmvc-dao.xml"/>
  13. <import resource="classpath:springmvc-service.xml"/>
  14. <import resource="classpath:springmvc-servlet.xml"/>
  15. </beans>

5、web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <!--配置请求转发器-->
  7. <servlet>
  8. <servlet-name>springmvc-01</servlet-name>
  9. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10. <!--配置servlet加载文件-->
  11. <init-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>classpath:applicationConfig.xml</param-value>
  14. </init-param>
  15. <load-on-startup>1</load-on-startup>
  16. </servlet>
  17. <servlet-mapping>
  18. <servlet-name>springmvc-01</servlet-name>
  19. <url-pattern>/</url-pattern>
  20. </servlet-mapping>
  21. <!--字符集过滤-->
  22. <filter>
  23. <filter-name>encoding</filter-name>
  24. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  25. <init-param>
  26. <param-name>encoding</param-name>
  27. <param-value>utf-8</param-value>
  28. </init-param>
  29. </filter>
  30. <filter-mapping>
  31. <filter-name>encoding</filter-name>
  32. <url-pattern>/*</url-pattern>
  33. </filter-mapping>
  34. </web-app>

+++++++++++++++++++++++++++++++++++++

  1. mybatisConfig.xml
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration><!-- 配置 -->
  6. <properties resource="dbconfig.properties"></properties>
  7. <settings>
  8. <!--mybatis标准日志-->
  9. <setting name="logImpl" value="STDOUT_LOGGING"/>
  10. <!--指定日志输出格式,需配置properties-->
  11. <!--<setting name="logImpl" value="LOG4J"/>-->
  12. </settings>
  13. <typeAliases>
  14. <typeAlias type="com.jin.entity" alias="User"/>
  15. </typeAliases>
  16. <mappers>
  17. <mapper resource="com/test/dao/UserMapper.xml"/>
  18. </mappers><!-- 映射器 -->
  19. </configuration>

发表评论

表情:
评论列表 (有 0 条评论,72人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SSM配置文件

    首先 这篇文章暂时只对框架中所要用到的配置文件进行解释说明,而且是针对注解形式的,框架运转的具体流程过两天再进行总结. spring+springmvc+mybatis框架中