SSM整合过程梳理

article/2025/10/28 2:04:29

文章目录

  • 前言
    • 一.SSM整合流程
    • 二.整合配置
      • 2.1添加依赖
      • 2.2创建项目包结构
      • 2.3创建SpringConfig配置类
      • 2.4创建JdbcConfig配置类
      • 2.5创建MybatisConfig配置类
      • 2.6创建jdbc.properties
      • 2.7创建SpringMVC配置类
      • 2.8创建Web项目入口配置类
    • 三.功能模块
      • 3.1创建模型类
      • 3.2编写Dao接口
      • 3.3编写Service接口和实现类
      • 3.4编写Contorller类
    • 四.单元测试
      • 4.1新建测试类
      • 4.2注入Service类
      • 4.3编写测试方法

前言

框架用的好效率自然高!

🏳️‍🌈给大家推荐一个Java面试刷题网站牛客网,懒羊羊祝你早日成神:Java刷题面试宝藏网站

一.SSM整合流程

1.创建工程

  • 创建一个Maven的web工程
  • pom.xml添加SSM需要的依赖jar包
  • 编写Web项目的入口配置类,实现AbstractAnnotationConfigDispatcherServletInitializer重写以下方法
    • getRootConfigClasses() :返回Spring的配置类->需要SpringConfig配置类
    • getServletConfigClasses() :返回SpringMVC的配置类->需要SpringMvcConfig配置类
    • getServletMappings() : 设置SpringMVC请求拦截路径规则
    • getServletFilters() :设置过滤器,解决POST请求中文乱码问题

2.SSM整合
Spring

SpringConfigSpring配置类
标识该类为配置类@Configuration
扫描Service所在的包@ComponentScan
在Service层要管理事务@EnableTransactionManagement
读取外部的properties配置文件@PropertySource
整合Mybatis需要引入Mybatis相关配置类@Import

第三方数据源配置类 JdbcConfig

第三方数据源配置类JdbcConfig
构建DataSource数据源,DruidDataSouroce@Bean @Value
构建平台事务管理器,DataSourceTransactionManager@Bean

Mybatis配置类 MybatisConfig

第三方数据源配置类JdbcConfig
构建SqlSessionFactoryBean,设置别名扫描与数据源@Bean
构建MapperScannerConfigurer并设置DAO层的包扫描@Bean

SpringMVC配置类SpringMvcConfig

SpringMVC配置类SpringMvcConfig
标识该类为配置类@Configuration
扫描Controller所在的包@Configuration
开启SpringMVC注解支持@EnableWebMvc

二.整合配置

2.1添加依赖

pom.xml添加SSM所需要的依赖jar包
在这里插入图片描述

2.2创建项目包结构

  • config目录存放的是相关的配置类
  • controller编写的是Controller类
  • dao存放的是Dao接口,因为使用的是Mapper接口代理方式,所以没有实现类包
  • service存的是Service接口,impl存放的是Service实现类
  • resources:存入的是配置文件,如Jdbc.properties
  • webapp:目录可以存放静态资源
  • test/java:存放的是测试类
    在这里插入图片描述

2.3创建SpringConfig配置类

在这里插入图片描述

2.4创建JdbcConfig配置类

在这里插入图片描述

2.5创建MybatisConfig配置类

在这里插入图片描述

2.6创建jdbc.properties

在这里插入图片描述

2.7创建SpringMVC配置类

在这里插入图片描述

2.8创建Web项目入口配置类

三.功能模块

3.1创建模型类

public class Book {private Integer id;private String type;private String name;private String description;}

3.2编写Dao接口

public interface BookDao {//    @Insert("insert into tbl_book values(null,#{type},#{name},#{description})")@Insert("insert into tbl_book (type,name,description) values(#{type},#{name},#{description})")public void save(Book book);@Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")public void update(Book book);@Delete("delete from tbl_book where id = #{id}")public void delete(Integer id);@Select("select * from tbl_book where id = #{id}")public Book getById(Integer id);@Select("select * from tbl_book")public List<Book> getAll();
}

3.3编写Service接口和实现类

