配置解析
1.核心配置文件
2.环境配置(environments)
mybatis可以适应多种环境,但是要记住:尽管可以配置多个环境,但是每个SqlSessionFactory实例只能选择一种环境。
mybatis的默认的事务管理器就是jdbc,连接池:POOLED
3.属性(properties)
我们可以通过properties属性来实现引用配置文件
这些属性都是可外部配置且动态替换的,既可以在典型的java属性文件中配置,亦可以通过properties元素的子元素来传递。
编写一个配置文件db.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=lxk123456
在核心配置文件中引入(注意:在xml中,所有的标签都可以规定其顺序)

4.起别名

在实体类多的时候,
使用第二种别名,注解方式


其他配置
映射器(mappers)
MapperRegistry:注册绑定我们的mapper文件;
方式一:(推荐使用)
<!-- 每一个mapper.xml都需要在mybatis核心配置中注册-->
<mappers>
<mapper resource="com/xxxx/mybatis/dao/EmpMapper.xml"></mapper>
</mappers>
方式二:使用class的方式绑定注册
<mappers>
<mapper class="com.xxxx.mybatis.dao.EmpDao" />
</mappers>
注意点:
接口和他的Mapper配置文件必须同名
接口和他的配置文件必须在同一个包下
方式三:使用扫描包进行注入绑定
<mappers>
<package name="com.xxxx.mybatis.dao"/>
</mappers>
接口和他的Mapper配置文件必须同名
接口和他的配置文件必须在同一个包下
网友评论