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

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

相关文章:

  • 《深入解析UART协议及其硬件实现》-- 第三篇:UART ASIC实现优化与低功耗设计
  • SpringBoot:统一功能处理、拦截器、适配器模式
  • Ubuntu 22.04 上安装 PostgreSQL(使用官方 APT 源)
  • 2025 一带一路暨金砖国家技能发展与技术创新大赛 第一届“信创适配及安全管理赛项”样题
  • 开发时如何通过Service暴露应用?ClusterIP、NodePort和LoadBalancer类型的使用场景分别是什么?
  • UDP编程
  • 4.1.1 Spark SQL概述
  • 2025年信息素养大赛 图形化编程复赛 官方样题绘制图形答案解析
  • 循环神经网络(RNN)模型
  • 【机器学习基础】机器学习入门核心算法:K均值(K-Means)
  • 强化学习(十三)DQN
  • CSS基础巩固-选择
  • 手机设备多?怎样设置IP保证不关联
  • 计算机视觉---YOLOv4
  • git reset --hard HEAD~1与git reset --hard origin/xxx
  • 力扣每日一题——连接两棵树后最大目标节点数目 ||
  • python 包管理工具uv
  • Python基础 | jupyter工具的安装与基本使用
  • 使用k8s服务进行端口代理
  • Parasoft C++Test软件单元测试_常见问题及处理
  • logo网站在线制作/杭州网站排名seo
  • 昆明网站做的好的公司哪家好/网络舆情监测系统软件
  • wordpress文件上传限制/西安seo排名
  • wordpress可视化函数/深圳seo博客
  • 一个网站绑定两个域名/优化提升
  • 优秀政府门户网站/谷歌应用商店