超详细整合SSM框架--(Spring + Spring MVC + MyBatis)

SpringMVC框架--文章跳转

Spring框架--文章跳转

Mybatis框架--文章跳转

整合思路

设计数据库

搭建环境,选择maven工程,选择骨架webapp

导入依赖

创建目录结构,创建domain,controller,service,dao

domain

Controller层

service层

dao层

index页面

Success页面

编写Spring框架

applicationContext文件

Spring整合SpringMVC框架

编写SpringMVC框架

web.xml

Springmvc.xml

整合SpringMVC框架

在web.xml中配置ContextLoaderListener监听器。加载applicationContext.xml文件

Spring整合Mybatis框架

编写Mybatis框架

在web项目中编写SqlMapConfig.xml的配置文件,编写核心配置文件(AccountDAO接口的方法上添加注解,编写Sql语句)

整合Mybatis框架

把SqlMapConfig.xml配置文件中的内容配置到applicationContext.xml配置文件中,同时配置Spring的声明式事务管理


超详细整合SSM框架--(Spring + Spring MVC + MyBatis)

阅读该文章之前首先要清楚Spring框架,SpringMVC框架,Mybatis框架。

SSM框架,是Spring + Spring MVC + MyBatis的缩写,这个是继SSH之后,目前比较主流的Java EE企业级框架,适用于搭建各种大型的企业级应用系统。

SpringMVC框架--文章跳转

SpringMVC 是一种基于 Java 的实现 MVC 设计模型的请求驱动类型的轻量级 Web 框架,属于 Spring FrameWork 的后续产品,已经融合在 Spring Web Flow 里面。Spring 框架提供了构建 Web

应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用 Spring 进行 WEB 开发时,可以选择使用 Spring 的 Spring MVC 框架或集成其他 MVC 开发框架。

SpringMVC 已经成为目前最主流的 MVC 框架之一,并且随着 Spring3.0 的发布,已成为最优秀的 MVC 框架。

Spring框架--文章跳转

Spring是一个轻量级Java开发框架,最早有Rod Johnson创建,目的是为了解决企业级应用开发的业务逻辑层和其他各层的耦合问题。它是一个分层的JavaSE/JavaEE full-stack(一站式)轻量级开源框架,为开发Java应用程序提供全面的基础架构支持。Spring负责基础架构,因此Java开发者可以专注于应用程序的开发。简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

Mybatis框架--文章跳转

MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

整合思路

1.先搭建整合的环境

2.把Spring的配置搭建完成

3.再使用Spring整合SpringMVC框架

4.最后使用Spring整合Mybatis框架

设计数据库

CREATE DATABASE ssm;
USE ssm;
CREATE TABLE account ( id INT PRIMARY KEY auto_increment, NAME VARCHAR ( 20 ), money DOUBLE );

搭建环境,选择maven工程,选择骨架webapp

