前段时间用到了SSM框架,由于时间久了容易忘记,所以以文字的形式将一些东西保存起来。
Spring是一款主要用于控制管理bean对象和面向切面编程的框架;SpringMVC是一个非常好用的MVC框架;Mybatis是一个底层持久化框架。三大框架的整合可以大大的加快代码的编写速度,从而提高效率。现将三大框架的整合整理如下:
一、JAR包
Spring和SpringMVC包
aopalliance.jar
aspectjweaver.jar
spring-aop-4.3.2.RELEASE.jar
spring-aspects-4.3.2.RELEASE.jar
spring-beans-4.3.2.RELEASE.jar
spring-context-4.3.2.RELEASE.jar
spring-core-4.3.2.RELEASE.jar
spring-expression-4.3.2.RELEASE.jar
spring-jdbc-4.3.2.RELEASE.jar
spring-tx-4.3.2.RELEASE.jar
spring-web-4.3.2.RELEASE.jar
spring-webmvc-4.3.2.RELEASE.jar
Mybatis包
mybatis-3.4.2.jar
mybatis-spring-1.3.0.jar
mysql-connector-java-5.1.8-bin.jar
由于项目需要,又额外引入了一些包
1、使用c3p0连接池需要引入包
c3p0-0.9.5.2.jar
2、使用mybatis分页插件需要的包
jsqlparser-0.9.5.jar
pagehelper-4.2.1.jar
3、由于Spring和mybatis都有日志功能,所以还需要以下的包,这里使用的是log4j日志框架
commons-logging-1.2.jar
log4j.jar
4、在前端使用jstl来获取一些信息,需要jstl包
commons-logging-1.2.jar
log4j.jar
二、配置文件整合
前面已经列举了整合时候一些需要的包,如果还有功能的增加,还可以继续引入其他相应的包,这里就不做过多的说明了。接着就需要配置xml文件了。
在之前,先介绍项目的结构
pers.ly.yuanban为package名。
controller为spring的控制器;
dao是数据库层,其中采用mapper方式即面向接口方式编程;
mapping下是对应的数据库对象的mapper配置文件;
service是服务层,也是接口方式。原因在于后面的配置文件使用的是申明式事务;
VO是实体类;
util是一些常用的工具。
言归正传,配置tomcat服务器启动时的一些配置即加载Spring和SpringMVC,在web.xml中配置。
1、web.xml
<!-- 加载Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载SpringMVC -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
再看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:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/yuanban"/>
<property name="user" value="root"/>
<property name="password" value="leiyong"/>
</bean>
<!-- mybatis整合,配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<!-- 配置申明式事务 -->
<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 事务通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="query*" propagation="NOT_SUPPORTED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置事务切面 ,对业务层的所有类进行切面编程-->
<aop:config>
<aop:pointcut expression="execution(* pers.ly.yuanban.service.impl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
<!-- 扫描主体包下的所有注解 -->
<context:component-scan base-package="pers.ly.yuanban"/>
</beans>
2、SpringMVC配置配置:mvc.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:context="http://www.springframework.org/schema/context"
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.xsd">
<!-- 扫描该包下面的注解 -->
<context:component-scan base-package="pers.ly.yuanban.controller"/>
</beans>
3、mybatis配置:mybatis.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>
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 开启按需加载 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<typeAliases>
<package name="pers.ly.yuanban.vo"/>
</typeAliases>
</plugins>
</configuration>
到此为止,SSM整合完成