当前位置: 首页 > ai >正文

Spring框架学习day4--Spring集成Mybatis(IOC)

  • Spring集成Mybatis
    • 1.添加jar包(pom.xml)
    • 2.配置sqlSessionFactiory(spring.xml)
    • 3.再service类中注入Dao代理接口
    • 4.测试类
    • 5文件结构

Spring集成Mybatis

Spring集成Mybatis其核心是将SqlSessionFactory交由Spring管理,并由 Spring管理对dao接口的代理实现。 导入mybatisjar包

创建新的ssm项目为例文件结构如下

1.添加jar包(pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<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://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>ssm</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.2.RELEASE</version></dependency><!-- spring-jdbc--><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.2.RELEASE</version></dependency><!-- 阿里数据源--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.10</version></dependency><!--        mysql驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.16</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.2</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.1</version></dependency></dependencies>
</project>

image-20250529222630979

2.配置sqlSessionFactiory(spring.xml)

<!--    spring管理生成SqlSessionFactory--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="configLocation" value="classpath:mybatis.xml"></property>
<!--        指定配置文件--><property name="mapperLocations" value="classpath:mappers/*Mapper.xml">
<!--            指定文件地址--></property></bean>

生成接口代理(spring.xml)

<!--    spring管理生成接口代理对象--><bean id="mapperFactory" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="org.example.dao"></property>
<!--        对指定包下的接口进行扫描,并生成接口代理的对象--><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean>

完整spring.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd
"><!--        开启注解扫描  对指定包下的注解进行扫描 ,检查添加spring类注解标签的类--><context:component-scan base-package="org.example" ></context:component-scan><context:property-placeholder location="config.properties"/><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><!--       方法一:通过配置文件配置数据源--><!--                <property name="driverClassName" value="com.mysql.jdbc.cj.Driver"/>--><!--        <property name="url" value="数据库连接/>--><!--        <property name="username" value="root"/>--><!--        <property name="password" value="root"/>--><!--        <property name="initialSize" value="1"/>--><!--        <property name="maxActive" value="1"/>--><!-- 方法二:通过注解配置数据源--><property name="driverClassName" value="${driverClassName}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/><property name="initialSize" value="${initialSize}"/><property name="maxActive" value="${maxActive}"/></bean>
<!--    spring管理生成SqlSessionFactory--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="configLocation" value="classpath:mybatis.xml"></property>
<!--        指定配置文件--><property name="mapperLocations" value="classpath:mappers/*Mapper.xml">
<!--            指定文件地址--></property></bean>
<!--    spring管理生成接口代理对象--><bean id="mapperFactory" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="org.example.dao"></property>
<!--        对指定包下的接口进行扫描,并生成接口代理的对象--><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean>
</beans>

注:对应的mybatis.xml文件有所变化

<?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><settings><!--        打印日志--><setting name="logImpl" value="STDOUT_LOGGING"/><!--            开启驼峰命名规则--><setting name="mapUnderscoreToCamelCase" value="true"/></settings><!--    为类配置起别名--><typeAliases><!--        下面别名怎么引用呢?  直接在xml文件中使用别名就可以了--><typeAlias type="org.example.model.Admin" alias="Admin"/></typeAliases>
</configuration>

3.再service类中注入Dao代理接口

@Service("loginService")
public class LoginService {@AutowiredLoginDao loginDao;public Admin login(Admin admin){return loginDao.login(admin);}
}

4.测试类

package org.example.test;import org.example.model.Admin;
import org.example.service.LoginService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class test {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");Admin admin = context.getBean("admin", Admin.class);admin.setAccount("admin");admin.setPassword("111");LoginService loginService = context.getBean("loginService", LoginService.class);Admin result = loginService.login(admin);System.out.println(result);}
}

5文件结构

image-20250529223641884

http://www.xdnf.cn/news/9713.html

相关文章:

  • Vue 3 的路由管理
  • 小白成长之路-Linux日志管理
  • [MMU]IOMMU的主要职能及详细的验证方案
  • 3 分钟学会使用 Puppeteer 将 HTML 转 PDF
  • Axure设计案例——科技感渐变线性图
  • 不可变集合类型转换异常
  • Astra学习之-如何修改Header-logo和favicon图标
  • Linux | Shell脚本的基础知识
  • 基于ubuntu安装hadoop
  • .NET8入门:14.ASP.NET Core MVC进阶——Model
  • 前端高频面试题1:HTML/CSS/浏览器/计算机网络
  • 安装 Node.js 和配置 cnpm 镜像源
  • Java异常处理的全面指南
  • 基于通义千问的儿童陪伴学习和成长的智能应用架构。
  • Spring AI 之对话记忆(Chat Memory)
  • [网页五子棋][匹配模块]处理开始匹配/停止匹配请求(匹配算法,匹配器的实现)
  • python h5py 读取mat文件的<HDF5 object reference> 问题
  • StarRocks x Iceberg:云原生湖仓分析技术揭秘与最佳实践
  • 【大模型】Bert变种
  • Kubernetes资源申请沾满但是实际的资源占用并不多,是怎么回事?
  • 微深节能 码头装卸船机定位与控制系统 格雷母线
  • WPF 按钮悬停动画效果实现
  • 【五模型时间序列预测对比】Transformer-LSTM、Transformer、CNN-LSTM、LSTM、CNN
  • 《AI大模型的开源与性能优化:DeepSeek R1的启示》
  • 互斥锁、自旋锁、读写锁、悲观锁、乐观锁的应用场景
  • 深入理解C#中的LINQ:数据查询的终极利器
  • 2013-2021年各省电子商务数据
  • 认识多系统萎缩:一种隐匿进展的神经退行性问题
  • spring IOC控制反转
  • 【春秋云镜】CVE-2022-26965 靶场writeup