@Transactional
public interface BookService {public boolean save(Book book);public boolean update(Book book);public boolean delete(Integer id);public Book getById(Integer id);public List<Book> getAll();
}
@Service
public class BookServiceImpl implements BookService {@Autowiredprivate BookDao bookDao;public boolean save(Book book) { bookDao.save(book); return true; }public boolean update(Book book) { bookDao.update(book); return true; }public boolean delete(Integer id) {bookDao.delete(id); return true; }public Book getById(Integer id) { return bookDao.getById(id); }public List<Book> getAll() { return bookDao.getAll(); }
}

3.4编写Contorller类

@RestController
@RequestMapping("/books")
public class BookController {@Autowiredprivate BookService bookService;@PostMappingpublic boolean save(@RequestBody Book book) {return bookService.save(book);}@PutMappingpublic boolean update(@RequestBody Book book) {return bookService.update(book);}@DeleteMapping("/{id}")public boolean delete(@PathVariable Integer id) {return bookService.delete(id);}@GetMapping("/{id}")public Book getById(@PathVariable Integer id) {return bookService.getById(id);}@GetMappingpublic List<Book> getAll() {return bookService.getAll();}
}

四.单元测试

4.1新建测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class BookServiceTest {}

4.2注入Service类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class BookServiceTest {@Autowiredprivate BookService bookService;
}

4.3编写测试方法

我们先来对查询进行单元测试。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class BookServiceTest {@Autowiredprivate BookService bookService;@Testpublic void testGetById(){Book book = bookService.getById(1);System.out.println(book);}@Testpublic void testGetAll(){List<Book> all = bookService.getAll();System.out.println(all);}}

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

相关文章

ssm整合详解

最近做项目用到了ssm,虽然以前用过ssm但这段时间发现&#xff0c;用过不代表就会了&#xff0c;即使以前用过&#xff0c;但现在要搭一个ssm框架不看教程还是很难&#xff0c;最基本的maven仓库需要哪些坐标都搞不清楚&#xff0c;所以今天打算写篇博客梳理一下。 一.基础梳理…

SSM整合分页插件

目录 一.环境配置 1.分页插件依赖 2.在Mybatis里面配置的内容 二.使用分页插件 1.在ServiceImpl层开启分页插件&#xff08;即查询数据库前开启&#xff09; 2.控制器方法 3.jsp页面进行整理 4.效果 5. 样式 6.常用的数据说明 一.环境配置 1.分页插件依赖 <depend…

SSM整合总结

这几天学完spring&#xff0c;springMVC&#xff0c;mybatis后&#xff0c;这两天试着去整合ssm&#xff0c;整合过程并不顺利&#xff0c;一是基础知识有的忘了&#xff0c;细节不能把握住&#xff0c;造成各种报错.看了各种视频&#xff0c;翻阅各种文章&#xff0c;多多少少…

SSM整合shiro

1.完成ssm整合shiro 企业中老项目还在使用ssm框架。 准备数据库 数据结构 张三 -user:query user:add user:update user:delete 李四 ---》user:query user:add user:update 王五-----》user:query user:export 搭建ssm的环境 &#xff08;1&#xff09;创建一个maven的web工…

SSM整合

1、ssm整合原理 SSM框架是spring MVC &#xff0c;spring和mybatis框架的整合&#xff0c;是标准的MVC模式&#xff0c;将整个系统划分为表现层&#xff0c;controller层&#xff0c;service层&#xff0c;DAO层四层。 使用spring MVC负责请求的转发和视图管理 spring实现业…

SSM整合,手把手教程,详解思路讲解

前言 一&#xff0c;工程创建 1.新建Maven项目&#xff0c;项目架构增加web支持 2.Maven本地仓库配置 3.集成Tomcat&#xff0c;搭建web环境 二&#xff0c;导入依赖 三&#xff0c;配置文件编写 1.web环境配置文件 2.spring整合环境配置文件 2.1 controller层(spring…

SSM整合完整流程讲解

目录 一、SSM整合说明 1、各个框架担任的角色 2、两个Ioc容器的创建顺序 1&#xff09;SpringMVC中IOC容器的创建时间 2&#xff09;Spring中IOC容器的创建时间 3&#xff09;Spring提供的监听器ContextLoaderListener 二、SSM整合步骤 1、准备工作 1&#xff09;导入…

Unresolved Dependencies

