Spring Mybatis整合小例子

Spring整合Mybatis全过程解析 MyBatis 是一款常用的持久层框架,使得程序能够以调用方法的方式执行某个指定的SQL,将执行SQL的底层逻辑进行封装。多数与Spring结合使用,本文讨论Spring如何整合Mybatis,首先看下Mybatis运行机制 1. Mybatis运行机制 1.1 Mybatis功能架构设计 Mybatis的功能架构分为三层: (1)API接口层:提供给外部使用的接口API,开发人员通过这些本地API来操纵数据库。接口层一接收到调用请求就会调用数据处理层来完成具体的数据处理。 (2)数据处理层:负责 阅读详情

1、新建JavaProject工程 spring-mybatis、部署spring、新建包、添加properties属性文件。目录如下:

2、applicationConetxt.xml文件已经把mybatis-config.xml中的dataSource、mappers等配置整合成bean或peroperty

<?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:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
    http://www.springframework.org/schema/context    
    http://www.springframework.org/schema/context/spring-context-3.1.xsd    
    http://www.springframework.org/schema/mvc    
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  
  <!-- 自动扫描所有的包,加载装配所有的实体类bean -->
  <context:component-scan base-package="com"/>
 
 <!-- 加载配置文件 -->
 <context:property-placeholder location="classpath:*.properties" />
 
 <!-- 配置数据源 -->
 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="${driver}"/>
  <property name="url" value="${url}"/>
  <property name="username" value="${user}"/>
  <property name="password" value="${password}"/>
 </bean>
 
 <!-- sqlSessionFactory -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <!-- 加载 mapper.xml的配置文件 -->
  <property name="mapperLocations" value="com/mapper/*.xml"/>
  <!-- 添加所需的数据源 -->
  <property name="dataSource" ref="dataSource"/>
 </bean>
 
 <!-- 扫描Dao包,加载接口映射类 -->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 	<property name="basePackage" value="com.mapper" />
 	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 	
 </bean>
 
 
 <!-- (事务管理)transaction manager -->  
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean> 
 
 
</beans>

3、mapper文件

 package com.mapper;
import java.util.List;
import com.model.Student;

public interface StudentMapper {

	public List<Student> getAllStudent();
}
<?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="com.mapper.StudentMapper">

	<select id="getAllStudent" resultType="com.model.Student">
		select * from student
	</select>

</mapper>

4、实体类Student

package com.model;

public class Student {
	private int sno;
	private String sname;
	private double score;
	
