properties: 属性配置
settings: 设置
typeAliases: 类型命名
typeHandlers: 类型处理器
objectFactory: 对象工厂
plugins: 插件
environments: 环境
enviroment: 环境变量
transactionManager: 事务管理器
dataSource: 数据源
databaseIdProvider: 数据库厂商标识
mappers: 映射器
1,properties: 引入外部 properties 配置文件的内容
2,settings: 设置项, 可以影响 mybatis 运行的行为
3,typeAliases: 为 java 类型取一个别名:
- <typeAliases>
- <typeAlias alias="emp" type="com.gong.mybatis.bean.Employee"/>
- <!-- 批量取别名, 会为某个包下的所有类取别名, 别名为类名小写 -->
- <package name="com.gong.mybatis.bean"/>
- </typeAliases>
另外, 我们也可以用 @Alias("名字") 注解, 直接在 java 文件中为类取别名. 注意: 别名不区分大小写.
4,typeHandlers:java 数据类型和数据库数据类型的桥梁.
5,plugins: 拦截 sql 语句执行的一些步骤.
- 6,environments:
- <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>
我们可以通过 id 来切换不同的环境.
7,databaseIdProvider: 支持多数据库, 根据数据库厂商的标识发送不同的 sql 语句.
8,mappers: 将 sql 映射文件注册到全局配置文件中.
mappers 中的子标签为 mapper, 在 mapper 中有以下属性:
url: 引用网络路径或者磁盘路径
resource: 引用类路径下的 sql 映射文件
假设在类路径下有 mabtis.mapper 专门放置 mapper.xml, 那么可以这么使用 resource
- <mappers>
- <mapper resource="mybatis/mapper/EmployeeMapper.xml"/>
- </mappers>
class: 注册接口. 使用这种方式时:
(1) 若有映射文件, 需要将 mapper.xml 文件放置在和接口的同一目录下, 同时接口名和 mapper 文件名要保持一致:
- <mappers>
- <mapper class="com.gong.mybatis.dao.EmployeeMapper" />
- </mappers>
(2) 若没有映射文件, 需要将 sql 语句利用注解写在接口的方法中:
- <mappers>
- <mapper class="com.gong.mybatis.dao.EmployeeMapperAnnotation" />
- </mappers>
EmployeeMapperAnnotation.java
- package com.gong.mybatis.dao;
- import org.apache.ibatis.annotations.Select;
- import com.gong.mybatis.bean.Employee;
- public interface EmployeeMapperAnnotation {
- @Select("select * from tbl_employee where id=#{id}")
- public Employee getEmpById(Integer id);
- }
在 TestMybatis.java 中进行测试:
- package com.gong.mybatis.test;
- import java.io.IOException;
- import java.io.InputStream;
- import org.apache.ibatis.io.Resources;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
- import org.junit.Test;
- import com.gong.mybatis.bean.Employee;
- import com.gong.mybatis.dao.EmployeeMapper;
- import com.gong.mybatis.dao.EmployeeMapperAnnotation;
- public class TestMybatis {
- public SqlSessionFactory getSqlSessionFactory() throws IOException {
- String resource = "mybatis-config.xml";
- InputStream is = Resources.getResourceAsStream(resource);
- return new SqlSessionFactoryBuilder().build(is);
- }
- @Test
- public void test02() throws IOException {
- // 1, 获取 sqlSessionFactory 对象
- SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
- // 2, 获取 sqlSession 对象
- SqlSession openSession = sqlSessionFactory.openSession();
- try {
- // 3, 获取接口的实现类对象
- // 会为接口自动的创建一个代理对象, 代理对象去执行增删改查方法
- EmployeeMapperAnnotation mapper = openSession.getMapper(EmployeeMapperAnnotation.class);
- Employee employee = mapper.getEmpById(1);
- System.out.println(mapper.getClass());
- System.out.println(employee);
- } finally {
- openSession.close();
- }
- }
- }
输出:
来源: http://www.bubuko.com/infodetail-3386593.html