Mybatis 全局配置文件

1 全局配置文件

在编译器内配置xml约束文件
标签是有顺序的

1.1 properties标签

引入外部properties配置文件

  • <properties resource=""> 引用类路径下的资源
  • <properties url=""> 引入网络路径或磁盘路径

1.2 settings标签

影响mybatis运行的设置项

  • <setting name="cacheEnabled" value="true"/> 缓存全局开关,默认true
  • <setting name="defaultStatementTimeout" value="10" /> 设置超时时间,默认null
  • <setting name="mapUnderscoreToCamelCase" value="true" /> 是否开启驼峰命名规则映射,默认false

1.3 typeAliases标签

设置java类型别名

<typeAliases>
  <typeAlias alias="Author" type="domain.blog.Author"/>
  <typeAlias alias="Blog" type="domain.blog.Blog"/>
  ...
</typeAliases>

设置扫描包下的bean,默认类名小写

<typeAliases>
  <package name="domain.blog"/>
</typeAliases>

使用@Alias注解 指定别名

@Alias("author")
public class Author {
    ...
}

1.4 typeHandlers标签

处理java类型 对应 数据库类型

1.5 plugins标签

插件
MyBatis允许您在映射语句的执行过程中的某些点截取对的调用。默认情况下,MyBatis允许插件拦截以下方法调用:

  • Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
  • ParameterHandler (getParameterObject, setParameters)
  • ResultSetHandler (handleResultSets, handleOutputParameters)
  • StatementHandler (prepare, parameterize, batch, update, query)

1.6 environments标签

mybatis可以配置多种环境

<environments default="development_002">
    <environment id="development_001">
    </environment>
    <environment id="development_002">
    </environment>
</environments>

每个environment是一个具体环境,且每个environment必须有dataSource和transactionManager标签
<transactionManager type=""/> 事务管理器

  • type=“JDBC” 以JDBC方式提交回滚事务控制
  • type=“MANAGED” 以JEE的方式控制事物
org.apache.ibatis.session.Configuration 中配置了很多别名
JDBC,MANAGED就是别名
...
public Configuration() {
    typeAliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class);
    typeAliasRegistry.registerAlias("MANAGED", ManagedTransactionFactory.class);
    ...
}
  • 自定义事物管理器,实现TrancsactionFactory接口,type=“全类名”

与Spring整合后会用Spring控制事物

<dataSource type=""> 数据源

  • UNPOOLED 不使用连接池
  • POOLED 使用连接池
  • JNDI
  • 自定义数据源
public interface DataSourceFactory {
  void setProperties(Properties props);
  DataSource getDataSource();
}
实现DataSourceFactory 接口,type="全类名"

与Spring整合后会用Spring控制

1.7 databaseIdProvider标签

支持多数据库厂商

<databaseIdProvider type="DB_VENDOR" /> 得到数据库厂商的标识,mybatis根据数据库厂商标识

<!-- 起别名 -->
<databaseIdProvider type="DB_VENDOR">
  <property name="SQL Server" value="sqlserver"/>
  <property name="DB2" value="db2"/>
  <property name="Oracle" value="oracle" />
</databaseIdProvider>
...
<select id="selectBlog" resultType="Blog" databaseId="mysql">
  select * from Blog where id = #{id}
</select>
...

如果有两个selectBlog同名方法,优先执行配置了当前数据库标识的语句

1.8 mappers标签

<!-- Using classpath relative resources 引用类路径下的sql映射文件 -->
<mappers>
  <mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
</mappers>
<!-- Using url fully qualified paths 远程路径或文件路径 -->
<mappers>
  <mapper url="file:///var/mappers/AuthorMapper.xml"/>
</mappers>
<!-- Using mapper interface classes 引用(注册)接口 -->
<!-- 接口必须和xml映射文件在同一目录下,且要同名 -->
<!-- 或者是注解版的 -->
<mappers>
  <mapper class="org.mybatis.builder.AuthorMapper"/>
</mappers>
public interface EmployeeMapperAnnotation {
	@Select("select * from employee")
	public Employee getEmployeeById(Integer id);
}
.......xml.........
<mappers>
  <mapper class="xxx.xxx.xxx.EmployeeMapperAnnotation "/>
</mappers>
<!-- Register all interfaces in a package as mappers -->
<!-- 接口必须和xml映射文件在同一目录下,且要同名 -->
<!-- 或者是注解版的 -->
<mappers>
  <package name="org.mybatis.builder"/>
</mappers>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值