MyBatis 配置文件元素说明

MyBatis 配置文件

  1. properties元素的用法
  2. settings元素的配置
  3. typeAliases的用法
  4. typeHandler在mybatis中的用法
  5. ObjectFactory的作用
  6. environments的配置
  7. databaseIdProvider的用法
  8. 如何有效引入映射器
  • mybatis配置文件及配置项顺序

    • <configuration><!--配置-->
      	<properties/><!--属性-->
        <settings/><!--设置-->
        <typeAliases/><!--类型命名-->
        <typeHandlers/><!--类型处理器-->
        <objectFactory/><!--对象工厂-->
        <plugins/><!--插件-->
        <environments/><!--配置环境-->
        	<environment><!--环境变量-->
            <transactionManager/><!--事务管理器-->
            <dataSource/><!--数据源-->
        	</environment>
      </configuration>
      
    • MyBatis 配置项的顺序不能颠倒

  • Properties 属性

    • 作用:配置运行参数

    • 使用方式:property子元素、properties文件、程序代码传递

    • Property

      • <!--运行参数配置-->
        <properties>
        	<property name="database.driver" value="com.mysql.jdbc.Driver"/>
        </properties>
        <!--数据库环境配置-->
        <dataSource type="POOLED">
        	<property name="driver" value="${database.driver}"/>
        </dataSource>
        
    • properties

      • <!--配置文件(文件名:jdbc.properties)-->
        database.driver=com.mysql.jdbc.Driver
        <!--数据库环境配置-->
        <properties resource="jdbc.properties"/>
        
    • 程序代码传递(从配置文件中读取密文解密后替换重新读取)

      • String resource="mybatis-config.xml";
        InputStream inputStream;
        InputStream in=Resources.getResourceAsStream("jdbc.properties");;
        Properties props=new Properties();
        props.load(in);
        String driver=props.getProperty("database.driver");
        //解密并重置属性
        props.put("database.diver",decode(driver));
        inputStream=Resources.getResourceAsStream(resource);
        //将props中属性值传入配置文件,并生成sqlsessionfactory
        SqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,props);
        
  • Settings 常用配置项

    • cacheEnabled
      • 该配置影响所有映射器中配置缓存的全局开关
        • true|false(default:true)
    • lazyLoadingEnabled
      • 延迟加载的全局开关,特定关联关系中可被fetchType属性覆盖
        • true|false(default:false)
    • aggressiveLazyLoading
      • 控制任意延迟属性的调用会带有延迟加载属性的对象的完整加载或按需加载
        • true|false(default:false)
    • autoMappingBehavior
      • 指定自动映射
        • NONE:取消自动映射|PARPTAL:自动映射,只对没有嵌套结果集进行自动映射|FULL:自动映射任意复杂结果集
    • mapUnderscoreToCameICase
      • 是否开启驼峰命名规则
        • true|false(default:false)
  • typeAliases

    • 自定义别名

      • <typeAliases>
        	<typeAlias alias="role" type="xxx.xxx.role"/>
        </typeAliases>
        
    • 按包定义别名

      • <typeAliases>
        	<typeAlias name="xxx.xxx"/>
        </typeAliases>
        
      • mybatis将扫描这个包里面的类,将其第一个字母变为小写作为别名

  • typeHandler类型转换器

    • 作用:承担javaType和jdbcType之间的相互转换
  • ObjectFactory(对象工厂)

    • 作用:resultType及resultMap结果集实例创建
  • environments

    • 作用:配置数据库信息

    • 组成:事务管理器、数据源

      • 事务管理器(transactionManager)

        • 配置:

          • <transactionManager type="JDBC"/>
            
        • JDBC:以jdbc方式对数据库提交和回滚进行操作

        • Managed:提交和回滚不用任何操作,把事务管理交给容器处理。默认自动关闭连接

      • 数据源环境(environment)

        • 配置:

          • <dataSource type="UNPOOLED"
            
          • 数据源介绍:UNPOOLED|POOLED|JNDI

            • UNPOOLED:采用非数据库池的管理方式,每次请求都会打开新的数据库连接,创建慢
            • POOLED:连接池的方式
            • JNDI:常在EJB或应用服务器容器中使用
  • databaseIdProvider(数据库厂商标识)

    • 作用:配置使用何种数据库运行sql

    • 配置:

      • <!--mybatis配置文件-->
        <databaseIdProvider>
        	<property name="Oracle" value="oracle"/>
          <property name="MySQL" value="mysql"/>
          <property name="DB2" value="db2"/>
        </databaseIdProvider>
        <!--映射器配置文件-->
        <select databaseId="oracle">
        	select * from xxx
        </select>
        
  • 引入映射器

    • 用文件路径引入映射器

      • <mappers>
        	<mapper resource="xxx/mapper/xxx.xml">
        </mappers>
        
    • 用包名引入映射器

      • <mappers>
        	<package name="xxx.xxx.mapper"/>
        </mappers>
        
    • 用类注册引入映射器

      • <mappers>
        	<mapper class="xxx.mapper.xxxmapper"/>
        </mappers>
        
    • 用映射器配置文件完整路径引入映射器

      • <mappers>
        	<mapper url="file://"/>
        </mappers>
        
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值