SSM框架整合springmvc代码
时间: 2023-08-04 16:34:29 浏览: 144
好的,下面是一个简单的SSM框架整合SpringMVC的代码示例。这个示例使用了Spring Boot来快速搭建框架,但是SSM框架整合SpringMVC的步骤与之类似。希望这个示例能够帮助到你。
1. pom.xml文件中的依赖配置:
```xml
<dependencies>
<!-- Spring Boot web starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot test starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- MyBatis starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- MySQL connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
</dependencies>
```
2. application.properties配置文件中的配置项:
```
# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
# MyBatis配置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.entity
# Spring MVC配置
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
```
3. Spring的配置文件(application.xml)中的配置:
```xml
<!-- 数据源配置 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${spring.datasource.url}"/>
<property name="username" value="${spring.datasource.username}"/>
<property name="password" value="${spring.datasource.password}"/>
</bean>
<!-- MyBatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.example.demo.entity"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.demo.mapper"/>
</bean>
<!-- 事务配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
```
4. SpringMVC的配置文件(springmvc.xml)中的配置:
```xml
<!-- ViewResolver配置 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="${spring.mvc.view.prefix}"/>
<property name="suffix" value="${spring.mvc.view.suffix}"/>
</bean>
<!-- 静态资源配置 -->
<mvc:resources location="/static/**" mapping="/static/**"/>
<!-- RequestMappingHandlerMapping配置 -->
<mvc:annotation-driven/>
```
5. Mapper接口和Mapper.xml文件的实现:
UserMapper.java
```java
public interface UserMapper {
User getUserById(int id);
}
```
UserMapper.xml
```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.demo.mapper.UserMapper">
<select id="getUserById" parameterType="int" resultType="com.example.demo.entity.User">
select * from user where id = #{id}
</select>
</mapper>
```
6. Controller类的实现:
```java
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/{id}")
@ResponseBody
public User getUserById(@PathVariable int id) {
return userMapper.getUserById(id);
}
}
```
以上就是一个简单的SSM框架整合SpringMVC的代码示例。希望能够对你有所帮助。
阅读全文