首页 > 其他分享 >Spring整合Mybatis

Spring整合Mybatis

时间:2023-09-08 09:44:26浏览次数:30  
标签:Spring UserMapper 接口 sqlSession 整合 spring Mybatis public

Spring整合Mybatis就是将原本Mybatis中的Mapper.xml文件在Spring容器中注册为对象。

导入mybatis-spring依赖包

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.2</version>
</dependency>

创建实体类User,创建UserMapper接口和UserMapper.xml实现的配置文件。

@Data
public class User {
    private int id;
    private String name;
    private String pwd;
}
public interface UserMapper {
    List<User> getUser();
}
<?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.along.mapper.UserMapper">
    <select id="getUser" resultType="user">
        select * from user;
    </select>
</mapper>

创建一个spring-dao.xml文件,将原本Mybatis核心配置文件连接数据库的工作代替,创建一个数据源的bean。原本使用Mybatis需要将核心配置文件转化为流,再用SqlSessionFactoryBuilder的build方法创建一个SqlSessionFactory对象,再用SqlSessionFactory的openSession方法创建一个SqlSession对象,再用SqlSession的getMapper方法读取接口的字节码创建一个接口的实例对象,这样我们就可以使用这个实例对象的各种方法了。但在spring容器中,这些步骤都可以省略了,我们先生成一个SqlSessionFactory的bean,里面配置数据源,绑定核心配置文件和Mapper.xml文件等属性。然后生成一个SqlSessionTemplate的bean,这个就相当于Mybatis中的SqlSession,功能是一致的。这样我们如果需要使用Mapper接口中的方法就不需要再写Mybatis中这些繁琐的步骤了,使用组合的方法就可以直接使用,使用组合的时候在类里面加上set方法,这个类也需要注册到spring容器中去,使用set注入。

<?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">
    <!--DateSource-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai"/>
        <property name="username" value="数据库账号"/>
        <property name="password" value="数据库密码"/>
    </bean>
    <!--SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--绑定Mybatis的核心配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/along/mapper/UserMapper.xml"/>
    </bean>
    <!--SqlSessionTemplate:就是我们使用的sqlSession-->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
</beans>

写一个Mapper接口的实现类,就是用组合的方法,从spring容器中取出sqlSession。

public class UserMapperImpl implements UserMapper{
    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    public List<User> getUser() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.getUser();
    }
}

将接口实现类注册到spring容器中去,以后使用dao层的方法就不用使用Mybatis中那些繁琐的步骤了,Mybatis已经被整合到spring中了,直接创建spring容器从里面拿就可以了。

<bean id="userMapperImpl" class="com.along.mapper.UserMapperImpl">
  <property name="sqlSession" ref="sqlSessionTemplate"/>
</bean>

总结整合步骤

1.编写数据源配置
2. sqlSessionFactory
3. sqlSessionTemplate
4.需要给接口加实现类
5.将自己写的实现类,注入到Spring中
6.测试使用即可!

标签:Spring,UserMapper,接口,sqlSession,整合,spring,Mybatis,public
From: https://www.cnblogs.com/panglinglong/p/17686299.html

相关文章

  • SpringApplication
    SpringApplication是SpringBoot驱动Spring应用上下文的引导类。SpringApplication的run()方法启动Spring应用,作用为Spring应用创建并初始化Spring上下文。SpringApplication可以自定义Banner和自定义SpringApplication实例1。@SpringBootApplication//使用这个注解必须先引......
  • Spring Cloud
    什么是SpringCloudSpringCloud是一系列框架的有序集合。它利用SpringBoot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、智能路由、消息总线、负载均衡、断路器、数据监控等,都可以用SpringBoot的开发风格做到一键启动和部署。Spring......
  • MyBatis-Plus的使用介绍
    MyBatis-Plus是MyBatis的增强工具,它简化了MyBatis的开发流程,提供了更多的便捷功能。下面是MyBatis-Plus的使用介绍:引入依赖:在Maven项目中,需要在pom.xml文件中添加MyBatis-Plus的依赖。com.baomidoumybatis-plus-boot-starter最新版本号com.baomidoumybatis-p......
  • MyBatis操作Oracle(实现兼容Oracle和MySQL)
    MyBatis操作Oracle(实现兼容Oracle和MySQL)以Oracle11g来演示,只需要创建序列,不需要创建触发器,通过ORM框架操作来生成主键MySQL版本是8.x代码地址:https://gitee.com/zhang-zhixi/springboot-mp-oracle-auto.git1、分别创建Student表Oracle:CREATETABLE"STUDENT"("ID"N......
  • springboot策略模式
    一.定义接口publicinterfacePearlTaskService{IntegergetTaskType();Map<String,Integer>execute(LonguserId,GameTaskgameTask,StringgameCode);}二.定义抽象类@Slf4jpublicabstractclassPearlTaskStrategyimplementsPearlTaskService{protec......
  • [SpringSecurity5.6.2源码分析四]:WebSecurityConfiguration
    WebSecurityConfiguration的重点是通过WebSecurity创建FilterChainProxy• 先分析内部的方法1、elegatingApplicationListener• 看名字就能看出来注册了一个委托类型的监听器publicclassWebSecurityConfigurationimplementsImportAware,BeanClassLoaderAware{............
  • SpringBoot整合thymeleaf
    JavaEE领域有几种常用的模板引擎:Jsp,Thymeleaf,Freemarker,Velocity等.对于前端页面渲染效率来说JSP其实还是最快的,Velocity次之.Thymeleaf虽然渲染效率不是很快,但语法比较轻巧.Thymeleaf支持html5标准,Thymeleaf页面无需部署到servlet开发到服务器上,以.html后缀结......
  • spring中的aop(面向切面编程)需要到导入的包与简单示例
    2023-09-07<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://......
  • springsecurity
    编辑 pom.xml,添加 spring-boot-starter-securtiy 依赖即可。添加后项目中所有的资源都会被保护起来。<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-security</artifactId></dependency>编写SecurityConfig......
  • 详细说明 BootStrap整合 BootStrap 【整合V3版本的,需要依赖JQuery】
    文章底部有个人公众号:热爱技术的小郑。主要分享开发知识、有兴趣的可以关注一下。为何分享?踩过的坑没必要让别人在再踩,自己复盘也能加深记忆。利己利人、所谓双赢。前言以下这个图说明了、如果你使用的是BootStrapV3,那么你在使用BootStrap的时候,同时需要依赖Jquery。这一篇文章......