做兼职写小说网站,做代收水果是什么网站,2003服务器建设网站,焦作焦煤电子商务网站建设大家好#xff0c;我是王有志。
今天给大家带来的是一道来自京东的 MyBatis 面试题#xff1a;MyBatis 中有几种加载映射器#xff08;Mapper.xml#xff09;的方式#xff1f;
常见加载 MyBatis 映射器的方式有 5 种#xff0c;可以根据不同的使用方式来进行具体区分我是王有志。
今天给大家带来的是一道来自京东的 MyBatis 面试题MyBatis 中有几种加载映射器Mapper.xml的方式
常见加载 MyBatis 映射器的方式有 5 种可以根据不同的使用方式来进行具体区分
MyBatis 原生应用即不与 Spring 或 Spring Boot 集成可以通过配置文件和 Java 编码的方式加载映射器MyBatis 与 Spring 集成可以通过加载 Spring Bean 的方式完成 MyBatis 映射器的加载MyBatis 与 Spring Boot 集成可以通过MapperScan或Mapper注解完成 MyBatis 映射器的加载。
下面我们来具体看下每种方式是如何加载 MyBatis 映射器的。
原生 MyBatis 应用
原生 MyBatis 应用中即不与 Spring 或 Spring Boot 集成的 MyBatis 应用可以通过两种方式加载映射器Mapper.xml
通过 myabtis-config.xml 加载映射器通过 Java 编码的方式加载映射器。
通过 mybatis-config.xml 加载映射器
与 MyBatis入门中实现的简单例子一样只需要在 mybatis-config.xml 中配置映射器即可
?xml version1.0 encodingUTF-8 ?
!DOCTYPE configuration PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-config.dtdconfiguration!-- 省略环境配置 --mappersmapper resourcemapper/UserMapper.xml/mapper resourcemapper/CompanyMapper.xml//mappers
/configuration通过 Java 编码的方式加载映射器
MyBatis入门中的附录中同样有相关的例子
DataSource dataSource new PooledDataSource(com.mysql.cj.jdbc.Driver, jdbc:mysql://localhost:3306/mybatis, root, 123456);
TransactionFactory transactionFactory new JdbcTransactionFactory();Environment environment new Environment(development, transactionFactory, dataSource);
Configuration configuration new Configuration(environment);// 加载映射器
configuration.addMapper(UserMapper.class);如果希望加载某个包下的全部 Mapper.xml可以使用Configuration#addMappers进行加载
configuration.addMappers(com.wyz.mapper);MyBatis 与 Spring 集成
MyBatis 与 Spring 集成后可以通过加载 Bean 的方式加载 MyBatis 的映射器Mapper.xml我们还是按照 MyBatis入门中的步骤先完成 Java 对象 UserDOJava 接口 UserMapper 的创建接着是编写 MyBatis 的映射器 UserMapper.xml最后我们还需要引入相关依赖
dependencies!-- 省略 MySQLjunit等依赖 --dependencygroupIdorg.mybatis/groupIdartifactIdmybatis-spring/artifactIdversion3.0.3/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-core/artifactIdversion6.1.4/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-jdbc/artifactIdversion6.1.4/version/dependency
/dependencies接下来我们通过注入 Spring Bean 的方式来加载 Mapper.xml这里我们创建 spring-beans.xml 配置文件
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdbean iddataSource classorg.springframework.jdbc.datasource.DriverManagerDataSourceproperty namedriverClassName valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql://localhost:3306/mybatis/property nameusername valueroot/property namepassword value123456//beanbean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBeanproperty namedataSource refdataSource/property namemapperLocations valuemapper/UserMapper.xml//bean!-- 通过 Spring Bean 的方式加载 MyBatis 映射器--bean iduserMapper classorg.mybatis.spring.mapper.MapperFactoryBeanproperty namemapperInterface valuecom.wyz.mapper.UserMapper/property namesqlSessionFactory refsqlSessionFactory//bean
/beans最后我们来测试
package com.wyz.mapper;import com.wyz.entity.UserDO;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List;Slf4j
public class UserMapperTest {Testpublic void test(){ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring-beans.xml);UserMapper userMapper applicationContext.getBean(userMapper, UserMapper.class);ListUserDO users userMapper.selectAll();for(UserDO user : users) {System.out.println(user.toString());}}
}MyBatis 与 Spring Boot 集成
MyBatis 与 Spring Boot 集成后可以通过注解的方式或配置文件的方式加载 MyBatis 的映射器Mapper.xml使用起来非常的方便。首先我们还是做好前期的准备创建 Java 对象 UserDOJava 接口 UserMapperMyBatis 的映射器文件 UserMapper.xml接着我们来写 Spring Boot 的配置文件主要完成数据库相关的配置
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatisusername: rootpassword: 123456通过 MapperScan 注解加载映射器
我们需要在 Spring Boot 应用的启动类上使用MapperScan注解并扫描 Mapper 文件所在的包来加载 MyBatis 的映射器
package com.wyz;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
MapperScan(com.wyz.mapper)
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}最后我们来进行测试
package com.wyz.mapper;import com.wyz.entity.UserDO;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;SpringBootTest
public class UserMapperTest {Autowiredprivate UserMapper userMapper;Testpublic void test() {ListUserDO users userMapper.selectAll();for(UserDO user : users) {System.out.println(user.toString());}}
}通过 Mapper 注解加载映射器
除了使用MapperScan注解加载某些包下的映射器外还可以为每个映射器接口添加Mapper接口来加载映射器我们把 Spring Boot 启动类上的MapperScan注解删除为 UserMapper 接口添加Mapper注解
package com.wyz.mapper;import com.wyz.entity.UserDO;import java.util.List;public interface UserMapper {ListUserDO selectAll();
}好了今天的内容就到这里了如果本文对你有帮助的话希望多多点赞支持如果文章中出现任何错误还请批评指正。最后欢迎大家关注分享硬核 Java 技术的金融摸鱼侠王有志我们下次再见