SSM(Spring+SpringMVC+Mybatis)整合

(Spring+SpringMVC+MyBatis)SSM三大框架整合教程 目录 一、基本概念 1、Spring 2、SpringMVC 3、MyBatis 二、开发环境搭建 三、Maven Web项目创建 四、SSM整合 1、Maven引入需要的JAR包 2、Spring与MyBatis的整合 3、整合SpringMVC 使用SSM(Spring、SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地... 阅读详情

1、导包

项目结构如下

 

2、配置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">

 

  <!-- 配置Spring IOC容器的Listener -->

    <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>

      

    <!-- 配置DispatcherServlet -->

    <servlet>

       <servlet-name>springDispatcherServlet</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

       <!-- 实际上也可以不使用contextConfigLocation来配置springMVC的配置文件

           而使用默认的,默认的配置文件为:/WEB-INF/<servlet-name>-servlet.xml

        -->

       <init-param>

           <param-name>contextConfigLocation</param-name>

           <param-value>classpath:springmvc.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>

 

3、配置springmvc.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"

    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="

        http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-4.3.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

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx.xsd

        http://www.springframework.org/schema/mvc

        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

       

        <!-- 配置自动扫描的包 -->

        <context:component-scan base-package="controller" use-default-filters="false">

           <!-- 只扫描标有Controller注解的类 -->

           <context:include-filter type="annotation"

           expression="org.springframework.stereotype.Controller"/>

        </context:component-scan>

       

        <!-- 配置视图解析器 -->

        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

           <property name="prefix" value="/WEB-INF/views/"></property>

           <property name="suffix" value=".jsp"></property>

        </bean>

       

        <!-- 处理静态资源 -->

        <mvc:default-servlet-handler/>

       

        <mvc:annotation-driven></mvc:annotation-driven>

       

</beans>

 

4、配置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"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:util="http://www.springframework.org/schema/util"

    xmlns:p="http://www.springframework.org/schema/p"

    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"

    xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd

       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.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

       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

 

    <!-- 配置自动扫描的包 -->

    <context:component-scan base-package="service">

       <!-- 扫描除了标有Controller注解的类 -->

       <context:exclude-filter type="annotation"

           expression="org.springframework.stereotype.Controller"/>

    </context:component-scan>

   

    <!-- 导入配置文件 -->

    <context:property-placeholder location="classpath:db.properties"/>

   

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

       <property name="user" value="${jdbc.user}"></property>

       <property name="password" value=""></property>

       <property name="driverClass" value="${jdbc.driverClass}"></property>

       <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC&amp;characterEncoding=utf8"></property>

 

       <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>

       <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>

    </bean>

   

    <!-- 声明事务管理器 -->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

       <property name="dataSource" ref="dataSource"></property>

    </bean>

   

    <!-- 开启注解事务 -->

    <tx:annotation-driven transaction-manager="transactionManager"/>

   

    <!-- 创建SqlsessionFactory对象 -->

    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">

       <property name="dataSource" ref="dataSource"></property>

       <!-- 指定全局配置文件的位置 -->

       <property name="configLocation" value="classpath:mybatis-config.xml"></property>

       <!-- 指定mapper文件的位置 -->

       <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>

    </bean>

   

    <!--

       扫描所有的mapper接口的实现,让这些mapper能够自动注入

       base-package:指定mapper接口的包名

     -->

    <mybatis-spring:scan base-package="dao"/>

   

</beans>

 

db.properties

 

jdbc.user=root

jdbc.password=

jdbc.driverClass=com.mysql.jdbc.Driver

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test

 

jdbc.initPoolSize=5

jdbc.maxPoolSize=10

 

5、测试(从数据库中查询所有员工)

 

Employee.java实体类

public class Employee implements Serializable {

    private Integer id;

    private String name;

    private String sex;

    private String email;

 

    public Employee() {

       super();

    }

 

    public Employee(Integer id, String name, String sex, String email) {

       super();

       this.id = id;

       this.name = name;

       this.sex = sex;

       this.email = email;

    }

 

    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 String getSex() {

       return sex;

    }

 

    public void setSex(String sex) {

       this.sex = sex;

    }

 

    public String getEmail() {

       return email;

    }

 

    public void setEmail(String email) {

       this.email = email;

    }

 

    @Override

    public String toString() {

       return "Employee [id=" + id + ", name=" + name + ", sex=" + sex + ", email=" + email + "]";

    }

}

 

EmployeeMapper.java接口

public interface EmployeeMapper {

   

    //查询所有员工的接口方法

    public List<Employee> getEmps();

   

}

 

EmployeeService.java

@Service

public class EmployeeService {

   

    @Autowired

    private EmployeeMapper employeeMapper;

   

    public List<Employee> getEmps(){

       return employeeMapper.getEmps();

    }

}

 

EmployeeController.java

@Controller

public class EmployeeController {

   

    @Autowired

    private EmployeeService employeeService;

   

    @RequestMapping("/getEmps")

    public String getEmps(Map<String, Object> map){

       List<Employee> emps = employeeService.getEmps();

       map.put("allEmps", emps);

       return "list";

    }

   

}

EmployeeMapper.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">

<mapper namespace="dao.EmployeeMapper">

<!-- namespace指定所对应的接口的全路径 -->

 

    <!-- public List<Employee> getEmps() -->

    <select id="getEmps" resultType="entity.Employee">

       select * from employee;

    </select>

   

</mapper>

 

index.jsp

<a href="getEmps">查询所有员工</a>

 

list.jsp

<h3>员工列表</h3>

    <table>

           <tr>

              <td>员工号</td>

              <td>姓名</td>

              <td>性别</td>

              <td>email</td>

           </tr>

       <c:forEach items="${allEmps }" var="emp">

           <tr>

              <td>${emp.id }</td>

              <td>${emp.name }</td>

              <td>${emp.sex }</td>

              <td>${emp.email }</td>

           </tr>

       </c:forEach>

</table>

 

测试结果

 

推荐大家去百度传课学习尚硅谷的免费网课,讲的很好

 

用朴素贝叶斯分类模型判断病人是否感染幽门螺杆菌(Python实现) 朴素贝叶斯,Python 阅读详情

相关推荐

懒人精灵手游内存技术分析

教学声明:全套教程教学用到的游戏采用手游私服网游进行测试,不针对任何游戏。教学只做计算机技术研究,不做其他用途。

yizhihaoren123的博客 1667

sprint boot 整合ssm 自动注入mapper报错

在使用springboot整合ssm时,service类自动注入mapper报错。 Could not autowire. No beans of ‘UserMapper’ type found. less… (Ctrl+F1) Inspection info:Checks autowiring problems in a bean class. 解决办法1,在启动器上加入mapper注解扫描 @...

xuwenjingrenca的博客 561

如何在离线的电脑上部署一个本地Deepseek(使用ollama)

例如,如果您的显卡显存为 16GB,官方推荐使用 32b 大小的 DeepSeek 模型。请根据您的硬件配置选择适合的模型版本,以获得最佳性能。首先我们需要将ollama和chatbox的安装包,Deepseek模型文件通过U盘或者光盘的形式导入到这台不能联网的电脑上。按照时间排序,找到最大的那个文件,就是gguf格式的模型,复制出来,改名为qwen2.5-coder0.5b.gguf。:基于开源模型(如 QWEN 和 LLAMA)微调,参数量从 1.5B 到 70B 不等,适合本地硬件部署。

qq_41854911的博客 1万+

java_spring:day04

注解@service、 @Controller、@repository使用 Controller层 @Controller public class StudentController { @Autowired private StudentService studentService; public void add(Student stu){ studentService.addStudent(stu); System.out.println(

m0_46540089的博客 168

解决国内网页无法加载reCaptcha的方法

科普: reCaptcha是Google公司的验证码服务,方便快捷,改变了传统验证码需要输入n位失真字符的特点。reCaptcha在使用的时候是这样的: 只需要点一下复选框,Google会收集一些鼠标轨迹、网络信息、浏览器信息等等,依靠后端的神经网络判断是机器还是人,绝大多数验证会一键通过,无需像传统验证码一样。但是reCaptcha使用了google.com的域名,这个域名在国内是被墙的。所以,也就导致了官网上无法注册/登录的问题,挡住了不少人入正的脚步。 解决办法: 【第一步】安装fir

调皮李小怪博客 25万+

手把手教你用SAM模型实现精准图像分割:从环境配置到实战应用

本文详细介绍了如何使用Meta推出的Segment Anything Model(SAM)实现精准图像分割,从环境配置到实战应用的全流程。特别针对电商场景中的商品分割需求,提供了硬件配置、模型下载、API使用及优化策略等实用指南,帮助开发者快速掌握这一强大的深度学习工具。

pepsi的博客 1090

【SSM 框架】SpringMVC+Spring+Mybatis SSM-Mybatis整合Spring(mapper接口扫描 )

4. MyBatis 整合 Spring - Mapper 接口扫描 (推荐) 4.1.修改 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://

mydreamljy的博客 399

LM339 vs LM358:手把手教你选对比较器和运放(附电路设计避坑指南)

本文深入比较了LM339比较器与LM358运算放大器的核心差异,包括内部架构、输出结构和典型应用场景。通过实测数据和电路设计案例,揭示了选型要点和常见设计陷阱,帮助工程师在电源监控、传感器信号调理等场景中做出正确选择。

convnet3designer的博客 368

【SSM整合】SpringMVC + Spring + Mybatis整合详细教程

本文为SpringMVC + Spring + Mybatis整合教程相关详细介绍,从数据库数据准备、相关依赖包添加、创建项目基本结构和配置框架,到Mybatis层的编写、Spring层的编写、SpringMVC层的编写,再到前端页面编写等,最后进行运行与测试,按步骤对SSM框架整合进行详细的解析~

小新要变强的博客 5434

激光雷达点云可视化:Open3D与PCL实战

激光雷达点云可视化:Open3D与PCL实战 ,人工智能,计算机视觉,大模型,AI,该文围绕激光雷达点云可视化,介绍了Open3D与PCL的实战应用。先阐述点云基础概念、特点及可视化意义,接着讲解Open3D的简介、环境配置,以及其在点云读取、显示、颜色设置、下采样和滤波等方面的实战操作,还介绍了PCL的基础、环境配置及相关实战,包括点云读取、显示、颜色设置、下采样和滤波,同时对比了两者的易用性、功能覆盖、性能和适用场景,给出综合实战案例与常见问题解决方法,为点云可视化处理提供了全面指导。

不踩坑,不知道坑有多深;不总结,踩过的坑白踩。 12万+

【06】新国都——新国都 嵌入式笔试,校招,题目记录及解析

本文总结了新国都嵌入式校招笔试题目及解析,内容涵盖C语言基础、Linux系统、数据结构与算法等多个方面。主要包括:1)不定项选择题10道,涉及字符串常量、蓝牙与GPRS区别、编译错误识别等;2)判断题10道,考察Linux权限、循环控制、内存管理等;3)填空题5道,包括编码计算、逻辑运算、宏定义等;4)简答题2道,解析全局变量引用和进程线程区别。题目难度适中,重点考察嵌入式开发基础知识和编程能力,并提供详细解析和扩展知识链接,对准备嵌入式相关笔试具有参考价值。

