1. Maven依赖
在pom.xml中添加SSM框架的依赖
<!-- Spring Core -->  
<dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-context</artifactId>  <version>5.3.x</version>  
</dependency>  
<!-- Spring Web MVC -->  
<dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-webmvc</artifactId>  <version>5.3.x</version>  
</dependency>  
<!-- MyBatis -->  
<dependency>  <groupId>org.mybatis</groupId>  <artifactId>mybatis</artifactId>  <version>3.5.x</version>  
</dependency>  
<!-- MyBatis Spring Integration -->  
<dependency>  <groupId>org.mybatis</groupId>  <artifactId>mybatis-spring</artifactId>  <version>2.0.x</version>  
</dependency>  
<!-- Database Driver (e.g., MySQL) -->  
<dependency>  <groupId>mysql</groupId>  <artifactId>mysql-connector-java</artifactId>  <version>8.0.x</version>  
</dependency>  
<!-- Other dependencies... -->2. Spring配置文件 (applicationContext.xml)
在src/main/resources目录下创建applicationContext.xml文件,并配置数据源、事务管理和MyBatis的SqlSessionFactoryBean。
<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"  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.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx.xsd">  <!-- DataSource Configuration -->  <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/your_database"/>  <property name="username" value="your_username"/>  <property name="password" value="your_password"/>  </bean>  <!-- Session Factory Configuration -->  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  <property name="dataSource" ref="dataSource"/>  <property name="mapperLocations" value="classpath:mappers/*.xml"/>  </bean>  <!-- Mapper Scanner Configuration -->  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  <property name="basePackage" value="com.example.yourapp.mapper"/>  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>  </bean>  <!-- Transaction Manager Configuration -->  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  <property name="dataSource" ref="dataSource"/>  </bean>  <!-- Enable Transaction Annotation Support -->  <tx:annotation-driven transaction-manager="transactionManager"/>  <!-- Other bean definitions... -->  
</beans>3. SpringMVC配置文件 (springmvc-config.xml)
在src/main/resources目录下创建springmvc-config.xml文件,并配置视图解析器、组件扫描等。
<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:mvc="http://www.springframework.org/schema/mvc"  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.xsd  http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc.xsd">  <!-- Enable @Controller support -->  <mvc:annotation-driven/>  <!-- Scan for @Controllers -->  <context:component-scan base-package="com.example.yourapp.controller" />  <!-- Configure View Resolver -->  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  <property name="prefix" value="/WEB-INF/views/" />  <property name="suffix" value=".jsp" />  </bean>  <!-- Other bean definitions for MVC components -->  </beans>4. 配置web.xml
在src/main/webapp/WEB-INF目录下,配置web.xml以加载Spring和SpringMVC的配置文件,并设置DispatcherServlet。
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"  version="4.0">  <!-- Spring Context Loader Listener -->  <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <!-- Spring Context Config Location -->  <context-param>  <param-name>contextConfigLocation</param-name>  <param-value>/WEB-INF/applicationContext.xml</param-value>  </context-param>  <!-- Spring MVC Servlet -->  <servlet>  <servlet-name>dispatcherServlet</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <init-param>  <param-name>contextConfigLocation</param-name>  <param-value>/WEB-INF/springmvc-config.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>dispatcherServlet</servlet-name>  <url-pattern>/</url-pattern>  </servlet-mapping>  <!-- Other configurations... -->  </web-app>5. 创建Mapper接口和映射文件
在src/main/java/com/example/yourapp/mapper目录下创建Mapper接口,并在src/main/resources/mappers目录下创建相应的XML映射文件。
Mapper接口
在src/main/java/com/example/yourapp/mapper目录下创建UserMapper.java接口:
package com.example.yourapp.mapper;  import com.example.yourapp.model.User;  
import org.apache.ibatis.annotations.Mapper;  
import org.apache.ibatis.annotations.Param;  import java.util.List;  @Mapper  
public interface UserMapper {  User findUserById(@Param("id") Integer id);  List<User> findAllUsers();  int insertUser(User user);  int updateUser(User user);  int deleteUserById(@Param("id") Integer id);  
}映射文件
 在src/main/resources/mappers目录下创建UserMapper.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >  
<mapper namespace="com.example.yourapp.mapper.UserMapper">  <select id="findUserById" resultType="com.example.yourapp.model.User">  SELECT * FROM users WHERE id = #{id}  </select>  <select id="findAllUsers" resultType="com.example.yourapp.model.User">  SELECT * FROM users  </select>  <insert id="insertUser" parameterType="com.example.yourapp.model.User">  INSERT INTO users (name, email) VALUES (#{name}, #{email})  </insert>  <update id="updateUser" parameterType="com.example.yourapp.model.User">  UPDATE users SET name = #{name}, email = #{email} WHERE id = #{id}  </update>  <delete id="deleteUserById" parameterType="java.lang.Integer">  DELETE FROM users WHERE id = #{id}  </delete>  </mapper>6. 创建Controller、Service和DAO层
在src/main/java/com/example/yourapp目录下创建Controller、Service和DAO层的相关类和接口。
Model类(可选,但通常在项目中会有)
在src/main/java/com/example/yourapp/model目录下创建User.java:
package com.example.yourapp.model;  public class User {  private Integer id;  private String name;  private String email;  // getters and setters  
}Service接口和实现
在src/main/java/com/example/yourapp/service目录下创建UserService.java接口和UserServiceImpl.java实现类:
UserService.java:
package com.example.yourapp.service;  import com.example.yourapp.model.User;  import java.util.List;  public interface UserService {  User findUserById(Integer id);  List<User> findAllUsers();  int insertUser(User user);  int updateUser(User user);  int deleteUserById(Integer id);  
}UserServiceImpl.java:
package com.example.yourapp.service.impl;  import com.example.yourapp.mapper.UserMapper;  
import com.example.yourapp.model.User;  
import com.example.yourapp.service.UserService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Service;  import java.util.List;  @Service  
public class UserServiceImpl implements UserService {  @Autowired  private UserMapper userMapper;  @Override  public User findUserById(Integer id) {  return userMapper.findUserById(id);  }  // ... 其他方法的实现 ...  
}Controller类
在src/main/java/com/example/yourapp/controller目录下创建UserController.java:
package com.example.yourapp.controller;  import com.example.yourapp.model.User;  
import com.example.yourapp.service.UserService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.http.HttpStatus;  
import org.springframework.http.ResponseEntity;  
import org.springframework.web.bind.annotation.*;  import java.util.List;  @RestController  
@RequestMapping("/users")  
public class UserController {  @Autowired  private UserService userService;  @GetMapping("/{id}")  public ResponseEntity<User> getUserById(@PathVariable Integer id) {  User user = userService.findUserById(id);  if (user == null) {  return new ResponseEntity<>(HttpStatus.NOT_FOUND);  }  return new ResponseEntity<>(user, HttpStatus.OK);  }  @GetMapping("/")  public ResponseEntity<List<User>> getAllUsers() {  List<User> users = userService.findAllUsers();  return new ResponseEntity<>(users, HttpStatus.OK);  }  @PostMapping("/")  public ResponseEntity<Integer> createUser(@RequestBody User user) {  int result = userService.insertUser(user);  if (result == 1) {  return new ResponseEntity<>(user.getId(), HttpStatus.CREATED);  } else {  return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);  }  }  @PutMapping("/")  public ResponseEntity<Integer> updateUser(@RequestBody User user) {  int result = userService.updateUser(user);  if (result == 1) {  return new ResponseEntity<>(HttpStatus.OK);  } else {  return new ResponseEntity<>(HttpStatus.NOT_FOUND);  }  }  @DeleteMapping("/{id}")  public ResponseEntity<Void> deleteUserById(@PathVariable Integer id) {  int result = userService.deleteUserById(id);  if (result == 1) {  return new ResponseEntity<>(HttpStatus.NO_CONTENT);  } else {  return new ResponseEntity<>(HttpStatus.NOT_FOUND);  }  }  
}7. 测试和部署
- 编写测试用例或使用Postman等工具测试API接口。
- 打包项目为WAR文件,并部署到Tomcat或其他Servlet容器中。