Spring+SpringMVC+MyBatis整合

5.Spring+Mybatis+SpringMVC整合 前端时间搭建了一个关于新闻的后台,使用Restful风格,向前台提供json格式数据。 对于刚学习的时候会觉得整合三个框架是一件很难的事情,但是当自己都熟悉了之后再来整理下思路其实也没有那么难。 就是把三个模块整合运行起来,然后写上自己的业务逻辑就形成了一个项目。 今天把就思路整一下,加深自己的印象,首先谈一下这三个东西分别是什么。 spring  Spring是一个开源框架,Sprin 阅读详情

一、思路

SSH --> Spring+SpringMVC+MyBatis

SM整合 --> SS整合 -->SSM

二、大体框架

三、Spring整合MyBatis

SqlSessionFactory ->SqlSession -> StudentMapper ->CRUD

MyBatis最终通过SqlSessionFactory 来操作数据库,Spring整合MyBatis,其实就是将MyBatis的SqlSessionFactory交给Spring。

1、jar包

2、配置

与spring整合时,mybatis的配置文件conf.xml(数据源+mapper.xml)可省,将其配置在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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
		<property name="locations">
			<array>
				<value>classpath:db.properties</value>
			</array>
		</property>
	</bean>

	<!-- 配置数据库信息(替代mybatis的配置文件confx.ml)   -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${driver}"></property>
		<property name="url" value="${url}"></property>
		<property name="username" value="${username}"></property>
		<property name="password" value="${password}"></property>
	</bean> 
	
	<!-- 在springIOC中创建mybatis的核心类SqlSessionFactoryBean  -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="mapperLocations" value="classpath:com/guor/mapper/*.xml"></property>
	</bean> 

	<bean id="studentService" class="com.guor.service.impl.StudentServiceImpl">
		<property name="studentMapper" ref="studentMapper"></property>
	</bean>
	
	<!-- 批量产生mapper对象在IOC中的id值默认就是接口名-->
	<bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
		<!-- 指定批量产生哪个包的mapper对象 -->
		<property name="basePackage" value="com.guor.mapper"></property>  
	</bean>
</beans>
driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
username=orcl
password=orcl

3、编写代码

(1)entity

package com.guor.entity;

public class Student {
	private int id;
	private String name;
	private int age;

	...
}

(2)mapper

package com.guor.mapper;

import com.guor.entity.Student;

public interface StudentMapper {
	public void addStudent(Student student);

	public Student queryStudentByStuNo(int id);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.guor.mapper.StudentMapper">
	<select id="queryStudentByStuNo" parameterType="int" resultType="com.guor.entity.Student">
		select * from student where id = #{stuNo}
	</select>
	