程序员,他们想的是什么?他们想的永远都是技术,他们崇尚的也永远都是技术。 1443

SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)

使用SSM(Spring、SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方。之前没有记录SSM整合的过程,这次刚刚好基于自己的一个小项目重新搭建了一次,而且比项目搭建的要更好一些。以前解决问题的过程和方法并没有及时记录,以后在自己的小项目中遇到我再整理分享一下。这次,先说说三大框架整合过程。个人认

在路上 64万+

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Spring+SpringMVC+Mybatis整合Mybatis plus(SSM整合MP)

SSM框架开发,Dao层还是需要写很多SQL,但是配合使用Mybatis plus,会减少很多SQL的书写,加快项目开发速度,稳定性和性能都有提升。以下是SSM整合MP的步骤: 1.依赖不同 ...

DmxExcalibur的博客 7849

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)

SSM框架——详细整合教程基本概念SpringSpringMVCMyBatis环境搭建SSM整合项目创建引入项目依赖的jar包配置SpringMVC配置日志使用Tomcat测试Spring与MyBatis的整合建立数据库配置文件db.properties 基本概念 Spring Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。 SpringMVC Spring MVC ...

JustinNeil的博客 1393

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

ssm整合,(spring,springMVC,mybatis)

ssm整合Java,xml版本

