MyBaties的基本配置标签
1- 全局配置文件(xxx.properties)引入的两种方式
resource:引入类路径下的资源 url:引入网络路径或磁盘路径下的资源
1 |
<properties resource= "dbconfig.properties" ></properties> |
2- settings包含设置项
name:配置项
value:属性值
1 2 3 |
<settings> <setting name= "mapUnderscoreToCamelCase" value= "true" /> </settings> |
3- typeAliases:别名处理器,为java类型起别名
type:指定要起别名的类型全类名;默认别名就是类名小写
alias:指定新的别名
1 |
<typeAlias type= "com.atguigu.mybatis.bean.Employee" alias= "emp" /> |
3.1 为某个包下所有类起别名
package:为某个包下的所有类批量起别名
name:指定包名(为当前包以及下面所有的后代包的每一个类都起一个默认别名(类名小写)
1 |
< package name= "com.atguigu.mybatis.bean" /> |
3.2 使用注解 @Alias 为某个类指定新的类型
1 2 3 4 |
@Alias ( "emp" ) public class Employee { ...code... } |
4-配置多种MyBatis环境
enviroments:配置的环境们都写在里面,default指定这个环境的名称 environment:配置一个具体的环境信息,有id唯一标识与transactionManager事务管理器 id:唯一标识 transactionManager:事务管理器,它的有属性type type:事务管理器的类型JDBC MANAGED 自定义事务管理器 dataSource:数据源 type:数据源类型 UNPOOLED POOLED JNDI 自定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<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> |
5-databaseIdProvider:支持多数据库
databaseIdProvider:支持多数据库,它的type为DB_VENDOR作用就是得到数据库厂商的标识(驱动getDatabaseProductName()),mybatis就能根据数据库厂商标识来执行不同的sql; property:为数据库起名字 name: value:
1 2 3 4 5 6 |
<databaseIdProvider type= "DB_VENDOR" > <!-- 为不同的数据库厂商起别名 --> <property name= "MySQL" value= "mysql" /> <property name= "Oracle" value= "oracle" /> <property name= "SQL Server" value= "sqlserver" /> </databaseIdProvider> |
最终,在mapper.xml中写入查询时的的语句,并申明使用到的数据库是什么
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<mapper namespace= "com.atguigu.mybatis.dao.EmployeeMapper" > <select id= "getEmpById" resultType= "com.atguigu.mybatis.bean.Employee" > select * from tbl_employee where id = #{id} </select> <select id= "getEmpById" resultType= "com.atguigu.mybatis.bean.Employee" databaseId= "mysql" > select * from tbl_employee where id = #{id} </select> <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> </mapper> |
6-mappers将sql文件注册进入全局配置文件
6.1注册配置文件: resource:引用类路径下的sql映射文件例如:mybatis/mapper/EmployeeMapper.xml url:引用网路路径或者磁盘路径下的sql映射文件例如:file:///var/mappers/AuthorMapper.xml 6.2注册接口:有sql映射文件,映射文件名必须和接口同名,并且放在与接口同一目录下;
1 |
<mapper resource= "mybatis/mapper/EmployeeMapper.xml" /> |
没有sql映射文件,所有的sql都是利用注解写在接口上,然后再mappers中进行注册;
1 2 3 4 |
public interface EmployeeMapperAnnotation { @Select ( "select * from tbl_employee where id=#{id}" ) public Employee getEmpById(Integer id); } |
1 |
<mapper class = "com.atguigu.mybatis.dao.EmployeeMapperAnnotation" /> |
6.3批量注册:
本质上,如果包名相同,不管是src内还是src外的文件,实际存储过程中会被存储到同一个文件夹中
1 |
< package name= "com.atguigu.mybatis.dao" /> |
到此这篇关于关于MyBaties的基本配置标签总结的文章就介绍到这了,更多相关MyBaties的基本配置标签内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/Wang_Pro/article/details/118116945
查看更多关于关于MyBaties的基本配置标签总结的详细内容...