目录
活动地址:CSDN21天学习挑战赛
Spring MVC 是Spring 框架的一个模块,那么Spring MVC与Spring之间不需要整合,只需要引入相应的jar包就可以直接使用。SSM框架的整合只需要在Spring 与MyBatis之间和Spring MVC 与MyBatis之间进行整合。
通过 Spring实例化Bean,然后调用实例对象中的查询方法来执行 MyBatis映射文件中的SQL语句,如果能够正确查询出数据库中的数据,那么就可以认为 Spring与 MyBatis框架整合成功。如果可以通过前台页面来执行查询方法,并且查询出数据能在页面中正确显示,可以认为SSM三大框架整合成功。

(1).在eclipse中创建一个动态web项目,并将上述jar包添加到lib目录下。
(2).在src目录下创建数据库常量配置文件db.properties,Spring配置文件applicationContext.xml以及MyBatis的配置文件mybatis-config.xml和Spring MVC的配置文件springmvc-config.xml。
db.properties
- jdbc.driver=com.mysql.cj.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/db_mybatis?serverTimezone=UTC
- jdbc.username=root
- jdbc.password=root
- jdbc.maxTotal=30
- jdbc.maxIdle=10
- jdbc.initialSize=5
applicationContext.xml
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd">
-
- <context:property-placeholder location="classpath:db.properties"/>
-
- <bean id="dataSource"
- class="org.apache.commons.dbcp2.BasicDataSource">
-
- <property name="driverClassName" value="${jdbc.driver}" />
-
- <property name="url" value="${jdbc.url}" />
-
- <property name="username" value="${jdbc.username}" />
-
- <property name="password" value="${jdbc.password}" />
-
- <property name="maxTotal" value="${jdbc.maxTotal}" />
-
- <property name="maxIdle" value="${jdbc.maxIdle}" />
-
- <property name="initialSize" value="${jdbc.initialSize}" />
- bean>
-
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- bean>
-
- <tx:annotation-driven transaction-manager="transactionManager"/>
-
- <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.ssm.dao">property>
- bean>
-
- <context:component-scan base-package="com.ssm.service"/>
- beans>
注意:
在实际开发时,为了避免Spring配置文件中的信息过于臃肿,通常会将Spring配置文件中的信息按照不同的功能分散在多个配置文件中。例如可以将事务配置放置在名称为 applicationContext-transaction.xml的文件中,将数据源等信息放置在名称为applicationContext-db.xml的文件中等。在web.xml中配置加载Spring文件信息时,只需通过applicationContext-*.xml的方式即可自动加载全部配置文件。
mybatis-config.xml
- configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
-
- <typeAliases>
- <package name="com.ssm.po"/>
- typeAliases>
- configuration>
springmvc-config.xml
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- 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/mvc
- http://www.springframework.org/schema/mvc/spring-mvc.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd">
-
- <context:component-scan base-package="com.ssm.controller" />
-
- <mvc:annotation-driven />
-
- <bean id="viewResoler"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
- <property name="prefix" value="/WEB-INF/jsp/" />
-
- <property name="suffix" value=".jsp" />
- bean>
- beans>
(3).在webapp的WEB-INF目录下创建web.xml,用于配置Spring 的文件监听器,编码过滤器以及Spring MVC的前端控制器等信息
- <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"
- id="WebApp_ID" version="4.0">
-
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:applicationContext.xmlparam-value>
- context-param>
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- listener-class>
- listener>
-
- <filter>
- <filter-name>encodingfilter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
- <init-param>
- <param-name>encodingparam-name>
- <param-value>UTF-8param-value>
- init-param>
- filter>
- <filter-mapping>
- <filter-name>encodingfilter-name>
- <url-pattern>*.actionurl-pattern>
- filter-mapping>
-
- <servlet>
-
- <servlet-name>springmvcservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
-
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:springmvc-config.xmlparam-value>
- init-param>
-
- <load-on-startup>1load-on-startup>
- servlet>
- <servlet-mapping>
- <servlet-name>springmvcservlet-name>
-
- <url-pattern>/url-pattern>
- servlet-mapping>
- web-app>
(1).创建一个数据库db_mybatis,在数据库内创建表t_user并插入数据
- create database db_mybatis;
- use db_mybatis;
- create table t_user(
- id int(32) primary key auto_increment ,
- username varchar(30)
- );
- insert into t_user values(1,'zyy');
- insert into t_user values(2,'wrr');
- insert into t_user values(3,'xxc');
(2).在src目录下创建一个com.ssm.po包,在包中创建实体类User
- package com.ssm.po;
- public class User {
- private Integer id;
- private String username;
-
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- }
(3).在src目录下创建一个com.ssm.dao包,在包中创建接口文件UserDao以及对应的映射文件UserDao.xml
- package com.ssm.dao;
- import com.ssm.po.User;
- /*
- * User接口文件
- */
- public interface UserDao {
- /*
- * 根据id查询用户信息
- */
- public User findUserById(Integer id);
- }
- mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.ssm.dao.UserDao">
-
- <select id="findUserById" parameterType="Integer" resultType="User">
- select * from t_user where id=#{id}
- select>
- mapper>
(4).在src目录下创建一个com.ssm.service包,然后在包中创建接口文件UserService,并在UserService中定义通过id查询客户的方法。
- package com.ssm.service;
- import com.ssm.po.User;
- public interface UserService {
- public User findUserById(Integer id);
- }
(5).在src目录下创建一个com.ssm.service.impl包,并在包中创建UserService接口的实现类UserServiceImpl
- package com.ssm.service.impl;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import com.ssm.dao.UserDao;
- import com.ssm.po.User;
- import com.ssm.service.UserService;
- @Service
- //使用@Service注解标识业务层的实现类
- @Transactional
- //使用@Transactional注解标识类中的所有方法纳入Spring的事务管理
- public class UserServiceImpl implements UserService {
- @Autowired
- private UserDao userDao;
- public User findUserById(Integer id) {
- return this.userDao.findUserById(id);
- }
- }
(6).在src目录下创建一个com.ss.controller包,并在包中创建用于处理页面请求的控制器类UserController
- package com.ssm.controller;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
- import com.ssm.po.User;
- import com.ssm.service.UserService;
- @Controller
- public class UserController {
- @Autowired
- private UserService userService;
- /*
- * 根据id查询用户详情
- */
- @RequestMapping("/findUserById")
- public String findUserById(Integer id,Model model){
- User user=userService.findUserById(id);
- model.addAttribute("user", user);
- //返回用户信息展示页面
- return "user";
- }
- }
(7).在webapp目录下创建user.jsp,用于展示客户详情页面
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>用户信息title>
- head>
- <body>
- <table>
- <tr>
- <td>用户ID:td>
- <td>${user.id}td>
- tr>
- <tr>
- <td>用户姓名:td>
- <td>${user.username}td>
- tr>
- table>
- body>
- html>
(8).运行项目
在浏览器运行localhost:8848/chapter15/findUserById?id=1
效果

注意:
在实际项目开发中,不单纯是查询信息,还存在包括增加、修改和删除在内的各种复杂业务。所示案例只是测试SSM三个框架的整合,即整合和测试三个框架是否能够“协同工作”。
源码地址:
链接:https://pan.baidu.com/s/1gKdG2AZzk35-A2VBdIkmpw?pwd=79bl
提取码:79bl