mybatis的xml映射问题

研究目的

最近在研究mybatis 因为总是使用spring的xml文件配置的方式或者yml配置的方式,今天想用配置类使用一下(因为application.xml整个文件其实就是一个配置类,yml没有研究过)

第一种情况 默认情况(xml文件与dao文件在相同目录时)

测试类
在这里插入图片描述

路径如下
在这里插入图片描述

看配置类

@Configuration
//@MapperScan("com.xwl.mapper")
public class MybatisConfig3 {
	//使用阿里数据源druid mysql 8以上才可以使用com.mysql.cj.jdbc.Driver
    @Bean
    public DataSource dataSource() throws Exception {
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai");
        dataSource.setUsername("root");
        dataSource.setPassword("123");
        return dataSource;
    }
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource());
        return factoryBean.getObject();
    }
   @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer=new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.xwl.mapper");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        return mapperScannerConfigurer;
    }

}

如上 添加了@Configuration就是一个配置类了,生成了三个Bean,
第一个是数据源datasource
第二个是SqlsessonFactory,它通过SqlSessionFactoryBean来获取,SqlsessionFactoryBean中调用了sqlsessionfactorybuilder来生成sqlsessionfactory,(如果是自定义的xml文件则会在这里被扫描并添加到configuration类中的mapperStatement中)
第三个则是Mapper的扫描器 相当于上面的@MapperScan注解 用于扫描指定路径下的mapper并将它注册到mapperRegistry中(在mybatis的configuration类中) 同时因为上面配置的mapper类和mapper.xml路径相同所以这个类会在扫描到mapper.class文件时也把xml文件加载进去,这个bean将mapper需要的sql语句 返回类型 参数类型等全部封装到了mapperStatement中(在mybatis的configuration类中)

其实mybatis调用也就是将所有需要的东西都放到configuration类中:

   1.通过sqlsession实现类中的getMapper来获取第三个bean注册到mapperRegistry中的mapper,生成代理对象
   2.通过代理代理对象(MapperProxy)调用接口中的方法,然后该方法会去和第二个bean生成的mapperStatement里的值比较,通过方法名这一唯一标识符来得到所有需要执行的内容**(因为mybatis底层有一个hashmap,所以一个mapper类的方法时不能重载的 记住 面试要问的呢)**
   3.通过Executor调用jdbc来执行sql

关于XML映射的问题

看下图
在这里插入图片描述
两种路径的配置都写出来就很明了了
第一种时一级级的建立目录 可以用com/xwl/mapper/ 来表示
第二种就不一样了,它解析出来时com.xwl.mapper/
这就是有些时候 看起来路径是对的 但是映射不到的原因, 第二种情况相当于一个未知目录,需要配置

如下在配置类中:

@Configuration
//@MapperScan("com.xwl.mapper")
public class MybatisConfig3 {

    @Bean
    public DataSource dataSource() throws Exception {
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai");
        dataSource.setUsername("root");
        dataSource.setPassword("123");
        return dataSource;
    }
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource());
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        factoryBean.setMapperLocations(resolver.getResources("classpath:com.xwl.mapper/*.xml"));
        return factoryBean.getObject();
    }
    public Resource[] resolveMapperLocations() {
        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
        List<Resource> resources = new ArrayList();
        List<String> mapperLocations = new ArrayList<>();
        mapperLocations.add("classpath*:com/xwl/mapper/*.xml");
/*        mapperLocations.add("classpath*:com/xwl/mapper/*.xml");
        mapperLocations.add("classpath*:com/xwl/mapper/*.xml");*/
        if (mapperLocations != null) {
            for (String mapperLocation : mapperLocations) {
                try {
                    Resource[] mappers = resourceResolver.getResources(mapperLocation);
                    resources.addAll(Arrays.asList(mappers));
                } catch (IOException e) {
                    // ignore
                }
            }
        }

        return resources.toArray(new Resource[resources.size()]);
    }


   @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer=new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.xwl.mapper");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        return mapperScannerConfigurer;
    }
}

通过手动配置来加入 相当于spring.xml中配置一样 如果有多个xml文件路径 就可以使用上面代码中的resolveMapperLocations来批量读取

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值