码迷,mamicode.com
首页 > 编程语言 > 详细

集成SpringMVC, Spring, Mybatis环境

时间:2018-10-11 01:42:10      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:数据   driver   数据库连接   factory   use   source   视图   not   frame   

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
  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"
  >
  <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:applicationContext.xml</param-value>
    </context-param> <!-- ContextLoadListener常用于加载Spring的配置 -->
   
    <listener>
     <listener-class>org.springframework.web.servlet.DispatcherServlet</listener-class>
    </listener>
   
    <!-- 配置DispatchcerServlet -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>             <!-- 而DispatcherSerclet用来加载SpringMVC的配置 -->
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:dispatcher-servlet.xml</param-value>
        </init-param>
        <!-- 配置初始化优先级 -->
         <load-on-startup>2</load-on-startup>
     </servlet>
    
     <servlet-mapping>
      <servlet-name>dispatcher</servlet-name>
      <url-pattern>*.do</url-pattern>
     </servlet-mapping>
</web-app>

SpringMVC的配置(dispatcher-servlet.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:p="http://www.springframework.org/schema/p"
  xmlns:tx="http://www.springframework.org/schema/tx"
  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-4.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  
  <!-- 使用注解驱动 -->
  <mvc:annotation-driven />
  
  <!-- 定义扫描装载的包 -->
  <context:component-scan base-package="Controller" />
  
  <!-- 视图解析器 -->
  <bean id="viewResolver"
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"
     p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
  
  <!-- 配置开启注解事务 -->
  <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

Spring的配置(appilicationContext.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:p="http://www.springframework.org/schema/p"
  xmlns:tx="http://www.springframework.org/schema/tx"
  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-4.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
 <!-- 注解驱动 -->
 <context:annotation-config/>
  
 <!-- 数据库连接池 -->
 <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
  <property name="username" value="root"></property>
  <property name="password" value=""></property>
  <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
  <property name="url" value="jdbc:mysql://localhost:3306/housekeeping"></property>
 </bean>
  
 <!-- 集成mybatis -->
 <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource"></property>
  <property name="configLocation" value="classpath:mybatis-config.xml"></property>
 </bean>
 
 <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource"></property>
 </bean>
 
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="Mapper" />
  <property name="SqlSessionFactoryBeanName" value="SqlSessionFactory" />
  
  <!-- 指定加了注解才视之为Mapper -->
  <property name="annotationClass" value="org.springframework.stereotype.Repository" />
 </bean>
 
</beans>

 

集成SpringMVC, Spring, Mybatis环境

标签:数据   driver   数据库连接   factory   use   source   视图   not   frame   

原文地址:https://www.cnblogs.com/Joey44/p/9651778.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!