傳送門:SSH配置筆記
傳送門:SSH簡單CRUD整合DEMO
最近有朋友問到如何整合SSH,儘管是個過時的MVC框架了,但是本着從簡而難的過程,瞭解一下也是應該的,後續學習springMVC、spring、springboot也是一個好的開始。
我找到了一箇舊的demo,修改了配置文件,啓動後,便遇到了若干問題。排查一遍,發現使用的這個demo是比較舊的,長時間沒有更新,jar包存在版本不對應、缺少的問題,着實令人頭痛,遂做以記錄,給大家一個參考。SSH整合DEMO可以參考傳送門,本文針對整合過程中容易碰到的問題做以記錄。
2019年11月14日 23:18:35
PS:SSH已經過於老舊,不建議想學習框架的初學者入手了,簡單瞭解即可。可以從springMVC、或更新的springboot入手。
spring整合mybatis、hibernate等ORM框架及struts、springMVC等MVC框架時,常見的問題
1、元素:xx的前綴 “xx” 未綁定 這類問題
原因: xml配置文件的文件頭中缺少響應的xmlns、xsi引用,添加相應的引用即可。
例:缺少context-placeholder……
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
修改後:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
# 添加context
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
#添加context
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
2、通配符的匹配很全面, 但無法找到元素 ‘aop:config’ 的聲明 這類問題
解決方案: 請檢查,xmlns、xsi中uri的拼寫,重要的事情說三遍,檢查拼寫!檢查拼寫!檢查拼寫!
3、nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor 這類錯誤
解決方案: 缺少響應的jar包,如,這個就是缺少aopalliance包,就是下面這個東東,添加jar即可。
4、Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate 這類錯誤
解決方案: 注意applicationContext配置文件中bean的配置。常見的就是誤配置了接口,正確的應該是實現類。
如, 這裏應該是impl,不應該是接口,易錯。
<bean id="userService" class="com.it.ssh.service.impl.UserServiceImpl">
<property name="userMapper" ref="userMapper"></property>
</bean>
5、Bean property * is not writable or has an invalid setter method 這類錯誤
解決方案: 注意property和實際接口/Mapper類中注入的對象要對應
<bean id="userService" class="com.it.ssh.service.impl.UserServiceImpl">
<property name="userMapper" ref="userMapper"></property>
</bean>
如上圖所示,我的配置文件中userService這個bean的property爲userMapper,那麼,實際的userService的實現中注入的UserMapper對象就應該和上處相同。
6、java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer; 這類問題
問題原因: 常見與mybatis+spring類的整合過程中,jar版本不對應。
解決方案: 選擇合適的jar包版本
參照mybatis官網對jar包版本的建議:
MyBatis-Spring | MyBatis | Spring |
---|---|---|
1.0.0 and 1.0.1 | 3.0.1 to 3.0.5 | 3.0.0 or higher |
1.0.2 | 3.0.6 | 3.0.0 or higher |
1.1.0 or higher | 3.1.0 or higher | 3.0.0 or higher |
1.3.0 or higher | 3.4.0 or higher | 3.0.0 or higher |
7、org.apache.commons.dbcp.DelegatingPreparedStatement.isClosed()Z 這類問題
問題原因: mybatis和c3p0或dbcp連接池jar版本不兼容問題
解決方案: 適當降低mybatis版本,或提高dbcp、c3p0版本
附,我的項目樹,及配置文件:
圖1, 項目樹,此SSH DEMO使用了配置文件的方式進行了配置、整合,並未使用註解的方式。
圖2 相關jar包,此jar包未精簡,基本SSH無需使用這麼多,有冗餘。(偷個懶-_-)
圖3 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>ssh</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<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>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.
StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
圖4 mybatis-config.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>
<typeAliases>
<typeAlias type="com.it.ssh.entity.User" alias="user"/>
<typeAlias type="com.it.ssh.entity.Product" alias="product"/>
</typeAliases>
<mappers>
<!-- spring sqlsessionfactory中自動掃描,這裏空白即可 -->
</mappers>
</configuration>
圖5 struts.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="userAction" />
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
<action name="userAction" class="UserAction">
<result name="success">show.jsp</result>
<result name="input" type="redirect">login.jsp</result>
</action>
</package>
<include file="classpath:applicationContext.xml"/>
<!-- Add packages here -->
</struts>
圖6 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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- jdbc配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- datasource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml">
</property>
<!-- 掃描所有實體映射文件 -->
<property name="mapperLocations">
<list>
<value>classpath:/com/it/ssh/mapper/*.xml</value>
</list>
</property>
</bean>
<!-- <bean id="userMapper" class="com.it.ssh.mapper.UserMapper">
<property name="sessionFactory" ref="sqlSessionFactory"></property>
</bean> -->
<bean id="userService" class="com.it.ssh.service.impl.UserServiceImpl">
<property name="userMapper" ref="userMapper"></property>
</bean>
<!--整合了Struts之後,不能直接找action,沒有personService會有空指針異常。
應該先找到Spring配置文件中的內容-->
<bean name="userAction" class="com.it.ssh.action.UserAction">
<property name="userService" ref="userService"></property>
</bean>
<!-- 事務管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- mapperScannerConfigurer -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.it.ssh.mapper"></property>
</bean>
<!-- transactionManager -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="query*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- aop事務配置 -->
<aop:config>
<aop:pointcut id="fooServiceOperation"
expression="execution(* com.it.ssh.service.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation" />
</aop:config>
</beans>
圖7 jdbc.properties <mysql版>
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/dbms?useUnicode=true
&characterEncoding=utf-8&useSSL=false
user=root
password=admin
測試效果:
public class Test {
public static void main(String[] ar gs) {
ApplicationContext ac=new
ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService=(UserService)ac.getBean("userService");
User user = new User();
user.setName("zhou");
user.setPassword("123456");
System.out.println(userService.addUser(user));
}
}
如上圖所示,解決了SSH整合中遇到的問題後,測試一個DAO方法,成功添加一條記錄。
後記:以上是筆者遇到的問題,及通過以上解決方案得到了解決,希望可以給大家一個參考。