在整合Spring,SpringMVC,MyBatis的过程中,会遇到相当多的问题,在完成一个项目的过程中,我们通常会将其三者进行整合,从而使用Spring依赖注入以减少代码的耦合。使用Spring MVC处理请求并作出响应,是Mybatis更加简洁的完成数据库的操作,但是在整合的过程中,稍有不慎,就会丢失页面样式,以下,我将我在项目中遇到的问题进行分析并解决后总结如下:
1.开发环境搭建:
·安装JDK1.8(环境配置此处不在赘述)
·Eclipse(安装插件:Spring,Mybatis)
·Tomcat8,并在Eclipse中配置运行环境
·MySql5以上版本
2.整合所需jar包
以下以我正在完成的项目为例分析jar包
其中主要用到的jar包:
·数据源c3p0所需jar包:c3p0-0.9.5.1.jar
·sql语句执行的日志包,在使用log4j.properties文件的时候必须导入log4j-1.2.16.jar
·Mybatis框架jar包:mybatis-3.1.1.jar
·Mybatis整合Spring中间件jar包:mybatis-spring-1.2.1.jar,该中间件有两个功能:
①在spring中配置Mybatis工厂类
②在DAO层使用Spring注入的工具Bean对数据进行操作。
·Spring的AOP事物包:spring-aop-4.3.0.RELEASE.jar
·数据库驱动包:mysql-connector-java-5.1.8-bin.jar
3.准备数据库资源
创建一个名为 ssm_booksystem 的数据库,接着创建如下表,并添加主键和外键
4.配置文件(最主要的部分)
创建一个Dynamic Web Project,命名为:BMS,并在其项目下创建Source Folder,命名为config,旗下继续创建两个package,命名为:mybatis,spring
/BMS/config/mybatis/mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<configuration>
<!-- 环境,数据源,事务,别名,配置文件加载,缓存 -->
</configuration>
<?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"
xsi:schemaLocation="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.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:component-scan base-package="com.wll.bms" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.wll.bms.po"></property>
</bean>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wll.bms.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
/BMS/config/spring/springmvc-config.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.3.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.3.xsd">
<context:component-scan base-package="com.wll.bms.controller" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
在 applicationContext-config.xml 和 springmvc-config.xml 扫描资源的时候要十分注意,两个xml文件必须把整个项目下的资源全部扫描到,根据如上代码中可发现当springmvc-config.xml中扫描了controller包下的资两个两个源之后, applicationContext-config.xml 在扫描的时候只能扫描项目下除了springmvc-config.xml 扫描内容之外的其他资源,此时,所有的资源都会被管理起来,同时,这两个文件都必须配置在Web应用的部署描述符 web.xml文件中。
db.properties(数据库)
jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_booksystem
web.xml
DispatchServlet是Servlet(继承自HttpServlet基类),该Servlet在Web应用程序启动时加载,DispatchServlet加载时会需要一个Spring MVC的配置文件,默认情况下回与应用程序文件夹的WEB-INF下查找对应的文件,我在项目中将该文件放在/BMS/config/spring下,并将其见名知意的命名为springmvc-config.xml。
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--定义spring的前端控制器-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- contextConfigLocation 参数用来指定 Spring 的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-config.xml</param-value>
</context-param>
<!--配置spring核心监听器,默认会以/WEB-INF/applicationContext-config.xml 作为配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>