首页 > 其他分享 >MyBatis-Spring包自动扫描MyBatis Mapper接口并将其注册为Spring Bean

MyBatis-Spring包自动扫描MyBatis Mapper接口并将其注册为Spring Bean

时间:2023-04-03 15:14:25浏览次数:27  
标签:Mapper Spring public book org MyBatis import com id

学习spring整合mybatis时,写SQL语句的Mapper接口明明没有任何被spring接管的痕迹(前面没有注解)但在serviceimpl类中却可以被自动装载。

 

BookDao.java(mapper接口类):

package com.itheima.dao;

import com.itheima.domain.Book;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface BookDao {

//    @Insert("insert into tbl_book values(null,#{type},#{name},#{description})")
    @Insert("insert into tbl_book (type,name,description) values(#{type},#{name},#{description})")
    public void save(Book book);

    @Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")
    public void update(Book book);

    @Delete("delete from tbl_book where id = #{id}")
    public void delete(Integer id);

    @Select("select * from tbl_book where id = #{id}")
    public Book getById(Integer id);

    @Select("select * from tbl_book")
    public List<Book> getAll();
}

 

 

 BookServiceImpl.java(业务层实现类):

package com.itheima.service.impl;

import com.itheima.dao.BookDao;
import com.itheima.domain.Book;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;

    public boolean save(Book book) {
        bookDao.save(book);
        return true;
    }

    public boolean update(Book book) {
        bookDao.update(book);
        return true;
    }

    public boolean delete(Integer id) {
        bookDao.delete(id);
        return true;
    }

    public Book getById(Integer id) {
        return bookDao.getById(id);
    }

    public List<Book> getAll() {
        return bookDao.getAll();
    }
}

 

 

这是因为之前引入的包MyBatis-Spring,默认会扫描整个项目中指定的包及其子包下的所有Mapper接口,并将其注册为Spring Bean。这个扫描范围是可以配置的,通过设置MapperScannerConfigurer的basePackage属性来指定要扫描的包。

若是在配置类里设置扫描范围,有两种写法,第一种是写在注解上:

@Configuration
@MapperScan("com.example.dao")
public class MyBatisConfig {
    // 其他配置
}

 第二种是定义实体bean:

package com.itheima.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;

public class MyBatisConfig {

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.itheima.dao");
        return msc;
    }
//其他配置

}

选其一就可

标签:Mapper,Spring,public,book,org,MyBatis,import,com,id
From: https://www.cnblogs.com/ban-boi-making-dinner/p/17283077.html

相关文章

  • 22-springboot应用监控-actuator
    可以做成页面监控(springboot-admin),而不是json的格式,看起来会更方便。在生产环境中,有时可能需要监控服务的可用性,spring-boot的actuator就是提供了对应用的配置查看、健康检查、相关功能统计等,可以通过HTTP,JMX来访问这些监控功能;(端点)如何使用该功能呢?1、在项目的Maven中添......
  • 加载spring配置的两个方法AnnotationConfigApplicationContext()和getRootConfigClass
    在Spring中,AnnotationConfigApplicationContext类和AbstractAnnotationConfigDispatcherServletInitializer类中的getRootConfigClasses()方法都是用来加载Spring配置类,并创建Spring容器的。因此,它们的作用是相似的,都是用来配置Spring容器的。但是,它们的使用场景和......
  • SpringCloud大文件上传解决方案支持分片断点上传
    ​IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头。 一. 两个必要响应头Accept-Ranges、ETag        客户端每次提交下载请求时,服务端都要添加这两个响应头,以保证客户端和服务端将此下载识别为可以断点续传......
  • SpringMVC大文件上传解决方案支持分片断点上传
    ​ 以ASP.NETCoreWebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API,包括文件的上传和下载。 准备文件上传的API #region 文件上传  可以带参数        [HttpPost("upload")]        publicJsonResultuploadProject(I......
  • 【Spring】AOP
    添加Maven依赖:<!--aspectj包的依赖--><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.9.1</version></dependency><!--aspectjr......
  • Spring源码复习
    Bean的生命周期 ApplicationContextCentralinterfacetoprovideconfigurationforanapplication.*Thisisread-onlywhiletheapplicationisrunning,butmaybe*reloadediftheimplementationsupportsthis.**<p>AnApplicationContextprovides:*<ul......
  • Mybatis配置文件解析(转载)
    流程图demo案例还是从案例开始。publicstaticvoidmain(String[]args){Stringresource="mybatis-config.xml";InputStreaminputStream=null;SqlSessionsqlSession=null;try{inputStream=Resources.getResourceAsStream(resourc......
  • Spring Initailizr(项目初始化向导)
    本地创建官网创建版在Spring官网https://start.spring.io/中选择此时这个项目以压缩包形式下载到本地文件中,然后解压,导入IDEA中阿里start创建如果国外的网址不能通过,将网址更换为http://start.aliyun.com更换版本号在pom.xml中更换maven依赖......
  • 【Spring】注解器
    applicationContext.xml1<?xmlversion="1.0"encoding="UTF-8"?>2<beansxmlns="http://www.springframework.org/schema/beans"3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"4xmln......
  • Mybatis之数据库连接+PageHelper分页插件+Mybatis-Plus插件
    MyBatisPlus教程(人人便成为)https://www.cnblogs.com/chch213/p/16320820.html前言ORM框架:对象关系映射 objectrelationalmapping 半自动ORM映射工具(自己编写sql语句)  Hibernater属于全自动映射规则:数据库表>类|表字段>类的属性|表数据>对象 JDBC操......