qq_46142046的博客 696

SSM之SSM整合Mybatis、Spring、SpringMVC

前面我们已经把SSM的三个部分讲完了,现在我们来整合这三个部分 文章目录SSM 整合搭建整合环境Spring框架代码的编写Spring整合SpringMVC框架搭建和测试SpringMVC的开发环境Spring整合SpringMVC的框架Spring整合MyBatis框架搭建和测试MyBatis的环境Spring整合MyBatis框架 SSM 整合 整合说明:SSM整合可以使用多种方式,咱们会选择XML + 注解的方式 整合的思路: 先搭建整合的环境 先把Spring的配置搭建完成 再使用Sprin.

chongzi的博客 362

【SSM整合】Spring、SpringMVC、MyBatis整合

一文解决Spring+SpringMVC+MyBatis整合配置

HXY19的博客 351

SSM整合(SpringMVC + Spring + Mybatis)

SSM整合开发 # SSM整合开发步骤 SSM = SpringMVC + Spring + Mybatis 1. 引入依赖 spring springmvc mybatis mybatis-spring mysql druid log4j servlet-api jstl fastjson(需要做ajax处理引入) 2. SM整合 = Spring + Mybatis 1). 建表 2). 实体 3). DAO接口 4). Mapper配置文件 5). Service接口 6