在Android Studio的开发中&#xff0c;在软件中集成了ButterKnife插件&#xff0c;另外需要集成ButterKnife的jar包。因为本地没有现成的&#xff0c;所以在module的build.gradle文件中添加了如下代码&#xff1a; compile com.jakewharton:butterknife:7.0.1 结果报了如下错误…

Pycharm 出现Unresolved reference ‘‘ 错误的解决方法 --- 亲测有效

在用Pycharm写项目的时候的时候碰到一个很无语的问题 路径明明没有问题&#xff0c;运行也没有出错&#xff0c;但就是爆红&#xff0c;逼死强迫症啊。。。 多方查找最后解决了。步骤如下&#xff1a; File–>Settings–>Project Structure–>找到问题目录–>Sou…

PyCharm错误提示- Unresolved reference 的解决

使用PyCharm加载工程时,发生解析错误 原本这个工程是可以通过 python manange.py runserver 方式运行的,说明文件是正常的。关键在于PyCharm环境的问题。 查找了半天,安装一些网上的解决方式,尝试了以下几种方式: 1,修改忽略文件 File –>Settings –>Editor –…

Goland 提示 Unresolved reference 错误解决

之前一直正常的项目&#xff0c;莫名其妙的 database/sql 包下的方法、结构体等等IDE都无法识别&#xff0c;出现一堆Unresolved reference错误提示&#xff0c;但包导入路径可以正确定位&#xff0c;而且项目运行也正常&#xff0c;其他包都正常&#xff0c;另一个项目下同样使…

error LNK2019: unresolved external symbol

error LNK2019: unresolved external symbol&#xff1a;链接器找不到所需要的东西 此时ctrlF7进行编译&#xff0c;没有报错&#xff0c;在这个程序中调用的是Log函数&#xff0c;我们实际上并没有这个函数&#xff0c; 我们有的函数是Logr函数&#xff0c;在编译过程中编译器…

Unresolved reference ‘matplotlib‘解决方法

首先打开最左边的file-settings 然后点这个添加 找想添加的 最后点最下面的install Package 就可以啦

快速解决Pycharm中unresolved reference

在用Pycharm时老是报红&#xff0c;虽然不影响运行&#xff0c;但是真的很不好看。 如下&#xff1a; 根据网上大部分教程设置source root&#xff0c;设置之后还是没有变化。 然后我发现了这个东西&#xff0c;真的是立即生效&#xff0c;就是不知道改了之后有没有什么后遗症…

解决unresolved symbol _RamfuncsLoadEnd

问题描述 移植大佬的文件之后&#xff0c;发现报错 问题解决 添加F28335.cmd到工程中

Redisson cannot use an unresolved DNS server address问题解决

概述 本文记录Mac IDEA开发&#xff0c;公司 远程办公时遇到的两个问题&#xff0c;记录一下。 问题 cannot use an unresolved DNS server address: [fe80::1%en0]:53 在家里&#xff0c;连上公司的VPN后&#xff0c;即可打开公司内网&#xff0c;远程办公。一切正常。某…

解决模块Unresolved Link问题

修改内容为下面的&#xff0c;请复制以下内容&#xff08;图中大小写有误&#xff09;&#xff01;&#xff01; Solver_SF/CarSim S-Function 此时就可以修改Simfile name了

c语言1 unresolved externals,出现了 unresolved external symbol _main和1 unresolved externals这是什么问题?...

满意答案 nyjspj 2014.01.18 采纳率&#xff1a;53% 等级&#xff1a;12 已帮助&#xff1a;5918人 {} 数量不匹配 #include #include "process.h" #include "stdio.h" #include "sqstack.h" template void convert(T n,T m) { T e; char c; …

AndroidStudio Unresolved reference

在学习Kotlin过程中&#xff0c;出现了两次在activity_main.xml中已注册id&#xff0c;但是在MainActivity.kt中无法找到该Button的情况。 后面发现是没有在build.gradle中导入 kotin-android-extensions’的包&#xff0c;导致无法 import kotlinx.android.synthetic.main.a…

Unresolved reference ‘SQLAlchemy‘

引用不到包的问题&#xff1b;setting已经添加了&#xff0c;外面是是报错 最后直接用pip下载包 pip install flask_sqlalchemy 就ok了