	<insert id="addStudent" parameterType="com.guor.entity.Student">
		insert into student(id,name,age) values (#{id},#{name},#{age})
	</insert>
</mapper>

(3)service(面向接口编程)

package com.guor.service;

import com.guor.entity.Student;

public interface IStudentService {
	public void addStudent(Student student);
	public Student queryStudentByStuNo(int id);
}
package com.guor.service.impl;

import com.guor.entity.Student;
import com.guor.mapper.StudentMapper;
import com.guor.service.IStudentService;

public class StudentServiceImpl implements IStudentService {
	private StudentMapper studentMapper;
	
	public void setStudentMapper(StudentMapper studentMapper) {
		this.studentMapper = studentMapper;
	}

	@Override
	public void addStudent(Student student) {
		studentMapper.addStudent(student);
	}

	@Override
	public Student queryStudentByStuNo(int id) {
		return studentMapper.queryStudentByStuNo(id);
	}
}

(4)controller

package com.guor.controller;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.guor.entity.Student;
import com.guor.service.IStudentService;

@Controller
@RequestMapping("studentController")
public class StudentController {
	@Autowired
	@Qualifier("studentService")
	private IStudentService studentService;

	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	@RequestMapping("queryStudentByStuNo/{id}")//映射
	public String queryStudentByStuNo(@PathVariable("id") Integer id,Map<String,Object> map) {
		Student student = studentService.queryStudentByStuNo(id);
		map.put("student", student);
		return "result";
	}
}

四、Spring整合SpringMVC

1、jar包

一个就行,spring-webmvc-4.3.9.RELEASE.jar

2、配置

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">
	
	<!-- 扫描有注解的包 -->
	<context:component-scan base-package="com.guor.controller"></context:component-scan>
	
	<!-- 配置视图解析器(InternalResourceViewResolver) -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- springMVC基础配置、标配 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter" />
			</list>
		</property>
	</bean>
	<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
			</list>
		</property>
	</bean>
	
	<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<!-- <property name="exceptionAttribute" value = "ex"></property> 异常变量的默认值是exception-->
		<property name="exceptionMappings">
			<props>
				<!-- 相当于catch -->
				<prop key="java.lang.ArithmeticException">
					error
				</prop>
				<prop key="java.lang.NullPointException">
					error
				</prop>
			</props>
		</property>
	</bean>
</beans>

3、index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="studentController/queryStudentByStuNo/3">queryStudentByStuNo</a>
</body>
</html>

4、运行

SpringMVC Spring Mybatis整合步骤 一.配置mybatis   1.在pom.xml中导入mybatis3.4.5的依赖,导入mysql-connector-java-5.1.43      2.写mybatis-config-xml的配置文件,里面包含连接数据库的信息和映射文件   3.实体类和对应的映射文件(person,person.xml)   4.dao接口   5.写一个测试类测试mybatis是否可以使用 阅读详情

相关推荐

Spring+SpringMVC+Mybatis SSM框架详解

JDBC其实就是 Java 官方提供的一套规范(接口),用于帮助开发人员快速实现不同关系型数据库的连接。程序运行的时候,数据都是在内存中的。当程序终止的时候,通常都需要将数据保存到磁盘上,无论是保存到本地磁盘,还是通过网络保存到服务器上,最终都会将数据写入磁盘文件。而如何定义数据的存储格式就是一个大问题。

悦分享 3498

SpringMVC框架+spring框架+Mybatis框架(SSM框架整合)

1.1原始方法整合 1.准备工作 2.创建Maven项目

weixin_61491281的博客 2382

SpringMVC、Mybatis与Spring整合实战演示

本文还有配套的精品资源,点击获取 简介:SpringMVC、Mybatis和Spring是Java开发中常用的三大框架,它们分别负责不同层次的应用开发。Spring作为基础容器负责对象管理和依赖注入;SpringMVC处理Web层的请求分发;Mybatis专注于数据访问层,简化了SQL操作。整合这三大框架能够构建出高效的模块化企业级应用。本文通过SSMDemo项目实例,详细...

weixin_29097457的博客 1095

Spring+SpringMVC+MyBatis

Web全栈实训三大框架笔记

m0_58642116的博客 1572

Spring、MyBatis与SpringMVC整合的实践教程

在现代Web应用开发中,为了提高开发效率和降低维护成本,越来越多的开发者选择整合Spring、MyBatis和SpringMVC这三个强大的框架。Spring提供了全面的企业级服务支持,MyBatis作为数据持久层框架,实现了对象关系映射(ORM),而SpringMVC则专注于Web层的MVC模型处理。整合这三者能够构建出一个结构清晰、功能完善的Java应用,易于扩展和维护。Spring框架是一个全面的企业级应用开发框架,它为Java开发人员提供了一整套解决方案。

weixin_34618526的博客 1058

企业级Web开发框架整合:SpringMVC、Spring与MyBatis实战

本文还有配套的精品资源,点击获取 简介:本文深入探讨了现代企业级Web应用开发中的三大框架——SpringMVC、Spring和MyBatis——的整合使用。SpringMVC处理HTTP请求,Spring管理依赖注入与面向切面编程,而MyBatis则简化数据库操作。整合过程涉及依赖引入、配置Spring容器、设置SpringMVC组件、配置MyBatis以及将MyBati...

weixin_30978239的博客 1820

Spring + MyBatis + SpringMVC 全面详解(学习笔记)

Spring + MyBatis + SpringMVC 全面详解(学习笔记)

as1517的博客 4420

STM32标准库下载保姆级教程:2024最新官网免注册直接下载方法

本文提供2024年最新STM32标准库免注册下载方法,包括直接下载链接、GitHub镜像仓库和开发工具内置库三种途径。详细介绍了标准外设库的优势、配置步骤及常见问题解决方案,帮助开发者快速获取并应用这一重要开发资源。

weixin_29163581的博客 369

Spring+SpringMVC+MyBatis的详细整合

1、基本概念   1.1、Spring          Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成...

weixin_42164207的博客 629

USB-C快充协议分析仪:QC/PD双协议识别与功率监测设计

USB-C快充协议是现代移动设备能量传输的核心技术标准,其本质是通过物理层电压特征(如DP/DM组合、CC线BMC编码)实现动态电压协商。理解协议握手原理对充电器研发、兼容性测试及故障诊断至关重要。本方案聚焦受电端(Sink)视角,基于FUSB302B与高阻抗DP/DM采样电路,实现QC2.0/QC3.0/QC4+与PD2.0/PD3.0的实时识别与状态机解析;结合INA219高精度电流检测与μs级同步采样,达成±0.06%功率测量精度。典型应用于快充芯片验证、产线协议一致性测试及第三方评测,为USB供电系

weixin_42153793的博客 248

6种常用数据分析方法详解:从描述统计到时间序列

数据分析是连接数据与业务决策的桥梁。面对海量数据,掌握描述性统计与对比分析可快速了解现状与差距;相关分析与回归分析能揭示变量间的关联与影响大小;聚类分析用于用户分群,时间序列分析则预测未来走势。这些方法广泛应用于用户画像、销量预测、活动效果评估等场景,是数据分析师和运营人员必备的技能。系统掌握这6种核心方法的使用场景、原理逻辑与实操步骤,能够帮助读者建立完整分析框架,真正用数据驱动业务增长。

weixin_34405354的博客 339

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

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

小新要变强的博客 5432

C++新手也能搞定!Windows下Cesium-Native编译全流程(附依赖下载技巧)

本文为C++新手详细介绍了在Windows系统下编译Cesium-Native库的全流程。重点针对网络环境不佳的情况,提供了手动下载依赖库的实用技巧,并指导用户使用CMake配置、生成解决方案,最终完成编译与测试验证,帮助开发者成功构建这一处理3D地理空间数据的C++运行时库。

weixin_28839549的博客 386

Spring、SpringMVC、Mybatis 整合

Spring、SpringMVC、Mybatis 整合 文章目录Spring、SpringMVC、Mybatis 整合1、Pom.xml配置1.1 Maven依赖的导入1.2 静态资源导出配置2、Spring和Mybatis整合2.1 创建applicationContext.xml2.2 创建mybatis-config.xml2.3 创建database.properties2.4 创建spring-dao.xml3 SpringMVC层3.1 配置web.xml3.2 创建spring-mvc.xml

xiaoxiao66668的博客 1467

不用公网IP,如何在内网高效搭建RustDesk远程控制服务器?基于Windows Server 2019的完整实践

本文详细介绍了在Windows Server 2019上部署RustDesk远程控制服务器的完整实践方案,无需公网IP即可实现内网高效远程控制。从环境准备、基础服务安装到网络与安全配置,提供了零成本的私有化部署指南,适用于企业、校园等内网环境。

weixin_28737073的博客 456

嵌入式面经(2)——央企篇

前文得知我只投央企,不投国企,有面试的央企大致有三类:感兴趣的同学可以和我私聊军工央企相关(航空航天,兵器兵工)制造央企相关(三桶油,装备制造)金融运维相关(信通集团,国有银行)接下来我将对我有的面试中询问的面经进行总结。

coldmood77的博客 473

Spring Springmvc Mybatis整合流程

Spring Springmvc Mybatis整合流程 导入jar包 1.导入相应的jar包 <!--spring相关的jar包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version...

qq_45857788的博客 320

单片机通过蜂鸣器播放任意音乐代码实现(2):音乐单片机代码自动生成

单片机通过蜂鸣器播放任意音乐代码实现(2):音乐单片机代码自动生成 上一节我们已经构建了基于51单片机的蜂鸣器音乐播放环境,接下来只需要手动或者自动添加音乐代码便能实现不同音乐的播放。当然,第二节将告诉你如何自动生成这些音乐代码。 所用软件 MuseScore 3;用于转换MID文件为musicxml文件 C/C++ IDE(本文为Visual Studio 2019) 1.生成音乐对应的MID文件 可以直接下载mid格式音乐文件,也可以将某段音乐转换为MID文件。最好是直接使用mid格式音乐文件,可

kevin1499的博客 2万+

SpringMVC快速入门第四讲——Spring、MyBatis和SpringMVC的整合

之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBatis框架的学习(六)——MyBatis整合Spring。本文我再来讲SpringMVC和MyBatis整合开发的方法,这样的话,Spring、MyBatis和SpringMVC三大框架的整合开发我们就学会了。这里我使用的Spring是Spring4.1.3这个版本(SpringMVC自然也是这个版本),MyBatis是

李阿昀的博客 9334

YOLOv12改进策略【注意力机制篇】| 添加SE、CBAM、ECA、CA、Swin Transformer等注意力和多头注意力机制

这篇文章带来一个经典注意力模块的汇总,虽然有些模块已经发布很久了,但后续的注意力模块也都是在此基础之上进行改进的,对于初学者来说还是有必要去学习了解一下,以加深对模块,模型的理解。

Limiiiing的博客 1513

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Spring__SpringMVC__Mybatis整合

Springmvc是controller层的,mybatis是dao和mapper层的,而spring类似粘合剂,springmvc与mybatis之间是不能直接整合的,而是通过springmvc和spring之间,mybatis与spring之间整合的。 ##Spring容器和SpringMVC容器的关系 Spring容器是一个父容器,SpringMVC容器是一个子容器,它继承自Spring容器...

qq_37432174的博客 457

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Spring整合SpringMVC及Mybatis

文章目录1、环境的搭建整合说明:整合的思路创建数据库和表结构创建maven的工程(今天会使用到工程的聚合和拆分的概念,这个技术maven高级会讲)2、搭建Spring框架3、搭建SpringMVC框架4、Spring整合SpringMVC框架5、搭建MyBatis框架6、Spring整合MyBatis框架 1、环境的搭建 整合说明: SSM整合可以使用多种方式,咱们会选择XML + 注解的方式 使用Spring去整合Mybatis和SpringMVC 整合的思路 先搭建整合的环境 先把Spring的

weixin_44051048的博客 366

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

MyBatis-Spring-SpringMVC 整合

MyBatis-Spring-SpringMVC 整合 文章目录MyBatis-Spring-SpringMVC 整合1、整个 MyBatis 和 Spring:所有的 jar 包导入2、引入 MyBatis 之前的配置:mybatis-config.xml3、SpringMVC 配置文件的编写:spring-servlet.xml4、Spring 配置文件的编写:applicationContext.xml 1、整个 MyBatis 和 Spring:所有的 jar 包导入 Spring 核心包(IOC)

前尘忆梦的博客 725

整合Mybatis、Spring、SpringMVC

整合Mybatis、Spring、SpringMVC 简单概述一下SSM它们各个的实现方式跟总结,方便更好的理解整合原理。 Mybatis 1.我们的Mybatis简单来说就是用来操作数据库写一些语句配mapping操作数据库,第一步就是导入依赖,然后再resource文件下创建一个mybatis.config ,正常使用就是把我们以前使用jdbc连接数据库的代码写上,驱动、账号、密码配置,然后我们写一个接口写一个查询的方法,然后创建一个Mybatis-Config.xml 再mapper中配置好语句设置好

qq_42283142的博客 388

SpringMVC+Spring+Mybatis整合步骤

SpringMVC+Spring+Mybatis整合步骤 1、SpringMVC SpringMVC是spring框架的一个模块,Spring和SpringMVC无需通过中间曾进行整合。 SpringMVC是基于mvc的WEB层框架。 MVC:Model View Controller 缩写 1.1、SpringMVC 运行原理 执行过程: a)当用户发起Http请求,以字符串的形式发送: ...

qq_45122010的博客 826

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

因为在整个框架整合中,Spring是管理Bean的容器,所以Spring容器要最先初始化,但是SSM是一个WEB项目,在WEB项目里最先启动的是Listener,所以Spring在WEB项目初始化是通过监听器(Listener)来启动,SpringMVC的启动是通过Servlet来启动。另外:SpringMVC是基于Spring开发,所以SpringMVC中也有一个管理自己Bean(@Controller)的容器,这个容器和上面Spring的容器不是一个容器,它只管理自己,所以两个容器之间包的扫描要分开。

打造专业技术博客 859
上一篇: Invalid property 'driver' of bean class [org.apache.commons.dbcp.BasicDataSource]
下一篇: oracle语句中把一列的值合并为一个值,用逗号隔开
哪 吒
哪 吒 2022年度博客之星TOP 1 2022年度博客之星TOP 1 领域专家: 后端开发技术领域 领域专家: 后端开发技术领域
博客等级 码龄9年 59万+粉丝 2962原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哪 吒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值