• SSM框架整合



    目录

    一.整合环境搭建

    1.整合思路

    2.准备所需jar包

     3.编写配置文件

    二.整合测试


    活动地址:CSDN21天学习挑战赛

    一.整合环境搭建

    Spring MVC 是Spring 框架的一个模块,那么Spring MVC与Spring之间不需要整合,只需要引入相应的jar包就可以直接使用。SSM框架的整合只需要在Spring 与MyBatis之间和Spring MVC 与MyBatis之间进行整合。

    1.整合思路

    通过 Spring实例化Bean,然后调用实例对象中的查询方法来执行 MyBatis映射文件中的SQL语句,如果能够正确查询出数据库中的数据,那么就可以认为 SpringMyBatis框架整合成功。如果可以通过前台页面来执行查询方法,并且查询出数据能在页面中正确显示,可以认为SSM三大框架整合成功。

    2.准备所需jar包

     3.编写配置文件

    (1).在eclipse中创建一个动态web项目,并将上述jar包添加到lib目录下。

    (2).在src目录下创建数据库常量配置文件db.properties,Spring配置文件applicationContext.xml以及MyBatis的配置文件mybatis-config.xml和Spring MVC的配置文件springmvc-config.xml。

    db.properties

    1. jdbc.driver=com.mysql.cj.jdbc.Driver
    2. jdbc.url=jdbc:mysql://localhost:3306/db_mybatis?serverTimezone=UTC
    3. jdbc.username=root
    4. jdbc.password=root
    5. jdbc.maxTotal=30
    6. jdbc.maxIdle=10
    7. jdbc.initialSize=5

     applicationContext.xml

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:aop="http://www.springframework.org/schema/aop"
    4. xmlns:tx="http://www.springframework.org/schema/tx"
    5. xmlns:context="http://www.springframework.org/schema/context"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans
    7. http://www.springframework.org/schema/beans/spring-beans.xsd
    8. http://www.springframework.org/schema/tx
    9. http://www.springframework.org/schema/tx/spring-tx.xsd
    10. http://www.springframework.org/schema/context
    11. http://www.springframework.org/schema/context/spring-context.xsd
    12. http://www.springframework.org/schema/aop
    13. http://www.springframework.org/schema/aop/spring-aop.xsd">
    14. <context:property-placeholder location="classpath:db.properties"/>
    15. <bean id="dataSource"
    16. class="org.apache.commons.dbcp2.BasicDataSource">
    17. <property name="driverClassName" value="${jdbc.driver}" />
    18. <property name="url" value="${jdbc.url}" />
    19. <property name="username" value="${jdbc.username}" />
    20. <property name="password" value="${jdbc.password}" />
    21. <property name="maxTotal" value="${jdbc.maxTotal}" />
    22. <property name="maxIdle" value="${jdbc.maxIdle}" />
    23. <property name="initialSize" value="${jdbc.initialSize}" />
    24. bean>
    25. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    26. <property name="dataSource" ref="dataSource"/>
    27. bean>
    28. <tx:annotation-driven transaction-manager="transactionManager"/>
    29. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    30. <property name="dataSource" ref="dataSource" />
    31. <property name="configLocation" value="classpath:mybatis-config.xml" />
    32. bean>
    33. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    34. <property name="basePackage" value="com.ssm.dao">property>
    35. bean>
    36. <context:component-scan base-package="com.ssm.service"/>
    37. beans>

     注意:

    在实际开发时,为了避免Spring配置文件中的信息过于臃肿,通常会将Spring配置文件中的信息按照不同的功能分散在多个配置文件中。例如可以将事务配置放置在名称为 applicationContext-transaction.xml的文件中,将数据源等信息放置在名称为applicationContext-db.xml的文件中等。在web.xml中配置加载Spring文件信息时,只需通过applicationContext-*.xml的方式即可自动加载全部配置文件。

     mybatis-config.xml

    1. configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    2. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    3. <configuration>
    4. <typeAliases>
    5. <package name="com.ssm.po"/>
    6. typeAliases>
    7. configuration>

     springmvc-config.xml

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:mvc="http://www.springframework.org/schema/mvc"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:tx="http://www.springframework.org/schema/tx"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans
    7. http://www.springframework.org/schema/beans/spring-beans.xsd
    8. http://www.springframework.org/schema/mvc
    9. http://www.springframework.org/schema/mvc/spring-mvc.xsd
    10. http://www.springframework.org/schema/context
    11. http://www.springframework.org/schema/context/spring-context.xsd">
    12. <context:component-scan base-package="com.ssm.controller" />
    13. <mvc:annotation-driven />
    14. <bean id="viewResoler"
    15. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    16. <property name="prefix" value="/WEB-INF/jsp/" />
    17. <property name="suffix" value=".jsp" />
    18. bean>
    19. beans>

    (3).在webapp的WEB-INF目录下创建web.xml,用于配置Spring 的文件监听器,编码过滤器以及Spring MVC的前端控制器等信息

    1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    4. http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5. id="WebApp_ID" version="4.0">
    6. <context-param>
    7. <param-name>contextConfigLocationparam-name>
    8. <param-value>classpath:applicationContext.xmlparam-value>
    9. context-param>
    10. <listener>
    11. <listener-class>
    12. org.springframework.web.context.ContextLoaderListener
    13. listener-class>
    14. listener>
    15. <filter>
    16. <filter-name>encodingfilter-name>
    17. <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    18. <init-param>
    19. <param-name>encodingparam-name>
    20. <param-value>UTF-8param-value>
    21. init-param>
    22. filter>
    23. <filter-mapping>
    24. <filter-name>encodingfilter-name>
    25. <url-pattern>*.actionurl-pattern>
    26. filter-mapping>
    27. <servlet>
    28. <servlet-name>springmvcservlet-name>
    29. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    30. <init-param>
    31. <param-name>contextConfigLocationparam-name>
    32. <param-value>classpath:springmvc-config.xmlparam-value>
    33. init-param>
    34. <load-on-startup>1load-on-startup>
    35. servlet>
    36. <servlet-mapping>
    37. <servlet-name>springmvcservlet-name>
    38. <url-pattern>/url-pattern>
    39. servlet-mapping>
    40. web-app>

    二.整合测试

     (1).创建一个数据库db_mybatis,在数据库内创建表t_user并插入数据

    1. create database db_mybatis;
    2. use db_mybatis;
    3. create table t_user(
    4. id int(32) primary key auto_increment ,
    5. username varchar(30)
    6. );
    7. insert into t_user values(1,'zyy');
    8. insert into t_user values(2,'wrr');
    9. insert into t_user values(3,'xxc');

    (2).在src目录下创建一个com.ssm.po包,在包中创建实体类User

    1. package com.ssm.po;
    2. public class User {
    3. private Integer id;
    4. private String username;
    5. public Integer getId() {
    6. return id;
    7. }
    8. public void setId(Integer id) {
    9. this.id = id;
    10. }
    11. public String getUsername() {
    12. return username;
    13. }
    14. public void setUsername(String username) {
    15. this.username = username;
    16. }
    17. }

    (3).在src目录下创建一个com.ssm.dao包,在包中创建接口文件UserDao以及对应的映射文件UserDao.xml

    1. package com.ssm.dao;
    2. import com.ssm.po.User;
    3. /*
    4. * User接口文件
    5. */
    6. public interface UserDao {
    7. /*
    8. * 根据id查询用户信息
    9. */
    10. public User findUserById(Integer id);
    11. }
    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. <mapper namespace="com.ssm.dao.UserDao">
    4. <select id="findUserById" parameterType="Integer" resultType="User">
    5. select * from t_user where id=#{id}
    6. select>
    7. mapper>

    (4).在src目录下创建一个com.ssm.service包,然后在包中创建接口文件UserService,并在UserService中定义通过id查询客户的方法。

    1. package com.ssm.service;
    2. import com.ssm.po.User;
    3. public interface UserService {
    4. public User findUserById(Integer id);
    5. }

    (5).在src目录下创建一个com.ssm.service.impl包,并在包中创建UserService接口的实现类UserServiceImpl

    1. package com.ssm.service.impl;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.stereotype.Service;
    4. import org.springframework.transaction.annotation.Transactional;
    5. import com.ssm.dao.UserDao;
    6. import com.ssm.po.User;
    7. import com.ssm.service.UserService;
    8. @Service
    9. //使用@Service注解标识业务层的实现类
    10. @Transactional
    11. //使用@Transactional注解标识类中的所有方法纳入Spring的事务管理
    12. public class UserServiceImpl implements UserService {
    13. @Autowired
    14. private UserDao userDao;
    15. public User findUserById(Integer id) {
    16. return this.userDao.findUserById(id);
    17. }
    18. }

    (6).在src目录下创建一个com.ss.controller包,并在包中创建用于处理页面请求的控制器类UserController

    1. package com.ssm.controller;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.ui.Model;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import com.ssm.po.User;
    7. import com.ssm.service.UserService;
    8. @Controller
    9. public class UserController {
    10. @Autowired
    11. private UserService userService;
    12. /*
    13. * 根据id查询用户详情
    14. */
    15. @RequestMapping("/findUserById")
    16. public String findUserById(Integer id,Model model){
    17. User user=userService.findUserById(id);
    18. model.addAttribute("user", user);
    19. //返回用户信息展示页面
    20. return "user";
    21. }
    22. }

    (7).在webapp目录下创建user.jsp,用于展示客户详情页面

    1. <%@ page language="java" contentType="text/html; charset=UTF-8"
    2. pageEncoding="UTF-8"%>
    3. html>
    4. <html>
    5. <head>
    6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    7. <title>用户信息title>
    8. head>
    9. <body>
    10. <table>
    11. <tr>
    12. <td>用户ID:td>
    13. <td>${user.id}td>
    14. tr>
    15. <tr>
    16. <td>用户姓名:td>
    17. <td>${user.username}td>
    18. tr>
    19. table>
    20. body>
    21. html>

    (8).运行项目

    在浏览器运行localhost:8848/chapter15/findUserById?id=1

    效果

     注意:

    在实际项目开发中,不单纯是查询信息,还存在包括增加、修改和删除在内的各种复杂业务。所示案例只是测试SSM三个框架的整合,即整合和测试三个框架是否能够“协同工作”。

    源码地址:

    链接:https://pan.baidu.com/s/1gKdG2AZzk35-A2VBdIkmpw?pwd=79bl 
    提取码:79bl

  • 相关阅读:
    2311rust,到50版本更新
    Zookeeper(一)- Zookeeper介绍与集群部署
    Activiti 7 启动流程实例
    Windows 控制台程序的 binary 输出
    特征降维(特征工程)
    2022年信息安全工程师考试知识点:访问控制
    目标检测中的解耦和耦合、anchor-free和anchor-base
    【MTK】【WIFI】手机和综测仪器误差在5db左右误差
    Linux之 rsyslog、日志轮转
    随想录一刷Day45——动态规划
  • 原文地址:https://blog.csdn.net/weixin_52473454/article/details/126293697