最近跟着一个教学视频,成功的搭建好了ssm框架并且完成了初期的配置,总结了两次搭ssm框架的经验,基本上最难的地方就是如何识别报错信息、解决冲突或失效jar包、修正拼写错误等一些小的问题,归根结底还是对ssm的运行原理的理解不够深刻,所以在这里进行一次比较小的总结,如果出现错误欢迎提出。
SSM框架搭建的配置文件及其分工
DAO层配置文件-applicationContext-dao.xml
首先是DAO层的配置文件,applicationContext-dao,在这个文件中,主要的分工有以下三点:
1、配置数据库连接池
1、将mybatis的sqlsessionfactory放入spring容器中
2、将mapper的代理对象放入spring容器中
代码如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<context:property-placeholder
location="classpath:resources/db.properties" />
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxActive" value="10"></property>
<property name="minIdle" value="5"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.amake.simpleshop.mapper"></property>
</bean>
</beans>
配置数据库连接文件db.properties
jdbc.url=jdbc:mysql://localhost:3309/ssm?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.user=root
jdbc.password=123456
在这两个配置文件中需要注意以下几点:
1、在配置数据库连接池时,bean下的property中,阿里巴巴的druid和c3p0不同点就是name字段不同。
2、低版本的c3p0可能会导致和mysql兼容问题,低版本的mysql connector可能会导致和druid兼容问题。
3、sqlsessionfactory是mybatis的必要配置文件,在不必要的前提下对内容可以不做要求。
4、在数据库连接配置文件中,url后数据库?后面的字符串表示的意思为:使用UTF-8编码格式,并且设置GMT的时区。
Service层配置文件-applicationContext-service、applicationContext-trust
applicationContext-service的作用是扫描事务逻辑类的,而trust用来配置事务信息
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<context:component-scan base-package="com.amake.simpleShop.service"/>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.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="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="create*" propagation="REQUIRED"/>
<tx:method name="updata*" 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:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.amake.simpleShop.service.*.*(..))"/>
</aop:config>
</beans>
在这两个代码中需要注意的有以下几点:
1、dataSource是引用dao层数据库连接池的id,此时ref后需要和dao层中的连接池id相同。
2、REQUIRED和SUPPORTS的区别:前者是在当方法在事务中,则在事务中执行,当没有事务会开启新的事务;后者是方法在事务中就执行事务,如果没有事务,也不会开启新的失误、
3、aop切面:该标签是标记了一个切入点,意为:执行任意文件夹中的ervice下的任意子包中的任意方法,可包含任意参数,所返回的值都将被拦截。
表示层springmvc 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"
xmlns:task="http://www.springframework.org/schema/task"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd">
<context:component-scan base-package="com.amake.simpleshop.controller"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>
<mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
</beans>
该配置文件中需要注意的是:
1、扫描的对象是controller,会扫描到包含@controller等注解的类
2、该配置使用了注解驱动
3、prefix中的WEB-INF,必须是“/WEB-INF/***/”以“/结尾”,否则重定向的路径将会重叠,导致重定向失败。
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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>test</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-list>
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>CharacterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
在web.xml中,主要需要配置的有以下几点:
1、创建一个Spring容器
2、配置一个字符过滤器,防止乱码
3、配置一个spring前端控制器
项目中容易出错的地方
1、tomcat7的部署,还有maven运行是需要依赖于jdk,而不是jre;
2、拼写错误,有时会将一些单词拼成错误的状态,导致引导类的方法无法正常识别,例如resourse和resource、WEB-INF和WBE-INF,包括在代码中和文件夹的命名,还有一些路径配置,如/和/*
3、jar包的版本兼容问题,当出现这一类的问题,在报错中通常会有找不到类文件的提示,此时就可以去maven的本地仓库,检查对应的jar包中的jar文件是否正常或者直接去maven的中央仓库去下载jar包替换掉就好,还有一类的jar包因为版本问题会导致一些错误,例如旧版本的c3p0可能会导致无法重复写入数据,新版本的mysql中,驱动的地址为com.mysql.cj.jdbc.Driver等。
4、文档约束错误,部分的配置文档需要有合理的约束,建议出现有关约束报错时,去网上或博客上找到对应的约束,并记得修改其适应的版本号。
5、在使用maven跑程序时,记得先停止前一次的运行,否则会报clean错误。
6、在配置拦截器后,可能会出现系统找不到mapper映射文件的错误,此时要在有关mapper的pom文件修改,保证mapper映射文件能够得到配置。代码如下
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
通常的一些问题解决方法
有报错不是病,成堆成堆的报错是真要命。通常如果是maven的报错,可以之间看控制台的error或者warning,比如说jar包已经存在就是在pom文件有多出的dependency,或者是一看到关于clean的错误,很有可能就是上一次启动tomcat没有关闭。
一、如果是jar包出错,我通常会采取以下办法(按顺序执行):
1、使用排除法或者替换法,查找是那个jar包出现问题(注意备份原代码),一般的jar包,也就是依赖源都是可以替换的,比如slf4j和log4,druid和c3p0,但如果替换失效,则可以先取消依赖jar包,尝试是否报错。
2、如果采用一方法报错,那么可以采用更换版本的方法,比如mybatis的版本,注意spring的版本要与其他spring相关的jar包版本相同。
3、找到报错的jar包,去maven中央仓库去手动下载jar包,并将其替换,一般就可以解决。
二、tomcat出现问题,那么很大的可能是端口号配置错了。
三、当出现一些引用,例如ref,出现没有找到相应的id的时候,就要依靠spring的执行原理,进行顺序性的代码检查,例如是否成功引入class文件,是否有拼写错误等。
四、当尝试测试是否可以根据字段查找信息,而localhost网页出现一直的载入的情况,这说明一些问题
1、数据库配置文件,也就是db.properties里的信息错误,导致连接失败。
2、数据库连接池和mysql的jar包出现冲突。
3、@RequestMapping后面的字段有误,或者后面{}中的参数与@PathVariable后的参数名不同,如果不同则需要用value声明。