通过几个月在公司的学习及项目的练习,总结了一下SSM框架的搭建和整合
新建WEB工程
打开Myecplsie,新建WEB工程
下一步,此处注意选择WEB2.5项目,若选择2.5版本以上,则WEB-INF目录下不会有web.xml配置文件,需自己手动引入
引入Jar包
先下载SSM框架jar包, 将jar包复制到WEB-INF/lib目录下, 选中所有jar包,右键Bulid path->Add to Build Path 。
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Logistics_SSM</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 加载Spring容器配置 -->
<!-- 配置ContextLoaderListener 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置Spring容器加载所有的配置文件的路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- log4j日志 -->
<!-- <context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param> -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- 配置SpringMVC核心控制器 -->
<servlet>
<!--配置SpringMVC的前端控制器 -->
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置前端控制器的路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--启动加载一次 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 为DispatcherServlet建立映射 -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<!-- 2.4.1此处可以可以配置成*.do -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 解决工程编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>userFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-app>
Spring与mybatis整合配置文件
新建applicationContext.xml文件于src目录下
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!--开启切面编程自动代理-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!--扫描注解生成bean-->
<context:annotation-config/>
<!--包扫描-->
<context:component-scan base-package="com.baihao"/>
<!-- 配置数据库jdbc.properties的地址 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源 写法一 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxActive" value="${jdbc.maxActive}"></property>
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<property name="maxWait" value="${jdbc.maxWait}"></property>
<!-- 解决数据库库链接8小时超时问题 -->
<property name="testOnBorrow" value="${jdbc.testOnBorrow}" />
<property name="testWhileIdle" value="${jdbc.testWhileIdle}" />
<property name="logAbandoned" value="${jdbc.logAbandoned}" />
<property name="removeAbandoned" value="${jdbc.removeAbandoned}" />
<property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}" />
<!-- #运行'判断连接超时任务'的时间间隔,单位为毫秒,默认为-1,即不执行任务。 -->
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />
<!-- #连接的超时时间,默认为半小时。 -->
<property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" />
</bean>
<!-- 扫描所有的mapper.xml 创建session -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"></property>
<!-- 这里的路径是xml文件所在的位置 -->
<property name="mapperLocations" value="classpath:com/baihao/mapper/*.xml"></property>
</bean>
<!-- 这里的操作相当于session.getMapper( ) -->
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 这里的basePackage是接口的位置 -->
<property name="basePackage" value="com.baihao.mapper" />
<property name="sqlSessionFactoryBeanName" value="sessionFactory" />
</bean>
<!--声明事务管理 采用注解方式-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transaction"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 所有dataSource里执行的每一次操作都作为一个事务提交 -->
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
配置数据源
新建jdbc.properties于src目录下, 将数据源修改成自己的数据源
jdbc.url=jdbc:mysql://localhost:3306/logistics?characterEncoding=UTF-8&autoReconnect=true
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123
jdbc.maxActive=100
jdbc.maxIdle=30
jdbc.maxWait=300
jdbc.testOnBorrow=false
jdbc.testWhileIdle=true
jdbc.logAbandoned=true
jdbc.removeAbandoned=true
jdbc.removeAbandonedTimeout=120
jdbc.timeBetweenEvictionRunsMillis=3600000
jdbc.minEvictableIdleTimeMillis=3600000
配置springMVC
新建spring-mvc.xml于src目录下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!--包扫描-->
<context:component-scan base-package="com.baihao.controller"/>
<!--开启注解扫描-->
<mvc:annotation-driven/>
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置从项目根目录到指定目录一端路径 ,建议指定浅一点的目录-->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!-- 文件的后缀名 -->
<property name="suffix" value=".jsp"></property>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
</beans>
配置Log4J
新建log4j.properties文件于src目录下
log4j.rootLogger=DEBUG,Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.org.apache=INFO
log4j.logger.com.ibatis = ERROR
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource = ERROR
log4j.logger.com.ibatis.common.jdbc.ScriptRunner = ERROR
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = ERROR
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
总结
至此ssm框架整合已经搭建完成, 创建好controller、bean、service、mapper各层的包结构即可开始测试该项目。在搭建框架时应注意细节问题, 比如jar包引入错误, 则会导致框架不能加载。