Spring学习Eclipse的各种坑

本文介绍如何在Eclipse中安装Spring IDE插件,并解决了Spring 3.2与JDK 8的不兼容问题及Servlet 3.0 web.xml配置缺失的问题。

最近在学后台技术SSH和SSI ,本来准备使用MyEclipse做开发,但是前辈们都说学习阶段还是使用Eclipse,所以我想想果断使用了Eclipse,所以遇到各种坑。
1. Eclipse对Spring的IDE没有集成!

解决方案一:首先下载并安装 ajdt_2.0.2_for_eclipse_3.5.zip,这个文件是安装SpringIDE必需的文件。

下载地址:http://www.eclipse.org/ajdt/downloads/

下 载完成后将plugins和features目录复制到eclipse安装目录下,覆盖原来的plugins和features目录。
如果以前已 经启动过eclipse,就把configuration目录下config.ini文件和.settings目录外的所有文件删除。

解决方案二:当然还可以使用在线安装的方式,官方是比较支持这种方式的,可参考http://www.eclipse.org /ajdt/downloads/网页。

启动 eclipse,可以查看一下插件是否安装成功,Help->About Eclipse SDK->Plug-in Details
如果这里能够看到有‘AJ’的图标,说明安装成功,
在弹出的Plug-in Details对话框中能够看到AspectJ的几个插件。

开始安装Spring IDE,在eclipse中依次点Help->Software Updates->Find and Install…,
选择Search for new features to install,点Next,选择New Remote Site,
添加一个新的站点即SpringIDE的下载站点。在Name中输入SpringIDE……,
URL栏中输入 http://springide.org/updatesite/,完成之后,选中SpringIDE开始搜索插件。
搜索完成后会显示搜索到的 插件,这时首先选中Spring IDE.
最后选Install All就OK的

2. Eclipse   Spring3.2和JDK8的不兼容
     <context:component-scan base-package="com">时报错

报错信息
报错信息

官方解释:The exception you see is not very likely to be cause by invalid Spring configuration but your classpath setup. This feels like a version incompatibility regarding the ASM libraries and Java 8. The ASM version, Spring 3.2 depends on is known to be incompatible with Java 8.

Thus, if you want run your code with Java 8, you need to use a recent Spring 4.0 version.

解决方案一:

JDK改为7版本
解决方案二:
Spring升级4.x版本,但是注意的是加个spring-aop-4.1.5.RELEASE.jar,不然还是会出错

  1. Servlet 3.0 web.xml缺失
    解决方案:
 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

  <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>

</context-param>

加上这些就能正常配置和提示了

<think>嗯,用户现在想了解SSM框架(SpringSpring MVC、MyBatis)的集成与使用教程。之前他们问过SSM框架与JavaSE、JavaWeb的关系和使用场景,现在需要更具体的集成步骤和使用方法。我需要先回忆之前的回答内容,确保这次教程和之前的讨论连贯。首先,用户可能需要一个从零开始搭建SSM项目的步骤。之前的回答提到了SSM如何优化JavaWeb开发,所以现在需要详细说明如何整合这三个框架。我应该分步骤讲解,包括环境准备、配置文件和代码示例。接下来,用户可能关心每个框架在集成中的作用。比如Spring的IoC和AOP,Spring MVC处理Web层,MyBatis负责数据持久化。需要明确各层的配置,比如Spring的applicationContext.xml,SpringMVC的DispatcherServlet配置,以及MyBatis的SqlSessionFactory和Mapper扫描。 还要注意配置文件之间的依赖关系,比如如何在Spring中整合MyBatis,需要配置数据源、事务管理等。同时,要给出具体的代码示例,比如Controller、Service、Mapper接口和XML文件,这样用户能更直观地理解。 另外,用户可能遇到常见的集成问题,比如配置错误、依赖冲突或者事务管理问题。需要提前指出这些可能的,并提供解决方法,比如检查XML配置、使用Maven管理依赖等。最后,生成的相关问题应该围绕集成中的难点和应用场景,比如事务管理、RESTfulAPI开发、性能优化等,帮助用户进一步深入学习。确保回答结构清晰,分步骤讲解,代码示例正确,并引用之前的参考资料。同时,按照用户的要求,使用LaTeX格式的数学表达式(如果有的话),但在这个主题下可能用不上,所以重点放在代码和配置示例上。</think>### SSM框架集成与使用教程(Spring + Spring MVC + MyBatis) --- #### 一、环境准备 1. **基础依赖**: - JDK 1.8+ - Maven 3.6+ - Tomcat 9+ - MySQL 5.7+ - IDE(推荐IntelliJ IDEA或Eclipse) 2. **Maven依赖**(`pom.xml`核心配置): ```xml <!-- Spring 核心 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.10</version> </dependency> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.10</version> </dependency> <!-- MyBatis 整合Spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.7</version> </dependency> <!-- MySQL驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> ``` --- #### 二、配置文件整合 ##### 1. **Spring核心配置**(`applicationContext.xml`) ```xml <!-- 数据源配置 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test_db?useSSL=false"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <!-- MyBatis整合Spring --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!-- 扫描Mapper接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.dao"/> </bean> <!-- 事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> ``` ##### 2. **Spring MVC配置**(`spring-mvc.xml`) ```xml <!-- 开启注解驱动 --> <mvc:annotation-driven/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 扫描Controller --> <context:component-scan base-package="com.example.controller"/> ``` ##### 3. **Web.xml配置**(入口配置) ```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> <!-- Spring MVC DispatcherServlet --> <servlet> <servlet-name>dispatcher</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> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ``` --- #### 三、代码开发示例 ##### 1. **实体类**(POJO) ```java public class User { private Integer id; private String name; // Getter/Setter省略 } ``` ##### 2. **Mapper接口与XML映射** ```java // UserMapper.java public interface UserMapper { @Select("SELECT * FROM user WHERE id = #{id}") User selectUserById(int id); } ``` ##### 3. **Service层** ```java @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserById(int id) { return userMapper.selectUserById(id); } } ``` ##### 4. **Controller层** ```java @Controller public class UserController { @Autowired private UserService userService; @RequestMapping("/user/{id}") public String getUser(@PathVariable int id, Model model) { model.addAttribute("user", userService.getUserById(id)); return "userDetail"; } } ``` --- #### 四、常见问题与调试 1. **依赖冲突**: - 使用`mvn dependency:tree`检查依赖版本冲突。 - 例如:Spring 5.x需要Jackson 2.9+,低版本会导致JSON解析失败[^1]。 2. **事务失效**: - 确保在Service方法上添加`@Transactional`注解。 - 检查`applicationContext.xml`中是否启用了事务管理器[^2]。 3. **MyBatis映射错误**: - 检查XML映射文件路径是否与`sqlSessionFactory`配置一致。 - 确认SQL语句中的字段名与数据库表结构匹配[^3]。 --- ### 相关问题 1. **如何在SSM框架中实现分布式事务管理?**[^2] 2. **Spring MVC如何支持RESTful API开发?**[^1] 3. **MyBatis动态SQL有哪些常用标签?**[^3] 4. **如何优化SSM项目的启动速度?**
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑着巨人找巨人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值