MyBatis全局配置文件 1 引入dtd约束
文档类型定义(DTD)可定义合法的XML文档构建模块。它使用一系列合法的元素来定义文档的结构,DTD 可被成行地声明于 XML 文档中,也可作为一个外部引用
在MyBatis全局配置文件mybatis-config.xml的开头,
1 2 3 4 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
引入了MyBatis的文档类型定义文件,这样在IDE中编辑就可以出现xml标签和属性的自动提示
1 2 3 4 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
2 引入外部配置文件
可以使用properties标签引入外部配置文件,
1 2 3 4 5 jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis jdbc.username=root jdbc.password=123456 <properties resource="dbsource.properties"></properties>
properties标签可以用两种方式表示外部配置文件的地址,
属性
含义
resource
从本地项目类路径下获取外部配置文件
url
从网络url远程获取外部配置文件
3 运行时行为设置
可以通过settings标签配置MyBatis的运行时行为设置项,
1 2 3 <settings > <setting name ="mapUnderscoreToCamelCase" value ="true" /> </settings >
在这个例子中,将运行时行为mapUnderscoreToCamelCase的值设置为true,MyBatis就会将数据库中下划线表示的字段自动映射到Java类中驼峰命名的属性上,即从经典数据库列名A_COLUMN映射到经典Java属性名aColumn
4 类型别名 4.1 typeAlias
可以使用typeAlias标签为Java类起别名,
1 2 3 <typeAliases > <typeAlias type ="com.lnhoo.Employee" alias ="emp" > </typeAlias > </typeAliases >
相应的,在Mapper配置文件中也需要修改resultType,
1 2 3 4 5 <mapper namespace ="com.lnhoo.dao.EmployeeMapper" > <select id ="getEmployeeById" resultType ="emp" > select * from employees where id = #{id} </select > </mapper >
4.2 package标签
在typeAliases标签内部,可以通过package标签批量为包下的所有类起别名,
1 2 3 <typeAliases > <package name ="com.lnhoo" /> </typeAliases >
此时类的别名是类名的小写形式,比如Employee类的别名就是employee
4.3 @Alias注解
在使用package标签批量起别名的时候,可能会造成名字冲突,此时可以在Java类上面应用@Alias注解起别名,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 @Alias("emp") public class Employee { private Integer id; private String lastName; private char gender; private String email; public void setId (Integer id) { this .id = id; } public void setLastName (String lastName) { this .lastName = lastName; } public void setGender (char gender) { this .gender = gender; } public void setEmail (String email) { this .email = email; } @Override public String toString () { return "Employee{" + "id=" + id + ", lastName='" + lastName + '\'' + ", gender=" + gender + ", email='" + email + '\'' + '}' ; } }
5 类型处理器
MyBatis 在设置预处理语句(PreparedStatement)中的参数或从结果集中取出一个值时, 都会用类型处理器将获取到的值以合适的方式转换成Java类型
6 插件
MyBatis允许你在映射语句执行过程中的某一点进行拦截调用。默认情况下,MyBatis允许使用插件来拦截的方法调用包括:
(1)Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
(2)ParameterHandler (getParameterObject, setParameters)
(3)ResultSetHandler (handleResultSets, handleOutputParameters)
(4)StatementHandler (prepare, parameterize, batch, update, query)
7 运行环境
可以在environments标签内部通过environment标签配置多种运行环境,
1 2 3 4 5 6 7 8 9 10 11 <environments default ="development" > <environment id ="development" > <transactionManager type ="JDBC" /> <dataSource type ="POOLED" > <property name ="driver" value ="${jdbc.driver}" /> <property name ="url" value ="${jdbc.url}" /> <property name ="username" value ="${jdbc.username}" /> <property name ="password" value ="${jdbc.password}" /> </dataSource > </environment > </environments >
大多数情况下,开发环境、测试环境的数据源是相互隔离的,所以需要两份配置
环境必须提供事务管理器和数据源配置信息
可以通过设置environments标签的default属性在多个环境之间快速切换
使用dataSource标签配置数据源,内置的数据源包括:POOLED(带连接池)、UNPOOLED(不带连接池)、JNDI
自定义数据源:实现DataSourceFactory接口,在dataSource标签的type属性填上全类名
8 多数据库支持
MyBatis可以根据不同的数据库厂商执行不同的语句,这种多厂商的支持是基于映射语句中的databaseId属性。 MyBatis会加载带有匹配当前数据库databaseId属性和所有不带databaseId属性的语句, 如果同时找到带有databaseId和不带databaseId的相同语句,则后者会被舍弃
1 2 3 4 5 <databaseIdProvider type ="DB_VENDOR" > <property name ="MySQL" value ="mysql" /> <property name ="Oracle" value ="oracle" /> <property name ="SQL Server" value ="sqlserver" /> </databaseIdProvider >
1 2 3 4 5 6 7 8 9 10 11 <mapper namespace ="com.lnhoo.dao.EmployeeMapper" > <select id ="getEmployeeById" resultType ="emp" > select * from employees where id = #{id} </select > <select id ="getEmployeeById" resultType ="emp" databaseId ="mysql" > select * from employees where id = #{id} </select > <select id ="getEmployeeById" resultType ="emp" databaseId ="oracle" > select id, last_name, email from tbl_employees where id = #{id} </select > </mapper >
使用databaseId属性指定SQL语句执行所在的数据库
9 SQL映射注册 9.1 注册配置文件 1 2 3 <mappers > <mapper resource ="EmployeeMapper.xml" /> </mappers >
使用mapper标签在MyBatis全局配置文件中注册SQL映射配置文件
resource属性从类路径加载xml配置文件,如果是url属性,则从网络或磁盘位置加载xml配置文件
9.2 注册接口
使用注解方式实现Mapper接口中方法和预执行的SQL语句的绑定,
1 2 3 4 public interface EmployeeMapperAnnotation { @Select(value = "select * from employees where id=#{id}") Employee getEmployeeById (Integer id) ; }
在MyBatis全局配置文件中引入SQL映射接口,
1 2 3 <mappers > <mapper class ="com.lnhoo.dao.EmployeeMapperAnnotation" > </mapper > </mappers >