SSM框架整合+简单案例实现

article/2025/9/18 14:42:19

SSM框架整合+简单案例实现

文章目录

  • 前言
  • 一、Spring+SpringMVC+Mybatis框架整合
    • 1.建立一个新的web项目
    • 2.所需jar包
    • 3.建立数据库表与实体类之间的映射
    • 4.web.xml
    • 5.数据库信息db.properties
    • 6.Spring与Mybatis的整合
    • 7.SpringMVC基本配置
  • 二、整合测试
    • 1.结构展示
    • 2.studentMapper.xml
    • 3.studentMapper.java接口
    • 4.StudentService接口
    • 5.StudentServiceImpl
    • 6.StudentController
    • 7.测试结果


前言

本文分为两个部分,第一部分为SSM框架整合,第二部分为测试整合框架,想看如何整合框架的朋友看完第一部分就可以离开了

在SSM框架的整合中,我们以Spring老大哥为基准,将各种需要交由Spring进行统一管理。


提示:以下是本篇文章正文内容,下面案例可供参考

一、Spring+SpringMVC+Mybatis框架整合

1.建立一个新的web项目

在这里插入图片描述

2.所需jar包

commons-dbcp-1.4.jar
commons-logging-1.1.1.jar
commons-pool-1.6.jar
log4j-1.2.17.jar
mybatis-3.5.6.jar
mybatis-spring-1.3.1.jar
mysql-connector-java-5.1.46.jar
spring-aop-4.3.9.RELEASE.jar
spring-beans-4.3.9.RELEASE.jar
spring-context-4.3.9.RELEASE.jar
spring-context-support-4.3.9.RELEASE.jar
spring-core-4.3.9.RELEASE.jar
spring-expression-4.3.9.RELEASE.jar
spring-jdbc-4.3.9.RELEASE.jar
spring-tx-4.3.9.RELEASE.jar
spring-web-4.3.9.RELEASE.jar
spring-webmvc-4.3.9.RELEASE.jar

3.建立数据库表与实体类之间的映射

表:
在这里插入图片描述

Stuednt实体类:

package org.lanqiao.entity;public class Student {private int stuNo;private String stuName;private int stuAge;public Student() {super();// TODO Auto-generated constructor stub}public Student(int stuNo, String stuName, int stuAge) {super();this.stuNo = stuNo;this.stuName = stuName;this.stuAge = stuAge;}public int getStuNo() {return stuNo;}public void setStuNo(int stuNo) {this.stuNo = stuNo;}public String getStuName() {return stuName;}public void setStuName(String stuName) {this.stuName = stuName;}public int getStuAge() {return stuAge;}public void setStuAge(int stuAge) {this.stuAge = stuAge;}}

4.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"><display-name>NewSSMProject</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- Spring --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- SpringMVC --><servlet><servlet-name>springDispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext-controller.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springDispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>

5.数据库信息db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/wxfwgl?useSSL=false
jdbc.user=root
jdbc.password=root

6.Spring与Mybatis的整合

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><!-- 整合Spring与Mybatis --><!-- 加载db.properties --><bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"><property name="locations"><array><value>classpath:db.properties</value></array></property></bean><!-- 连接池 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="${jdbc.driver}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property></bean><!-- sqlSessionFactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><!-- 加载mapper文件 --><property name="mapperLocations" value="classpath:org/lanqiao/mapper/*.xml" ></property></bean><!-- 以扫描包的方式生成mapper对象 --><bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property><property name="basePackage" value="org.lanqiao.mapper"></property></bean>
</beans>

7.SpringMVC基本配置

applicationContext-controller.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><!-- 配置视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/views/"></property><property name="suffix" value=".jsp"></property></bean><mvc:annotation-driven></mvc:annotation-driven></beans>

整合完毕,开始测试整合SSM的效果,我们对其进行一个简单实现

二、整合测试

我们实现一个点击超链接,显示学生信息的效果来测验框架的整合

1.结构展示

在这里插入图片描述

2.studentMapper.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"><!-- namespace:该mapper.xml映射文件的唯一标识 -->
<mapper namespace="org.lanqiao.mapper.StudentMapper"><select id="queryStudentByStuno" parameterType="int" resultType="org.lanqiao.entity.Student">select * from student where stuno = ${stuno}</select></mapper>

3.studentMapper.java接口

package org.lanqiao.mapper;import org.lanqiao.entity.Student;public interface StudentMapper {Student queryStudentByStuno(int stuno);
}

4.StudentService接口

package org.lanqiao.service;import org.lanqiao.entity.Student;
import org.springframework.stereotype.Service;public interface StudentService {Student queryStudentByStuno(int stuno);
}

5.StudentServiceImpl

package org.lanqiao.serviceImpl;import org.lanqiao.entity.Student;
import org.lanqiao.mapper.StudentMapper;
import org.lanqiao.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class StudentServiceImpl implements StudentService {@AutowiredStudentMapper studentMapper;@Overridepublic Student queryStudentByStuno(int stuno) {Student student = studentMapper.queryStudentByStuno(stuno);return student;}
}

在applicationContext.xml文件对其进行扫描

<context:component-scan base-package="org.lanqiao.serviceImpl"></context:component-scan>

6.StudentController

package org.lanqiao.controller;import org.lanqiao.entity.Student;
import org.lanqiao.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;@RequestMapping("controller")
@Controller
public class StudentController {@AutowiredStudentService studentService;@RequestMapping("queryStudentByStuno/{stuno}")public String queryStudentByStuno(@PathVariable("stuno") Integer stuno,Model model) {Student student = studentService.queryStudentByStuno(1);model.addAttribute("student", student);return "result";}}

在applicationContext-controller.xml文件中对其进行扫描

<context:component-scan base-package="org.lanqiao.controller"></context:component-scan>

7.测试结果

在这里插入图片描述

小测试成功,框架整合完毕


http://chatgpt.dhexx.cn/article/ECljb5eJ.shtml

相关文章

ssm框架的简单案例

学完ssm框架后做了一个简单的案例&#xff0c;实现简单的增删改查。 项目结构图&#xff1a; 看项目结构&#xff1a; 关键代码&#xff1a; 前端是jsp技术&#xff0c;当然也可以用vue分离的自己选择就好。 订单页面&#xff1a; <% page language"java&quo…

SSM框架开发-基础案例

SSM框架整合基础案例详解 1.数据库环境 创建一个存放书籍数据的数据库表 CREATE DATABASE ssmbuild;USE ssmbuild;DROP TABLE IF EXISTS books;CREATE TABLE books (bookID INT(10) NOT NULL AUTO_INCREMENT COMMENT 书id,bookName VARCHAR(100) NOT NULL COMMENT 书名,bookCou…

SSM框架简单实例

1、SSM框架 SSM&#xff08;SpringSpringMVCMyBatis&#xff09;框架集由Spring、SpringMVC、MyBatis三个开源框架整合而成&#xff0c;常作为数据源较简单的web项目的框架。 2、简单实例 &#xff08;1&#xff09;项目结构 &#xff08;2&#xff09;PageBean.java packa…

用SSM实现简单实例

SSM的简例实现 这是项目完整代码的链接地址 [https://download.csdn.net/download/m0_46308149/12961224]源码地址 SSM介绍 SSM&#xff08;SpringSpringMVCMyBatis&#xff09;框架集由Spring、MyBatis两个开源框架整合而成&#xff08;SpringMVC是Spring中的部分内容&am…

SpringMVC笔记——SSM框架搭建简单实例

简介 SpringSpringMVCMyBatis框架&#xff08;SSM&#xff09;是比较热门的中小型企业级项目开发的框架&#xff0c;对于新手来说也是比较容易学习入门的。虽说容易&#xff0c;但在框架搭建过程中仍然遇到了许多问题&#xff0c;因此用实例记录下来吧。 实例 第一步——导包 S…

SSM框架整合+案例

SSM框架整合 1、环境要求2、数据库环境3、基本环境搭建3.1 创建项目3.2 Maven项目添加web支持3.3 配置pom.xml文件3.4 建立框架的基本结构和配置文件3.4.1 创建包3.4.2 添加配置文件3.4.3 database.properties文件3.4.4 mybatis-config.xml 文件3.4.5 applicationContext.xml 文…

C# SqlHelper类的使用

SqlHelper类 1.首先SqlHelper类是一个基于.NET Framework的数据库操作组件&#xff0c;包含了数据库的操作方法。可以简化在C#中使用ADO.NET连接数据库时每次都要编写连接、打开、执行SQL语句的代码&#xff0c;它将每次连接都要写的代码封装成方法&#xff0c;把要执行的SQL语…

SqlHelper类(C#)

大神可以绕道了... 目的&#xff1a;搜集SqlHelper类 自己写的一个SQLHelper类&#xff0c;如下&#xff1a; 编辑App.config节点&#xff0c;添加<connectionStrings>节点&#xff0c;并根据实际填上相应的参数 <?xml version"1.0" encoding"utf…

SQLHelper

前言 小编在最近的学习过程中用到了SQLHelper&#xff0c;说起来&#xff0c;小编也是有点懒&#xff0c;虽然是用到了&#xff0c;但是也没有用明白&#xff0c;开始的时候也没有好好的研究&#xff0c;直到后来报错了&#xff0c;才下定决心好好好学习了解一下这个东西。下面…

sqlhelper 的使用 (C#)超级详细的入门教程

sql helper 的使用 &#xff08;C#&#xff09;小白教程 提到CRUD&#xff0c;很多刚入门的小白总是来一条写一条连接&#xff0c;先建立连接connection 打开连接 open 来查询query 最后别忘了关闭连接 close 。 要是一次写多个操作&#xff0c;那一遍一遍写下来肯定麻木了。…

Python中的BIF

什么是BIF呢&#xff1f; BIF是built-in functions的缩写&#xff0c;顾名思义&#xff0c;就是内建函数。Python中提供了大量的BIF&#xff0c;这就意味着代码量可以大大减少。 如果要查看Python中的内建函数&#xff0c;就可以使用命令&#xff1a;dir(__builtins__) 注意…

一、bif

缩进是python的灵魂&#xff0c;缩进可以使python的代码整洁&#xff0c;有层次。 python是脚本语言&#xff0c;就是为了简单方便以辅助科学运算&#xff0c;因此python有许多bif&#xff0c;build in function 全部都是小写的就是bif。 转义字符是一个字符&#xff0c;在内存…

FineBI

还是数据可视化工具Tableau、FineBI? 不禁联想起在微软系统出现之前&#xff0c;程序员的电脑系统还是用的linux&#xff0c;只能通过各种复杂的指令来实现字符的简单可视化&#xff1b;而当win系统普及于世后&#xff0c;计算机从此突破了技术人群的限制&#xff0c;交互方式…

Python学习笔记2(小甲鱼)—— 内置函数BIF

&#xfeff;&#xfeff; 这里有一个让人“亮瞎眼”的“游戏”开始python的学习。首先我们编写一段代码&#xff0c;来实现这个游戏。编写操作可参考前面的《Python学习笔记1——搭建环境与“Hello World”》这篇文章&#xff0c;网址&#xff1a;http://blog.csdn.net/tongbi…

BSDiff算法

https://blog.csdn.net/add_ada/article/details/51232889 BSDiff是一个差量更新算法&#xff0c;它在服务器端运行BSDiff算法产生patch包&#xff0c;在客户端运行BSPatch算法&#xff0c;将旧文件和patch包合成新文件。 差量更新算法的核心思想 尽可能多的利用old文件中已有…

MFBI

MFBI MFBI: Multi-Frequency Band Indicator 之前在写” Carrier frequency 和 EARFCN的关系”, 提到过MFBI. 简单的讲就是不同band间对应的frequency 的范围之间有overlapping, 一个frequency 可能对应多个band. 比如2595MHz 即属于band38 又属于band41. 关于那些band间存…

Python的内置函数(BIF)与变量的使用

Python的内置函数&#xff08;BIF&#xff09;与变量的使用 1、内置函数 使用dir()可查看所有的内置函数 dir(__builtins__)使用help()可查看内置函数的功能&#xff0c;例如&#xff1a; help(UserWarning)2、input函数 作用是在控制台输入数据&#xff0c;返回的是字符串…

Python内置函数(BIF)查询(附中文详解说明)

我们知道&#xff0c;Python 解释器内置了一些常量和函数&#xff0c;叫做内置常量&#xff08;Built-in Constants&#xff09;和内置函数&#xff08;Built-in Functions&#xff09;&#xff0c;来实现各种不同的特定功能&#xff0c;在我的另外一篇博客中 第8章&#xff1a…

【BF算法】

BF 算法 BF 算法精讲 在学习到字符串的匹配问题时&#xff0c;了解到了BF算法和KMP算法。 对比这两个算法&#xff0c;先了解BF算法&#xff1b; 字符串匹配问题&#xff0c;比如说&#xff1a;有一个主串 “abbbcdef” &#xff0c; 子串 “bbc”&#xff0c;该问题就是在主…

BIF

python3的内置函数