MyBatis和Spring和SpringMVC的簡單整合

需要的jar包

springIOC需要的jar包
在這裏插入圖片描述
jdbc需要的jar包
在這裏插入圖片描述
springmvc需要的jar包
在這裏插入圖片描述
JSTL需要的jar包
在這裏插入圖片描述
springAOP和spring允許的日誌包
在這裏插入圖片描述
mybatis運行的jar包和數據交互的包
在這裏插入圖片描述
在這裏插入圖片描述
mybatis和spring整合的適配包
在這裏插入圖片描述

mybatis準備

bean,dao,mybatis全局配置文件,映射文件,什麼亂七八糟的基本配置準備好

SpringMVC準備

在web.xml裏面配置

  <!-- springmvc配置 -->
  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

web.xml的同級目錄的spring-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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<!-- SpringMVC控制網站跳轉邏輯 -->
	<!-- 只掃描控制器 -->
	<context:component-scan base-package="bean" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

	<context:component-scan base-package="dao" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 視圖解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<mvc:annotation-driven></mvc:annotation-driven>
	<mvc:default-servlet-handler/>
	
</beans>

spring準備

配置springIOC容器隨web一起啓動,在web.xml裏面配置

  <!--Spring配置: needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

spring的配置文件applicationContext.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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
	
	<!-- Spring管理所有的業務邏輯組件 -->	
	<context:component-scan base-package="com">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 引入數據庫配置文件 -->
	<context:property-placeholder location="classpath:jdbcconfig.properties"/>
	<!-- Spring用來控制業務邏輯,數據源,事務控制,AOP... -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="user" value="${jdbc.usename}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	
	<!-- spring的事務管理 -->
	<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 開啓基於註解的事務 -->
	<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
	
	<!-- 整合mybatis
		作用目的:1.spring來管理所有組件,比如那些mapper實現類
					這樣就不用在代碼裏面寫getmapper了
					比如service層調用Dao層的時候,就直接@Auto wired:自動注入mapper就行
				2.spring來管理事務,比如spring的聲明式事務
	 -->
	 <!-- 配置出一個SqlSessionFactory對象 -->
	 <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
	 	<property name="dataSource" ref="dataSource"></property>
	 	<!-- configLocation指定mybatis的全局配置文件的位置 -->
	 	<property name="configLocation" value="classpath:mybatis.xml"></property>
	 	<!-- 指定mapper文件的位置 -->
	 	<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
	 </bean>
	 
	<!--配置一個可以進行批量執行的sqlSession  -->
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg>
		<constructor-arg name="executorType" value="BATCH"></constructor-arg>
	</bean>
	 
	 
	 <!-- 掃描所有的mapper接口,讓這些mapper能夠自動注入 -->
	 <mybatis-spring:scan base-package="dao"/>
	 <!--也可以這樣寫 
	 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	 	<property name="basePackage" value="dao"></property>
	 </bean>
	 -->
</beans>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章