	public int getSno() {
		return sno;
	}
	public void setSno(int sno) {
		this.sno = sno;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	public Student(int sno, String sname, double score) {
		super();
		this.sno = sno;
		this.sname = sname;
		this.score = score;
	}
	public Student() {
		super();
	}
	@Override
	public String toString() {
		return "Student [sno=" + sno + ", sname=" + sname + ", score=" + score + "]";
	}
	
	
}

5、从配置文件中获得ApplicationContext上下文的工具类

package com.tool;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppContext {

	private static ApplicationContext appContext;
	
	static{
		appContext=new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	
	public static <T>T getBean(Class<T> t){
		return appContext.getBean(t);
	}
}

6、service业务包

package com.service;

import java.util.List;

import com.model.Student;

public interface InterStudentService {

	public List<Student> showStudentList();
}
package com.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mapper.StudentMapper;
import com.model.Student;
import com.tool.AppContext;

@Service
public class StudentServiceImpl implements InterStudentService {

	@Resource
	private StudentMapper studentMapper;	

	@Override
	public List<Student> showStudentList() {
		
		//ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		studentMapper=AppContext.getBean(StudentMapper.class);
		
		return studentMapper.getAllStudent();
	}

}

7、测试类Test

package com.test;

import com.service.InterStudentService;
import com.service.StudentServiceImpl;
import com.tool.AppContext;

public class Test {

	public static void main(String[] args) {
		
		//InterStudentService stuService=new StudentServiceImpl();
		
		InterStudentService stuService=AppContext.getBean(StudentServiceImpl.class);
		
		System.out.println(stuService.showStudentList());
		
	}
	
}

8、结果

Qt 5.15.2 MinGW下qthttpserver的构建、部署与实战 HTTP服务是现代软件中常见的交互基础,从Web后端到桌面应用的本地接口都离不开它。在C++/Qt技术栈中,开发者通常面临选择:引入重型Web服务器,或自行实现Socket协议。QHttpServer基于QtNetwork提供轻量HTTP服务能力,借助CMake构建系统可快速集成到现有工程。本文从工具链选型切入,对比MSVC与MinGW的ABI差异,并围绕Qt 5.15.2 LTS版本说明Release构建参数、库依赖及打包流程。通过windeployqt部署工具解决运行时依赖问题,同时分享路由编写、JSO 阅读详情

相关推荐

使用电荷放大器处理压电加速度计输出

电阻器 R p模拟传感器的绝缘电阻,为产生的电荷创建泄漏路径。在此图中,顶部波形显示传感器产生的电荷,而底部波形显示电荷放大器的输出。这需要具有大输入阻抗的放大级,以防止产生的电荷通过与传感元件并联的放大器的输入阻抗泄漏。换句话说,电荷放大器补偿传感器产生的电荷 q p ,反馈电容器 C F中具有相反极性的等量电荷。因此,任何与传感器并联的绝缘电阻,如电缆的绝缘电阻或传感器的漏电电阻Rp,都不能流过电流。漂移是指电荷放大器输出在一段时间内发生的变化,而不是由被测物理参数的变化(我们讨论的加速度)引起的。

weixin_45260069的博客 3905

了解Spring整合mybatis,这一篇就够了

目录 一、引言: 1.1、Spring的两大核心思想: 1.2 、Spring可以对mybatis提供哪些支持? 二、Spring整合mybatis准备工作: 2.1 创建maven工程 2.2 部署mybatis、Spring、整合依赖等框架 三、配置数据源(Druid连接池) 3.1 添加Druid依赖 3.2 创建Druid.properties属性文件 3.3 在ApplicationContext.xml配置数据源 四:配置sqlsessionFactory 五、配置Map

WuHuaSen的博客 6550

金蝶EAS uploadlogo漏洞实战:如何快速检测你的系统是否受影响

本文详细解析了金蝶EAS uploadlogo任意文件上传漏洞的检测与防护方法,帮助企业安全团队快速识别系统风险。通过资产发现、漏洞验证和应急响应方案,有效防范财务数据泄露和系统入侵风险,提升企业整体安全防护能力。

weixin_29322553的博客 386

Spring和Mybatis的整合

spring和mybatis的整合,回顾之前的笔记

beautifulsen的博客 6021

SQL注入绕过information_schema:实战技法与替代路径详解

SQL注入是Web安全领域的核心漏洞类型,其原理是通过构造恶意SQL语句,干扰应用程序与数据库的正常交互逻辑,从而窃取或篡改数据。在渗透测试与CTF竞赛中,绕过常见防护措施是提升技术能力的关键。当标准的信息收集方式受阻时,例如无法访问information_schema数据库,攻击者需要掌握替代的元数据获取方法。这些技术价值在于模拟真实环境中日益严密的防御体系,锻炼在受限条件下的系统性信息收集思维。典型的应用场景包括存在WAF过滤、权限限制或编码干扰的实战环境。本文将围绕SQL注入这一核心,深入探讨如何利用

weixin_33980459的博客 429

kaggle竞赛平台上数据集下载详解

从kaggle 网站上下载数据集既保证了速度又避免了网络的不稳定造成下载失败问题。该平台也可以上传代码作为自己的代码库,不过本人更习惯将代码上传到github上。希望本人整理的kaggle库使用技巧能够帮助到正在阅读本博客的你。此外还有一篇博客针对kaggle数据集下载问题的几个问题的相关文章进行了整理【博客链接】。

li1784506的博客 2万+

Spring——Spring整合MyBatis

Spring——Spring整合MyBatis

贝尔摩德苦艾酒 2万+

从时序参数到电压供给:TTL(RGB)液晶屏驱动调试全解析

本文全面解析TTL(RGB)液晶屏驱动调试的关键技术,从时序参数配置到电压供给系统搭建,详细介绍了同步信号参数、寄存器配置技巧以及升压电路设计。通过实战案例和常见故障分析,帮助工程师快速掌握液晶显示屏调试方法,提升开发效率。

weixin_30588675的博客 247

Spring+Mybatis的整合

所以,该整合,只需要将 SqlSessionFactory 的对象生成器 SqlSessionFactoryBean 注册在 Spring 容器中,再将其注入给 Dao 的实现类即可完成整合。实现 Spring 与 MyBatis 的整合常用的方式:扫描的 Mapper 动态代理 Spring 像插线板一样,mybatis 框架是插头,可以容易的组合到一起。使用mybatis,需要创建mybatis框架中的某些对象,使用这些对象,就可以使用mybatis提供的功能了。在基础的 MyBatis 中,通过。

weixin_65824274的博客 2108

双目相机模型

近期想研究一下 双目相机 的内容,故先把 理论 弄清楚! 一、双目相机模型 针孔相机模型描述了 单个相机 的成像模型。然而,仅根据一个像素,我们是无法确定 这个空间点的具体位置的。这是因为,从相机光心到归一化平面连线上的所有点,都可以 投影至该像素上。只有当 P 的深度确定时(比如通过双目或 RGB-D 相机),我们才能确 切地知道它的空间位置。 测量像素距离(或深度)的方式有很多种,像人眼就可以根据左右眼看到的景物差异 (或称视差)来判断物体与我们的距离。双目相机的原理亦是如..

努力努力努力 2408

SAP 中配置权限职责菜单并分配给用户的核心流程是:用PFCG 创建 / 维护角色(含菜单、事务码、权限)→ 生成权限参数文件 → 通过 SU01/SU10 将角色分配给用户

SAP权限配置核心流程:1)通过PFCG创建角色,配置菜单事务码及权限对象;2)生成权限参数文件;3)用SU01/SU10分配角色给用户。关键步骤包括:在PFCG中维护菜单结构、设置权限字段值、生成Profile,通过用户比较验证权限。批量分配可用SU10,特殊需求可创建复合角色或自定义权限对象。常见问题需检查权限缺失(SU53)、菜单分配及组织级别设置。流程简化为:创建角色→配置权限→分配用户→验证生效。

