1.因为我使用idea创建的是聚合工程,子模块虽然创建的是webapp类型,但是还不算web项目,没有artifacts。
解决方法:File-Project Structrue-Project Settings-Artifacts中添加子模块war包。Modules中修改web的Deplooyment Desciptos
和Web Resource Directories.
2.包找不到路径。就好像没有jar包一样,pom文件也没问题。
问题原因:不清楚
解决办法:idea中Reimport All MavenProject,其实老早就想这样操作了,但老是失败。
原因是maven版本比idea高,改个版本低的maven。
3.spring和springMVP整合时,老是启动报错。找不到WEB-INF/spring-servlet.xml文件。
问题原因:因为spring的listener默认找WEB-INF/spring-servlet.xml,因为创建的是maven项目,配置文件在src/main/resources中。
解决办法:尝试在web.xml中添加contextConfigLocation,但是没解决。
<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext.xml</param-value> </context-param>
然后在web.xml中添加DIspatcherServlet的初始化参数,解决了。
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> 默认 </init-param> --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
4.spring和mybatis整合时,启动报错。(...expected at least 1 bean which qualifies as autowire candidate for this dependency...)
问题原因:忘记配置spring-mybatis.xml文件。
解决办法:添加spring-mybatis.xml文件。applicationContext.xml文件引入此文件(当然直接在applicationContext文件中也可以编写)。此文件为:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 导入属性配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 1、数据源 DriverManagerDataSource --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${name}" /> <property name="password" value="${password}" /> </bean> <!-- 2.mybatis的SqlSession的工厂:SqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:spring/mybatis-config.xml" /> <property name="dataSource" ref="dataSource" /> <!-- 别名包 <property name="typeAliasesPackage" value="com.manager.entity"/>--> </bean> <!-- 3.mybatis自动扫描加载sql映射文件:MapperScannerConfigurer --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="annotationClass" value="org.springframework.stereotype.Repository"/> <property name="basePackage" value="com.along.mapper"/> <!-- 如果用sqlSessionFactory方式,启动报错。因为把通配符${}当成字符串了,而不是db.properties配置文件中的值了 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!--<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>--> </bean> <!-- 4.事物管理:DataSourceTransactionManager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 5.使用声明事物 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- mapper bean <bean id="studentMapper" class="org.mybatis.spring.MapperFactoryBean"> <property name="mapperInterface" value="com.manager.data.StudentMapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>--> </beans>
其中引入的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> <!-- 引用db.properties配置文件 <properties resource="classpath:/db.properties"/>--> <!-- development : 开发模式 work : 工作模式 --> <!--<environments default="development"> <environment id="development"> <transactionManager type="JDBC" /><!– 配置数据库连接信息 –> <dataSource type="POOLED"><!– value属性值引用db.properties配置文件中配置的值 –> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${name}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments>--> <!-- 加载 映射文件 --> <mappers> <mapper resource="com/along/mapper/LangMapper.xml"></mapper> <!--<mapper class="com.along.mapper.LangMapper"/>--> <!--<package name="com.along.mapper" />--> </mappers> </configuration>
db.properties为:
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/lang name=root password=root
5.spring和mybatis整和,启动报错。(java.lang.IllegalStateException: Could not load JDBC driver class [${jdbc.driver}])
问题原因:spring-mybatis.xml文件中引入db.properties,然而value值${}通配符没起作用。
解决办法:将spring-mybatis.xml从
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>改为
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
6.spring和mybatis整合,启动报错。(*with invalid types () or values (). Cause: java.lang.NoSuchMethodException)
问题原因:mapper初始化错误。install后在mapper包下竟然没有mapper.xml文件。
解决办法:在pom.xml文件中添加:
<!-- 配置文件导出问题:--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources>
7.spring和mybatis整合,启动报错。(Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating interface dao.UserMapper with invalid types () or values (). Cause: java.lang.NoSuchMethodException: dao.UserMapper.<init>()
问题原因:因为今天尝试自己写mybatis的增删改查,没有用自动生成工具。<resultMap >配置错误,他的type类型应该为要映射的JavaBean类,而我写成了对于dao中mapper接口的映射,导致一直报错
解决办法:<resultMap >改为JavaBean类。