SSM进阶学习系列丨整合篇:Spring + SpringMVC + MyBatis 框架配置详解
在Java EE开发中,Spring、Spring MVC和MyBatis这三大框架组合被广泛应用于企业级应用程序的开发中。它们各自承担着不同的角色,Spring负责控制反转(IoC)和面向切面编程(AOP),Spring MVC则负责处理用户请求及返回视图,而MyBatis用于持久层与数据库的交互。本文将详细介绍如何将这三者整合,并进行相应的配置。
1. 项目结构
首先,我们确定项目的基本结构如下:
ssm-integration
│
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── example
│ │ │ ├── controller
│ │ │ ├── dao
│ │ │ ├── service
│ │ │ └── model
│ │ └── resources
│ │ └── mybatis
│ │ └── config.xml
│ └── webapp
│ ├── WEB-INF
│ │ ├── web.xml
│ │ └── dispatcher-servlet.xml
│ └── index.jsp
└── pom.xml
2. Maven依赖
在 pom.xml
文件中添加所需依赖:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.12</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- 其他依赖 -->
</dependencies>
3. Spring配置
1. dispatcher-servlet.xml
这是Spring MVC的配置文件,主要用于配置控制器、视图解析和其他组件。
<context:component-scan base-package="com.example.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
2. Spring根配置
创建一个 applicationContext.xml
,用于配置Spring框架的上下文,包括数据源和MyBatis的SqlSessionFactory。
<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">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/yourdb" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/config.xml" />
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<context:component-scan base-package="com.example.dao,com.example.service" />
</beans>
4. MyBatis配置
在这个配置文件中,定义数据库的映射关系:
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/yourdb" />
<property name="username" value="root" />
<property name="password" value="password" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/dao/UserMapper.xml" />
</mappers>
</configuration>
5. 编写Controller和Service
Controller
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list")
public String listUsers(Model model) {
List<User> users = userService.getAllUsers();
model.addAttribute("users", users);
return "userList";
}
}
Service
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> getAllUsers() {
return userMapper.selectAllUsers();
}
}
6. Web.xml配置
最后,定义 web.xml
文件,设置DispatcherServlet和加载Spring应用上下文:
<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_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
</web-app>
总结
通过上述步骤,您已经成功整合了Spring、Spring MVC和MyBatis的基本配置。之后,您可以根据实际项目需求,继续扩展和完善这些配置。这种架构的优点在于分层清晰、职责明确,为企业级应用的开发提供了极大的便利。