关于Spring、SpringMVC、Mybatis框架的整合

关于Spring、SpringMVC、Mybatis框架的整合
1.首先在工程中建立一个Dynamic Web 或者Maven工程
2.引入相关的jar包
2.1一共是3个配置文件:SpringContext.xml、Springmvc.xml、SqlMapConfig.xml
3.配置applicationContext.xml文件:
3.1一开始配置相应的schema检测(这里可以根据自己情况增加,并不是说就这些)

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

3.2配置jdbc属性文件:
<!--载入jdbc配置文件 --> <context:property-placeholder location="classpath:jdbc.properties" />

3.3封装数据源:

<bean   name="dataSource"  class="com.alibaba.druid.pool.DruidDataSource">
	<property name="driverClassName" value="${jdbc.driver}"></property>
	<property name="url" value="${jdbc.url}"></property>
	<property name="username" value="${jdbc.username}"></property>
	<property name="password" value="${jdbc.password}"></property>
	</bean>

这里的数据池我用了阿里的Druid

3.4配置SqlSessionFactory:

<!-- 配置SqlSessionFactory -->
	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 设置MyBatis核心配置文件 -->
		<property name="configLocation" value="classpath:SqlMapConfig.xml" />
		<!-- 设置数据源 -->
		<property name="dataSource" ref="dataSource" />
		<!--别名包扫描  -->
		<property name="typeAliasesPackage" value="com.wzu.crm.pojo"></property>
	</bean>

这其中的Mybatis全局配置文件我取名是“SqlMapConfig.xml”

3.5Mapper包的扫描:

<!-- 配置Mapper扫描 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 设置Mapper扫描包 -->
		<property name="basePackage" value="com.wzu.crm.mapper" />
	</bean>

3.6Service包的扫描:

<context:component-scan base-package="com.wzu.crm.service"></context:component-scan>

3.7有关事务的部分:配置事务管理器:

  <!-- 事务管理器 -->
	<bean id="transactionManager"	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 传播行为 -->
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>

3.8 配置AOP切面:

<aop:config>
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* com.wzu.crm.service.*.*(..))" />
	</aop:config>

4.到这里的话完整的Spring配置文件已经配好,接下来是SpringMvc.xml

4.1:SpringMvc内容较少:

<?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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<!-- 配置Controller扫描 -->
	<context:component-scan base-package="com.wzu.crm.controller" />
  <!--  属性文件配置加载-->
    <context:property-placeholder   location="classpath:crm.properties"/>
	<!-- 配置注解驱动 -->
	<mvc:annotation-driven></mvc:annotation-driven>	
	<!-- 配置视图解析器 -->
	<bean	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前缀 -->
		<property name="prefix" value="/WEB-INF/jsp/" />
		<!-- 后缀 -->
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

5.Mybatis的全局配置文件SqlMapConfig.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>

</configuration>

6.还需要在WEB.xml中配置相关内容:

  <!--引进applicationContext.xml文件-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!-- 配置Spring监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 编码过滤器 -->
  <filter>
    <filter-name>encoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 配置Spring MVC servlet -->
  <servlet>
    <servlet-name>CRM</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>CRM</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>

配置到这里就已经完成了:只需要改动配置文件中的包名就行!然后按照正常的开发步骤做下去即可:Pojo——Mapper——Service——Serviceimpl——Controller

PS:在开发过程中applicationContext.xml可能会变得很多,你也可以按照功能的区别将其拆分成:1.applicationContext-dao.xml 2.applicationContext-service.xml 3.applicationContext-tran.xml

猜你喜欢

转载自blog.csdn.net/qq_35069673/article/details/87905048