qq_50313418的博客 2135

SSM整合_MQ(eclipse版本) 超详细Spring+SpringMVC+Mybatis整合

SSM整合_MQ(eclipse版本) 超详细Spring+SpringMVC+Mybatis整合 0创建项目 1)、创建Maven的简单web工程 2)、补全WEB-INF目录和web.xml文件: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSc...

weixin_42588676的博客 2095

SSM框架整合—详细整合教程(Spring+SpringMVC+MyBatis)

SSM框架整合—详细整合教程(Spring+SpringMVC+MyBatis)

mh17680313750的博客 1029

SSM框架整合Spring+SpringMVC+MyBatis

【SSM的系统架构】 【整合概述】 第一步: MyBatis和Spring整合,通过Spring管理mapper接口。 使用mapper的扫描器自动扫描mapper接口在Spring中进行注册。 第二步: 通过Spring管理Service接口。 使用配置方式将Service接口配置在Spring配置文件中。 实现事务控制。 第三步: 由于SpringMVC是Spring的...

我的CSDN博客之家 939

Spring之SSM整合流程详解(Spring+SpringMVC+MyBatis)

SSM(Spring + SpringMVC + MyBatis)是企业级Java开发的经典框架组合:Spring负责IoC容器和事务管理,SpringMVC处理Web请求,MyBatis简化数据库操作,掌握SSM整合是Java后端开发的必备技能。

aa_hdkf_vg的博客 1581

SSM整合-Spring+SpringMVC+Mybatis

title: SSM整合-SpringMVC+Spring+Mybatis tags: JAVA SSM categories: 程序人生 编程语言 JAVA keywords: ‘时光小站,假冒伪劣小小Ho,TimeBlog,记录流逝的时光’ cover: ‘http://www.dmoe.cc/random.php?time=2020-02-10 12:41:39’ typora-root...

Time_Ho的学习日记 330

SSM整合(Spring、SpringMVC、MyBatis、Thymeleaf)

SSM-CRUD整合 1、整合整体概况 2、spring所需依赖: ioc依赖的jar包: aop依赖的jar包: 在maven工程中只要添加context依赖,因为存在传递依赖,因此其他依赖也随之被导入。也可一个一个地添加依赖 3、在pom.xml中添加依赖所需要的坐标 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:x

baidu_39056574的博客 983
上一篇: Spring 配置事务的两种方式(注解和XML)
下一篇: javax.imageio.IIOException: Can't read input file!
JokerZhan
博客等级 码龄9年 1粉丝 19原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值