写文章

手把手带你实现Spring、Spring MVC与Mybatis整合工程的搭建

2018-11-29 00:35:48

1742 | 0 | 0

1.整合的思路

1.Mybatis3.x与Spring3.x整合的基础上再进行Spring MVC框架的整合。

2.Spring要管理Spring MVC编写的Handler(controller)、Mybatis的SqlSessionFactory、mapper、数据源。

其实整合思路就是下面这三步:

  1. 第一步:整合dao(即mapper),完成Spring与Mybatis的整合。

  2. 第二步:整合service,Spring管理service接口,service中可以调用Spring容器中的dao(mapper)。

  3. 第三步:整合controller,Spring管理controller接口,在controller调用service。

2.导入jar包

mybatis-3.x.jar、spring3.x.jar(包含springmvc的jar包)、mybatis与spring的整合jar、数据库驱动包、log4j.jar、jstl.jar。如下:

3.工程结构

3.1需要创建的配置文件

  1. applicationContext-dao.xml—配置数据源、SqlSessionFactory、mapper扫描器。

  2. applicationContext-service.xml—配置service接口。

  3. applicationContext-transaction.xml–事务管理。

  4. sprintmvc.xml—springmvc的配置,配置处理器映射器、适配器、视图解析器(这里我们统一采用注解的方式进行开发)。

  5. SqlMapConfig.xml—mybatis的配置文件,配置别名、settings、mapper。

工程目录如下:

3.2各个配置文件的内容

applicationContext-dao.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath:config/db.properties" />

	<!-- 数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxIdle" value="5" />
	</bean>


	<!-- SqlsessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource"/>
		<!-- mybatis配置文件 -->
		<property name="configLocation" value="classpath:config/mybatis/SqlMapConfig.xml"/>
	</bean>
	
		
	<!--
	MapperScannerConfigurer:mapper的扫描器,将包下边的mapper接口自动创建代理对象,
	自动创建到spring容器中,bean的id是mapper的类名(首字母小写)
	 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 配置扫描包的路径
		如果要扫描多个包,中间使用半角逗号分隔
		要求mapper.xml和mapper.java同名且在同一个目录
		 -->
		<property name="basePackage" value="mapper"/>
		<!-- 使用sqlSessionFactoryBeanName -->
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

	</bean>
	
</beans>

applicationContext-service.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">


	
</beans>

applicationContext-transaction.xml:配置事务,在配置文件中使用声明式事务配置

<beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">


		<!--使用声明式的控制配置,可以有效的规范代码-->
		<!--事务管理器的配置-->
		<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="sava" propagation="REQUIRED"/>
				<tx:method name="insert*" propagation="REQUIRED"/>
				<tx:method name="update*" propagation="REQUIRED"/>
				<tx:method name="delete*" 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:attributes>
		</tx:advice>

		<!--配置aop-->
		<aop:config>
			<aop:advisor advice-ref="txAdvice" pointcut="execution(* service.impl.*.*(..))"/>
		</aop:config>

</beans>

sprintmvc.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">


    <!--使用spring组件扫描
    一次性配置此包下所有的Handler-->
    <context:component-scan base-package="controller"/>

    <!--注解处理器映射器-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    
    <!--注解的适配器-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

    <!--配置视图解析器
    要求将jstl的包加到classpath
    prefix:代表请求url的前缀
    suffix:代表请求url的后缀
    设置了这两个属性值后我们在Controller中进行代码开发时返回的modelandview对象设置的页面路径值就不用带前缀名和后缀名了-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

	<!-- 定义 别名 -->
	<typeAliases>
		<!-- 批量别名定义 指定包路径,自动扫描包下边的pojo,定义别名,别名默认为类名(首字母小写或大写) -->
		<package name="po" />

	</typeAliases>


	<!--由于使用了spring和mybatis整合的mapper扫描器,-->
	<!--这里就不用配置了-->
	<!--<mappers>-->
		<!--<package name="mapper"/>-->
	<!--</mappers>-->

</configuration>

在web.xml文件中加入对前端控制器的配置:

<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--加载springmvc配置文件-->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <!--配置文件的地址
        如果不配置contextConfigLocation,默认查找的配置文件名称是classpath下的:servlet名称+"-servlet.xml"即springmvc-servlet.xml-->
        <param-value>springmvc.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!--可以配置/:此工程所有的请求全部由springmvc解析,此种方式可以实现RESTful方式,需要特殊处理对静态文件的解析不能由springmvc解析
    可以配置*.do或者*.action,所有请求的url扩展名为.do或.action由springmvc解析,此中方法常用
    不可以配置/*,如果配置/*,返回jsp也由springmvc解析,这是不对的-->

    <url-pattern>*.action</url-pattern>
</servlet-mapping>

另外还需要添加数据库的配置文件db.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=xiaxunwu1996.

在src下创建各个包:mapper、po、controller、service,在web包下创建页面包jsp,工程目录如下:

经过上述步骤,我们便完成了Spring、Spring MVC与Mybatis的整合,是不是很简单?没错就是这么简单。可是整合后又该如何进行web项目的开发呢?接下来我将通过下篇文章一个案例带你快速入门SSM开发介绍利用Spring、Spring MVC与Mybatis的整合项目进行开发一个案例带你快速学会使用SSM框架开发项目。

转自:http://codingxiaxw.cn/2016/11/15/44-ssm%E7%9A%84%E6%95%B4%E5%90%88/

0

收藏
分享