SSM整合是指Spring + Spring MVC + MyBatis三个框架的整合,它们是Java企业级开发中常用的框架。Spring作为IoC容器和AOP框架,能够管理对象的生命周期和提供各种服务;Spring MVC是一种基于MVC模式的Web框架,主要用于请求和响应的处理;MyBatis是一种优秀的持久层框架,利用它可以方便地操作数据库。
SSM整合的好处是结合了三个框架的优点,能够提高开发效率、降低维护成本、方便测试和调试等。在SSM整合中,Spring负责管理对象,MyBatis进行数据库操作,Spring MVC接收请求并进行处理,整合后能够实现前端与后台的高效交互和数据的持久化存储,是一种优秀的开发模式。
以下是一个简单的SSM整合示例:
- 创建一个Maven项目,导入Spring、Spring MVC、MyBatis依赖:
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
- 创建数据库表和实体类:
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`age` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
public class User {
private Integer id;
private String name;
private Integer age;
// 省略getter和setter
}
- 创建Mapper接口和Mapper映射文件:
public interface UserMapper {
int insert(User user);
int update(User user);
int deleteById(Integer id);
User getById(Integer id);
List<User> getAll();
}
<mapper namespace="com.example.mapper.UserMapper">
<insert id="insert" parameterType="com.example.entity.User" useGeneratedKeys="true" keyProperty="id">
INSERT INTO user(name, age) VALUES(#{name}, #{age})
</insert>
<update id="update" parameterType="com.example.entity.User">
UPDATE user SET name=#{name}, age=#{age} WHERE id=#{id}
</update>
<delete id="deleteById" parameterType="java.lang.Integer">
DELETE FROM user WHERE id=#{id}
</delete>
<select id="getById" parameterType="java.lang.Integer" resultMap="userResult">
SELECT * FROM user WHERE id=#{id}
</select>
<select id="getAll" resultMap="userResult">
SELECT * FROM user
</select>
<resultMap id="userResult" type="com.example.entity.User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap>
</mapper>
- 配置Spring、Spring MVC和MyBatis:
<!-- Spring配置 -->
<context:component-scan base-package="com.example"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.mapper"/>
</bean>
<!-- Spring MVC配置 -->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<context:annotation-config/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- MyBatis配置 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.mapper"/>
</bean>
- 创建Controller:
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(User user) {
userMapper.insert(user);
return "redirect:/user/list";
}
@RequestMapping(value = "/list", method = RequestMethod.GET)
public ModelAndView list() {
List<User> userList = userMapper.getAll();
return new ModelAndView("user/list", "userList", userList);
}
}
- 创建JSP页面:
<!-- 添加用户页面 -->
<form:form action="${pageContext.request.contextPath}/user/add" method="POST" modelAttribute="user">
<label>Name:</label>
<form:input path="name"/>
<br/>
<label>Age:</label>
<form:input path="age"/>
<br/>
<button type="submit">Add User</button>
</form:form>
<!-- 用户列表页面 -->
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>
</c:forEach>
以上就是一个简单的SSM整合示例,它实现了一个简单的用户管理系统,包括添加用户和查看所有用户。