SpringBoot整合ssm
时间: 2023-09-12 08:03:25 浏览: 62
Spring Boot 是一个非常流行的 Java Web 开发框架,而 SSM 是 Spring + Spring MVC + MyBatis 的缩写,是一个经典的 Java Web 开发框架。将两者整合起来可以快速构建出高效且易于维护的 Java Web 应用程序。
下面是简单的整合步骤:
1. 引入依赖
在 pom.xml 中引入 Spring Boot、Spring MVC 和 MyBatis 的相关依赖,例如:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
```
2. 配置数据源
在 application.properties 中配置数据源信息,例如:
```properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
```
3. 配置 MyBatis
在 MyBatis 的配置文件中配置数据源和 mapper,例如:
```xml
<configuration>
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${spring.datasource.driver-class-name}" />
<property name="url" value="${spring.datasource.url}" />
<property name="username" value="${spring.datasource.username}" />
<property name="password" value="${spring.datasource.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/demo/mapper/UserMapper.xml" />
</mappers>
</configuration>
```
4. 配置 Spring MVC
在 Spring MVC 的配置文件中配置扫描包和视图解析器,例如:
```xml
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="com.example.demo.controller" />
```
5. 编写代码
编写控制器、服务和 DAO 层的代码,例如:
```java
@RestController
@RequestMapping("/api")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user/{id}")
public User getUserById(@PathVariable("id") int id) {
return userService.getUserById(id);
}
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(int id) {
return userMapper.getUserById(id);
}
}
public interface UserMapper {
User getUserById(int id);
}
```
6. 运行程序
使用 Spring Boot 的启动器运行程序,例如:
```java
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
以上是一个简单的 Spring Boot 整合 SSM 的过程,当然还有更复杂的场景和技术细节需要处理,但大致流程是相似的。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)