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

SSM框架配置

2017-09-07 10:52 232 查看
spring-context.xml

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

  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.smd"></context:component-scan>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >  

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />  

        <property name="url" value="jdbc:mysql://localhost:3306/task828" />  

        <property name="username" value="root" />  

        <property name="password" value="" />

    </bean> 

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  

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

        <property name="dataSource" ref="dataSource" />  

        <!-- 自动扫描mapping.xml文件 -->  

        <property name="mapperLocations" value="classpath:com/smd/db/sql_xml/*.xml"></property>  

    </bean>  

 

  <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">

  <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>

  </bean>

 

    <!-- Mapper接口所在包名,Spring会自动查找其下的类 -->  

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

        <property name="basePackage" value="com.smd.db.entity_mapper" />  

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

    </bean>

    

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

</beans>

web.xml

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

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

  <display-name>SpringMVCDemo</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>

  <!-- 配置spring上下文  -->

  <context-param>

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

  <param-value>classpath:config/spring-context.xml</param-value>

  </context-param>

  <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>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

</web-app>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息