金牌架构师 1273

Spring与mybatis简单整合例子

相关jar(其中有些不必须有,由于是从另外项目复制过来的,懒得删): pojo: package com.bjsxt.pojo; public class Flower { private int id; private String name; private double price; private String production; public Flower(in...

tomcat我们走 295

当C#遇见声音魔法:从麦克风到文字的0.1秒奇幻漂流

为什么选300Hz?因为人声基频通常在85-255Hz,但清脆的“啊”可能到2kHz,所以300Hz是安全区!(参考知识库[1])

墨夶的博客 365

spring整合mybatis例子

新建一个Maven项目 pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://mave...

weixin_45799367的博客 180

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

老版QQ原生表情gif动态

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

MyBatis 与 Spring 的整合(第一个例子)【学习笔记 四 】

MyBatis 与 Spring 的整合       1.在pom.xml文件中添加下列内容,导入相关的jar包。 &lt;!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --&gt; &lt;dependency&gt; &lt;groupId&gt;org.mybatis&...

卟♪恋 555

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Spring整合mybatis例子

1、固定模板spring-dao.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/..

weixin_42701161的博客 213

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Spring与Mybatis整合例子(基于MapperScannerConfigurer的整合)

前言 最近在尝试 Spring 与 Mybatis 的整合,所参考的是 黑马程序员 编著的ssm框架教材,书中提及一共有三种整合方式,这里就不一一列举了,只列举最简单,也最常用的方式,那就是基于 MapperScannerConfigurer 的整合。

爱吃猪蹄的博客 2228

mybatis例子及mybatis和spring整合

mybatis:初探   MyBatis本身Apache的一个开源项目ibatis,2010年这个项目由Apache software foundation迁移到了Googlecode,并且改名为mybatis.   MyBatis是支持普通sql查询、存储过程和高级映射的优秀持久层框架。Mybatis消除了几乎所有jdbc代码和参数的手工设置以及结果集的检索。Mybatis使用简单的xm

long636的专栏 914

spring整合mybatis的简单例子及原理

spring-mybatis介绍代码jdk动态代理注入mapper优雅地注入mapper像mybatis一样工作 介绍 spring是spring,它是个容器的框架,mybatis是mybatis,它是个封装jdbc的框架。它们两个怎么联系起来呢? 当然是spring来收纳mybatis呀。我会给出一个小例子,你必须注意,哪些是spring的东西,哪些是mybatis的东西,以及代码中奇怪的地方。 代码 先讲mybatis的东西: @Data public class Entity implements

weixin_43810802的博客 403

mybatis 不整合spring 入门小例子

先上一个搭建完的项目结构截图: 相对比较重要的配置文件有 db.properties , SqlMappingConfig.xml , mapper/User.xml , log4j.properties (主要是 mybatis 有日志管理这些依赖 后面 pom.xml 中也要添加 log4j 的jar 包依赖) jdbc.driver=com.mysql.jd...

dianxunma2886的博客 180

Spring-MyBatis整合简单例子及个人的经验杂谈

Spring与MyBatis整合例子,相关的源码分析及个人的一些想法和对MyBatis整合相关源码的杂谈

z28126308的专栏 3023

Spring mvc整合Mybatis例子

整合的框架理解  Spring mvc框架是前面展示使用JSP进行页面展示,使用ServletDispatcher或者ContextLoadListener作为bean的容器,其中包括拦截器和视图解析。业务层使用Controller进行业务包装,底层使用Service实现具体的数据交互。   Mybatis是针对sqlmap使用的语句作为实现返回到service中的方法。 环境:Spring版本

sutonline的博客 788

DB2+MyBatis+spring整合例子

DB2+MyBatis+spring整合例子

那些疯狂到以为自己能够改变世界的人,才能真正改变世界! 6591

spring mvc + mybatis + mysql 整合的一个简单的登录例子

今天用spring跟mybatis整合写了一个简单的登录例子,第一次整合,给自己做个笔记,可能注释写的有点少,做的不足的地方谢谢指出,也分享给需要的朋友,下面给出登录失败和成功的效果图:        这个登录例子用的工具是myeclipse8.6+mysql5.1,使用到的技术有spring3.0+mybatis3.2.3+mybatis-spring-1.1.1(这个是spring跟m

oo流云oO 1万+

Mybatis笔记之四 -- mybatis与spring整合简单例子

1、导入开发所需要的jar包

说:/~不了的专栏 596

mybatis+spring整合的几个好的例子

触发器与序列整合的例子: http://wanqiufeng.blog.51cto.com/409430/515523 比较详细的一个参考方案: http://www.oschina.net/code/snippet_94799_3366   dao层的全部操作方式: http://www.oschina.net/code/snippet_125563_8870...

weixin_33724059的博客 183
上一篇: Maven Junit单元测试入门
下一篇: SSM 整合简单实现crud
zero_130
博客等级 码龄10年 21粉丝 46原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值