导入依赖

 <name>SSM Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target><!--    版本锁定--><spring.version>5.0.2.RELEASE</spring.version><slf4j.version>1.6.6</slf4j.version><log4j.version>1.2.12</log4j.version><mysql.version>5.1.6</mysql.version><mybatis.version>3.4.5</mybatis.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- spring --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.6.8</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.0.2.RELEASE</version></dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.2.RELEASE</version></dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.0.2.RELEASE</version></dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>compile</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency> <dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.0</version><scope>provided</scope></dependency><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!-- log start --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>${log4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${slf4j.version}</version></dependency> <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version></dependency><!-- log end --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.0</version></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version><type>jar</type><scope>compile</scope></dependency></dependencies>

创建目录结构,创建domain,controller,service,dao

web依赖于service,service依赖于dao,dao依赖于domain

domain

package com.dynamic.domain;import java.io.Serializable;/*** @Author: Promsing* @Date: 2021/5/5 - 17:44* @Description: 实体类-Account* @version: 1.0*/
public class Account implements Serializable {private Integer id;private String name;private Double money;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Double getMoney() {return money;}public void setMoney(Double money) {this.money = money;}@Overridepublic String toString() {return "Account{" +"id=" + id +", name='" + name + '\'' +", money=" + money +'}';}
}

Controller层

package com.dynamic.controller;/*** @Author: Promsing* @Date: 2021/5/5 - 17:50* @Description: Web层账户* @version: 1.0*/
@Controller
@RequestMapping("/account")
public class AccountController {@Autowiredprivate AccountService service;//需要加 /@GetMapping("/findAll")public String findAll(Model model){System.out.println("表现层查询所有信息!");//调用Service方法List<Account> all = service.findAll();for (Account account : all) {System.out.println(account);}model.addAttribute("all",all);return "success";}@PostMapping("/save")public String save(Account account){service.saveAccount(account);return "success";}}

service层

public interface AccountService {/*** 查询所有* @return*/public List<Account> findAll();/*** 保存账户信息* @param account*/public void saveAccount(Account account);}@Service("accountService")
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao dao;@Overridepublic List<Account> findAll() {System.out.println("业务层:查询所有信息!");return  dao.findAll();}@Overridepublic void saveAccount(Account account) {System.out.println("业务层:保存账户。。。");dao.saveAccount(account);}
}

dao层

/*** @Author: Promsing* @Date: 2021/5/5 - 17:46* @Description: DAO层  使用注解* @version: 1.0*/
@Repository
public interface AccountDao {/*** 查询所有* @return*/@Select("select * from account")public List<Account> findAll();/*** 保存账户信息* @param account*/@Insert("insert into account (name,money) values(#{name},#{money})")public void saveAccount(Account account);
}

index页面

<%--Created by IntelliJ IDEA.User: AdministratorDate: 2021/5/5Time: 19:44To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head><title>首页</title>
</head>
<body><a href="account/findAll">测试查询</a><form action="account/save" method="post">姓名:<input type="text" name="name"><br/>金额:<input type="text" name="money"><br/><input type="submit" value="保存"><br/></form></body>
</html>

Success页面

<%--Created by IntelliJ IDEA.User: AdministratorDate: 2021/5/5Time: 19:49To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><html>
<head><title>成功页面</title>
</head>
<body><h1>成功页面</h1>${all}<br/><c:forEach items="${all}" var="account">${account.name}${account.money}</c:forEach>
</body>
</html>

编写Spring框架

applicationContext文件

<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"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!--    1.导入约束2.开启注解扫描支持--><!--    开启注解的扫描--><context:component-scan base-package="com.dynamic">
<!--        配置哪些注解不扫描,进行忽略--><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan></beans>

Spring整合SpringMVC框架

编写SpringMVC框架

web.xml

<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app><display-name>Archetype Created Web Application</display-name><!-- 配置前端控制器--><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--    加载springmvc.xml配置文件--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><!--  启动服务器,创建该Servlet--><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!--    解决中文乱码的过滤器--><filter><filter-name>characterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>characterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
</web-app>

Springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation=" http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--   开启注解扫描  只扫描Controller注解--><context:component-scan base-package="com.dynamic"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!--   配置视图解析器对象     --><bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/pages/"></property><property name="suffix" value=".jsp"></property></bean><!--    过滤静态资源--><!-- 设置静态资源不过滤 --><mvc:resources location="/css/" mapping="/css/**" /><mvc:resources location="/images/" mapping="/images/**" /><mvc:resources location="/js/" mapping="/js/**" /><!--    开启springMVC注解的支持--><mvc:annotation-driven></mvc:annotation-driven>
</beans>

整合SpringMVC框架

在Controller中能够成功调用service对象中的方法

在web.xml中配置ContextLoaderListener监听器。加载applicationContext.xml文件

在项目启动的时候,就去加载applicationContext.xml的配置文件,在web.xml中配置ContextLoaderListener监听器。该监听器只能加载WEB-INF目录下的applicationContext.xml的配置文件

<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app><display-name>Archetype Created Web Application</display-name><!--    配置Spring的监听器,默认只加载 WEB-INF目录下的applicationContext.xml--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--    设置配置文件的路径--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- 配置前端控制器--><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--    加载springmvc.xml配置文件--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><!--  启动服务器,创建该Servlet--><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!--    解决中文乱码的过滤器--><filter><filter-name>characterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>characterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
</web-app>

Spring整合Mybatis框架

编写Mybatis框架

在web项目中编写SqlMapConfig.xml的配置文件,编写核心配置文件(AccountDAO接口的方法上添加注解,编写Sql语句)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!--    配置环境--><environments default="mysql"><environment id="mysql"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql:///ssm"/><property name="username" value="root"/><property name="password" value="root"/></dataSource></environment></environments><!--    引入映射配置文件 resource--><!-- 使用的是注解 class --><mappers><!-- 该包下所有的dao接口都可以使用 --><package name="com.dynamic.dao"/></mappers>
</configuration>

整合Mybatis框架

把SqlMapConfig.xml配置文件中的内容配置到applicationContext.xml配置文件中,同时配置Spring的声明式事务管理

<?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"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!--    1.导入约束2.开启注解扫描支持--><!--    开启注解的扫描--><context:component-scan base-package="com.dynamic"><!--        配置哪些注解不扫描,进行忽略--><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!--    spring整合Mybatis框架--><!--    配置数据库连接池--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql:///ssm"/><property name="user" value="root"/><property name="password" value="root"/></bean><!--    配置sqlSessionFactory工厂--><bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property></bean><!--    配置AccountDAO接口所在的包--><bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.dynamic.dao"></property></bean><!--    配置Spring框架声明式事务管理-->
<!--    配置事务管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!--配置事务通知--><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="find*" read-only="true"></tx:method><tx:method name="*" isolation="DEFAULT"></tx:method></tx:attributes></tx:advice>
<!--    配置AOP增强--><aop:config><aop:advisor advice-ref="txAdvice" pointcut="execution(public * com.dynamic.service..ServiceImpl.*(..))"/></aop:config>
</beans>

如果本篇博客对您有一定的帮助,大家记得留言+点赞+收藏哦。原创不易,转载请联系作者!

超详细整合SSM框架--(Spring + Spring MVC + MyBatis)相关推荐

  1. 一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程重要

    前言 SSM(Spring+SpringMVC+Mybatis)是目前较为主流的企业级架构方案,不知道大家有没有留意,在我们看招聘信息的时候,经常会看到这一点,需要具备SSH框架的技能:而且在大部分教 ...

  2. 【超详细】SSM框架项目实战|Spring+Mybatis+Springmvc框架项目实战整合-【CRM客户管理系统】——课程笔记

    相关资料网盘链接: CRM客户管理系统资料 提取码 :0u04 P1--CRM阶段简介: web项目开发:如何分析,设计,编码,测试.        形成编程思想和编程习惯. P2--CRM的技术架构 ...

  3. ❤️《集成SSM框架—图书系统》Mybatis+Spring+SpirngMVC

    <集成SSM框架-图书系统>Mybatis+Spring+SpirngMVC 希望各位博友给个三连+关注,谢谢!!! 全部源码地址:链接:https://pan.baidu.com/s/1 ...

  4. SSM框架:Spring

    SSM框架:Spring 文章目录 前言 一.Spring 1. 简介 2. 优点 3. 组成 4. 拓展 二.IOC理论推导 1. IOC原型引入 2. IOC本质(基本思想) 三.HelloSpr ...

  5. JavaWeb学习之路——SSM框架之Spring(五)

    前情提要请看JavaWeb学习之路--SSM框架之Spring(四)                                         整合Spring和Mybatis框架 1.在项目的 ...

  6. idea 使用 maven 整合 ssm 框架 实现简单的增、删、改 和 分页查询功能

    详细请参考:   idea 使用 maven 整合 ssm 框架 文章目录 ==效果图== ==准备数据库== ==创建maven项目== ==配置文件== pom.xml jdbc.properti ...

  7. 2023新版图文详解SpringBoot整合SSM框架(附源码)

    版权声明 本文原创作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl 教程概述 本教程以图文形式详细讲解SpringBoot整合SSM框架的流程以及具体步骤及其注意 ...

  8. springboot2整合mysql5_SpringBoot2整合SSM框架详解

    SpringBoot2整合SSM框架详解 发布时间:2019-01-15 21:33, 浏览次数:1218 , 标签: SpringBoot SSM <>开发环境 * 开发工具:Eclip ...

  9. idea 使用maven 整合ssm框架

    创建 maven 项目 刚创建好的 maven 项目结构 整合 SSM 框架后的项目结构 数据库环境 创建 mybatis 数据库,在 mybatis 数据库中创建 teacher 数据库表,然后在 ...

最新文章

  1. Xamarin图表开发基础教程(11)OxyPlot框架支持的图表类型
  2. netty接收大文件的方法
  3. 【mysql执行计划 const eq_ref ref range index all】
  4. windows10计算机用户密码,忘记Windows 10系统密码?教你重置
  5. HTML中常见元素及格式
  6. JVM,卷走面试官(二)—— 有党性的前端编译
  7. Java实现简单计算器
  8. python绘制箭头_python如何绘制坐标箭头?
  9. 视频教程-Photoshop(PS)软件基础入门-Photoshop
  10. python可以这样学读后感_《Python深度学习》读后感
  11. XML HttpRequest
  12. 网络信息安全攻防学习平台——基础关
  13. 联想IdeaPad710s进入bios界面的详细步骤
  14. 带你开发个转盘抽奖小游戏【附源码】
  15. 如何用计算机名查看共享打印机,共享打印机怎么连接?打印机如何共享?查看图文帮你解决...
  16. win10计算机自带的游戏怎么打开方式,win10自带游戏及应用打不开,应用商店闪退无法使用...
  17. NYOJ-975 关于521
  18. 用个人电脑搭建服务器
  19. UltraEdit 快捷键(UE 快捷键)
  20. (一)什么是Mybatis?Mybatis的优点是什么?

热门文章

  1. 再次认识 errno之线程安全
  2. 女生适合linux运维吗,女生适不适合做Linux运维开发工程师?
  3. airflow和MySQL_Airflow知识总结
  4. linux如何使history命令可以显示更多信息
  5. Python编程从入门到实践,第7章练习:7-1~7-10
  6. Mac搭建安卓模拟器(支持M1/M2)
  7. android数控加工软件,基于Android平台数控车床应用软件的设计与实现
  8. 计算机表格定义,怎么进行报表定义
  9. QT自定义拖放_图片的移动
  10. 离线使用ElementUI