二、mybatis全局配置文件说明
中文文档地址:https://mybatis.org/mybatis-3/zh/configuration.html
开始前的准备:
本章实列为 mybatis的简介和helloworld 中helloworld 的实例;
测试可以用helloworld 中test包中的方法测试;
配置顺序,properties settings typeAliases typeHandlers objectFactory objectWrapperFactory reflectFactory plugings environment databaseIdProvider mappers
1.properties:引入外部properties配置文件的内容:
属性:
- resource:引入类路径下的资源
- url:引入网络路径或者磁盘路径下的资源
<properties resource="dbconfig.properties"></properties>
测试
外部文件dbconfig.peoperties
dbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis jdbc.username=root jdbc.password=root
全局文件mybatis-config.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!-- 7 1、mybatis可以使用properties来引入外部properties配置文件的内容; 8 resource:引入类路径下的资源 9 url:引入网络路径或者磁盘路径下的资源 10 --> 11 <properties resource="dbconfig.properties"></properties> 12 13 <environments default="development"> 14 <environment id="development"> 15 <transactionManager type="JDBC"/> 16 <dataSource type="POOLED"> 17 <property name="driver" value="${jdbc.driver}" /> 18 <property name="url" value="${jdbc.url}" /> 19 <property name="username" value="${jdbc.username}" /> 20 <property name="password" value="${jdbc.password}" /> 21 </dataSource> 22 </environment> 23 </environments> 24 <!-- 将我们写好的SQL映射文件一定注册到全局配置文件中 --> 25 <mappers> 26 <mapper resource="EmployeeMapper.xml"/> 27 </mappers> 28 </configuration>
运行测试类:结果Employee [id=0, lastName=null, email=1602211058@qq.com, gender=女];
2、settings包含很多重要的设置项:
setting:用来设置每一个设置项
属性:
- name:设置项名
- value:设置项取值
<settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
这里的 mapUnderscoreToCamelCase为是否开启驼峰命名自动映射,即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn,默认值为false .。
测试:
全局文件mybatis-config.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!-- 7 1、mybatis可以使用properties来引入外部properties配置文件的内容; 8 resource:引入类路径下的资源 9 url:引入网络路径或者磁盘路径下的资源 10 --> 11 <properties resource="dbconfig.properties"></properties> 12 <!-- 13 2、settings包含很多重要的设置项 14 setting:用来设置每一个设置项 15 name:设置项名 16 value:设置项取值 17 --> 18 <settings> 19 <setting name="mapUnderscoreToCamelCase" value="true"/> 20 </settings> 21 <environments default="development"> 22 <environment id="development"> 23 <transactionManager type="JDBC"/> 24 <dataSource type="POOLED"> 25 <property name="driver" value="${jdbc.driver}" /> 26 <property name="url" value="${jdbc.url}" /> 27 <property name="username" value="${jdbc.username}" /> 28 <property name="password" value="${jdbc.password}" /> 29 </dataSource> 30 </environment> 31 </environments> 32 <!-- 将我们写好的SQL映射文件一定注册到全局配置文件中 --> 33 <mappers> 34 <mapper resource="EmployeeMapper.xml"/> 35 </mappers> 36 </configuration>
修改EmployeeMapper.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="aaa"> 6 <!-- 7 namespace:命名空间; 8 id:唯一标识; 9 resultType:返回值类型; 10 #{id}:从传递过来的参数中去除ID值 11 --> 12 <select id="getById" resultType="entity.Employee"> 13 select LAST_NAME 14 ,gender as gender,email as email from 15 tbl_employee where id =#{id} 16 </select> 17 </mapper>
运行测试类,结果为:Employee [id=0, lastName=null, email=1602211058@qq.com, gender=女]
3、typeAliases:别名处理器;(较少使用)
sql 映射文件的返回类型 需要添加 全类名,比较长;可以使用别名;typeAliases 别名处理器 可以为我们的java类型起别名,别名不区分大小写;别名处理器可以分两种:
- typeAlias:为某个java类型起别名
- package:为某个包下的所有类批量起别名
typeAlias属性:
- type:指定要起别名的类型全类名;默认别名就是类名小写;employee
- alias:指定新的别名
<typeAlias type="com.atguigu.mybatis.bean.Employee" alias="emp"/>
package属性:
name:指定包名(为当前包以及下面所有的后代包的每一个类都起一个默认别名(类名小写),)
<package name="com.atguigu.mybatis.bean"/>
typeAlias测试:
全局文件mybatis-config.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!-- 7 1、mybatis可以使用properties来引入外部properties配置文件的内容; 8 resource:引入类路径下的资源 9 url:引入网络路径或者磁盘路径下的资源 10 --> 11 <properties resource="dbconfig.properties"></properties> 12 <!-- 13 2、settings包含很多重要的设置项 14 setting:用来设置每一个设置项 15 name:设置项名 16 value:设置项取值 17 --> 18 <settings> 19 <setting name="mapUnderscoreToCamelCase" value="true"/> 20 </settings> 21 <!-- 3、typeAliases:别名处理器:可以为我们的java类型起别名 22 别名不区分大小写 23 --> 24 <typeAliases> 25 <typeAlias type="entity.Employee" alias="emp"/> 26 </typeAliases> 27 <environments default="development"> 28 <environment id="development"> 29 <transactionManager type="JDBC"/> 30 <dataSource type="POOLED"> 31 <property name="driver" value="${jdbc.driver}" /> 32 <property name="url" value="${jdbc.url}" /> 33 <property name="username" value="${jdbc.username}" /> 34 <property name="password" value="${jdbc.password}" /> 35 </dataSource> 36 </environment> 37 </environments> 38 <!-- 将我们写好的SQL映射文件一定注册到全局配置文件中 --> 39 <mappers> 40 <mapper resource="EmployeeMapper.xml"/> 41 </mappers> 42 </configuration>
修改EmployeeMapper.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="aaa"> 6 <!-- 7 namespace:命名空间; 8 id:唯一标识; 9 resultType:返回值类型; 10 #{id}:从传递过来的参数中去除ID值 11 --> 12 <select id="getById" resultType="emp"> 13 select LAST_NAME 14 ,gender as gender,email as email from 15 tbl_employee where id =#{id} 16 </select> 17 </mapper>
测试结果:Employee [id=0, lastName=JoyLi, email=1602211058@qq.com, gender=女]
4、environments:环境们,mybatis可以配置多种环境
default指定使用某种环境。可以达到快速切换环境。
environment:配置一个具体的环境信息;id代表当前环境的唯一标识;必须有两个标签:transactionManager(事务管理器)、dataSource(数据源):
transactionManager:事务管理器;
type:事务管理器的类型;JDBC(JdbcTransactionFactory)、MANAGED(ManagedTransactionFactory)、自定义
自定义事务管理器:实现TransactionFactory接口.type指定为全类名
dataSource:数据源;
type:数据源类型; 数据源类型包括有 UNPOOLED(UnpooledDataSourceFactory)、POOLED(PooledDataSourceFactory)、JNDI(JndiDataSourceFactory)、自定义;
自定义数据源:实现DataSourceFactory接口,type是全类名
1 <environments default="dev_mysql"> 2 <environment id="dev_mysql"> 3 <transactionManager type="JDBC"></transactionManager> 4 <dataSource type="POOLED"> 5 <property name="driver" value="${jdbc.driver}" /> 6 <property name="url" value="${jdbc.url}" /> 7 <property name="username" value="${jdbc.username}" /> 8 <property name="password" value="${jdbc.password}" /> 9 </dataSource> 10 </environment> 11 12 <environment id="dev_oracle"> 13 <transactionManager type="JDBC" /> 14 <dataSource type="POOLED"> 15 <property name="driver" value="${orcl.driver}" /> 16 <property name="url" value="${orcl.url}" /> 17 <property name="username" value="${orcl.username}" /> 18 <property name="password" value="${orcl.password}" /> 19 </dataSource> 20 </environment> 21 </environments>
测试:
全局文件mybatis-config.xml:
<?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"> <configuration> <properties resource="dbconfig.properties"></properties> <environments default="dev_mysql"> <environment id="dev_mysql"> <transactionManager type="JDBC"></transactionManager> <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> <environment id="dev_oracle"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${orcl.driver}" /> <property name="url" value="${orcl.url}" /> <property name="username" value="${orcl.username}" /> <property name="password" value="${orcl.password}" /> </dataSource> </environment> </environments> <!-- 将我们写好的SQL映射文件一定哟啊注册到全局配置文件中 --> <mappers> <mapper resource="EmployeeMapper.xml"/> </mappers> </configuration>
修改EmployeeMapper.xml:(这里默认就是访问dev_mysql,如果要切换则更改mybatis-config.xml中的environments default="dev_mysql“中的default 实现快速切换环境)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="aaa"> 6 <!-- 7 namespace:命名空间;指定为接口的全类名 8 id:唯一标识; 9 resultType:返回值类型; 10 #{id}:从传递过来的参数中去除ID值 11 --> 12 <select id="getById" resultType="entity.Employee"> 13 select LAST_NAME AS 14 lastName,gender as gender,email as email from 15 tbl_employee where id =#{id} 16 </select> 17 </mapper>
外部文件dbconfig.peoperties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis jdbc.username=root jdbc.password= orcl.driver=oracle.jdbc.OracleDriver orcl.url=jdbc:oracle:thin:@localhost:1521:orcl orcl.username=scott orcl.password=123456
测试结果:Employee [id=null, lastName=joy2x2, email=joy52112225@iclound.com, gender=女];(结果可能跟上面结果不同,因为不同笔记本的数据库,所以数据有些许不同)
5.databaseIdProvider:支持多数据库厂商;
type="DB_VENDOR":vendordatabaseidprovider
作用就是得到数据库厂商的标识(驱动getDatabaseProductName())
MySQL,Oracle,SQL Server
<databaseIdProvider type="DB_VENDOR"> <!-- 为不同的数据库厂商起别名 --> <property name="MySQL" value="mysql"/> <property name="Oracle" value="oracle"/> <property name="SQL Server" value="sqlserver"/> </databaseIdProvider>
执行SQL语句时可以选择oracle的数据库:
<select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee" databaseId="oracle"> select EMPLOYEE_ID id,LAST_NAME lastName,EMAIL email from employees where EMPLOYEE_ID=#{id} </select>
测试结果:Employee [id=null, lastName=joy2x2, email=joy52112225@iclound.com, gender=女];(结果可能跟上面结果不同,因为不同笔记本的数据库,所以数据有些许不同)
6、mappers:将sql映射注册到全局配置中
mapper:注册单个
- 注册配置文件
resource:引用类路径下的SQL映射文件,例如:EmployeeMapper.xml,前面的例子都是这种注册方式,这里不再做例子;
<mapper resource="EmployeeMapper.xml"/>
url:引用网络路径下或者磁盘路径下的SQL映射文件;例如:file:///var/mappers/AutorMapper.xml
- class:引用注册接口:
1.有SQL映射文件,映射文件名必须接口同名,并且放在与接口同一目录下;
<mapper class="dao.EmployeeMapper"/>
2.没有SQL映射文件,所有的SQL都是利于注解写在接口上;
推荐:比较重要的、复杂的DAO接口我们来写SQL映射文件
不重要、简单的Dao接口为了开发快速可以使用注解
批量注册:
批量注册 有SQL映射文件,映射文件名必须接口同名,并且放在与接口同一目录下;
<package name="dao"/>
class:引用注册接口测试:
全局文件mybatis-config.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <properties resource="dbconfig.properties"></properties> 7 <environments default="dev_mysql"> 8 <environment id="dev_mysql"> 9 <transactionManager type="JDBC"></transactionManager> 10 <dataSource type="POOLED"> 11 <property name="driver" value="${jdbc.driver}" /> 12 <property name="url" value="${jdbc.url}" /> 13 <property name="username" value="${jdbc.username}" /> 14 <property name="password" value="${jdbc.password}" /> 15 </dataSource> 16 </environment> 17 18 <environment id="dev_oracle"> 19 <transactionManager type="JDBC" /> 20 <dataSource type="POOLED"> 21 <property name="driver" value="${orcl.driver}" /> 22 <property name="url" value="${orcl.url}" /> 23 <property name="username" value="${orcl.username}" /> 24 <property name="password" value="${orcl.password}" /> 25 </dataSource> 26 </environment> 27 </environments> 28 <!-- 将我们写好的SQL映射文件一定哟啊注册到全局配置文件中 --> 29 <mappers> 30 <mapper class="dao.EmployeeMapper"/> 31 </mappers> 32 </configuration>
目录结构调整:为了让EmployeeMapper.xml 和EmployeeMapper.java在同一个目录下做出如下调整;
修改EmployeeMapper.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="dao.EmployeeMapper"> 6 <!-- 7 namespace:命名空间;指定为接口的全类名 8 id:唯一标识; 9 resultType:返回值类型; 10 #{id}:从传递过来的参数中去除ID值 11 --> 12 <select id="getById" resultType="entity.Employee"> 13 select LAST_NAME AS 14 lastName,gender as gender,email as email from 15 tbl_employee where id =#{id} 16 </select> 17 </mapper>
使用接口的测试方法测试;
1 @Test 2 public void test01() throws IOException { 3 String resource = "mybatis-config.xml"; 4 InputStream inputStream = Resources.getResourceAsStream(resource); 5 // 1.获取SqlSessionFactory对象 6 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 7 // 获取SqlSessionFactory 实例,能直接运行已经执行的sql语句 8 SqlSession openSession = sqlSessionFactory.openSession(); 9 try { 10 // 3.获取接口的实现类对象,当接口和xml文件动态绑定时,MyBatis会为接口自动创建一个代理对象,通过代理对象去调用相应的方法 11 EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); 12 System.out.println(mapper.getById(1)); 13 } finally { 14 openSession.close(); 15 } 16 17 }
测试结果:
Employee [id=null, lastName=joy2x2, email=joy52112225@iclound.com, gender=女]
批量注册测试:
与上面的classy引入接口测试文件类似,全局文件mybatis-config.xml把 <mapper class="dao.EmployeeMapper"/> 换成 <package name="dao"/>即可;
全局文件mybatis-config.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <properties resource="dbconfig.properties"></properties> 7 <environments default="dev_mysql"> 8 <environment id="dev_mysql"> 9 <transactionManager type="JDBC"></transactionManager> 10 <dataSource type="POOLED"> 11 <property name="driver" value="${jdbc.driver}" /> 12 <property name="url" value="${jdbc.url}" /> 13 <property name="username" value="${jdbc.username}" /> 14 <property name="password" value="${jdbc.password}" /> 15 </dataSource> 16 </environment> 17 18 <environment id="dev_oracle"> 19 <transactionManager type="JDBC" /> 20 <dataSource type="POOLED"> 21 <property name="driver" value="${orcl.driver}" /> 22 <property name="url" value="${orcl.url}" /> 23 <property name="username" value="${orcl.username}" /> 24 <property name="password" value="${orcl.password}" /> 25 </dataSource> 26 </environment> 27 </environments> 28 <!-- 将我们写好的SQL映射文件一定哟啊注册到全局配置文件中 --> 29 <mappers> 30 <package name="dao"/> 31 </mappers> 32 </configuration>
测试结果和上面的一样;
为了在视觉效果上 区分 src的dao中的 mapper接口和xml文件,可以在config文件目录(不是包 )中建立与mapper接口相同名称的包;因为编译后的文件,同包的文件在同一目录,不会区分src和config