MyBaties的基本配置標(biāo)簽
1-全局配置文件(xxx.properties)引入的兩種方式
- resource:引入類路徑下的資源
- url:引入網(wǎng)絡(luò)路徑或磁盤路徑下的資源
1
|
<properties resource= "dbconfig.properties" ></properties> |
2-settings包含設(shè)置項(xiàng)
name:配置項(xiàng)
value:屬性值
1
2
3
|
<settings> <setting name= "mapUnderscoreToCamelCase" value= "true" /> </settings> |
3-typeAliases:別名處理器,為java類型起別名
type:指定要起別名的類型全類名;默認(rèn)別名就是類名小寫
alias:指定新的別名
1
|
<typeAlias type= "com.atguigu.mybatis.bean.Employee" alias= "emp" /> |
3.1 為某個包下所有類起別名
package:為某個包下的所有類批量起別名
name:指定包名(為當(dāng)前包以及下面所有的后代包的每一個類都起一個默認(rèn)別名(類名小寫)
1
|
< package name= "com.atguigu.mybatis.bean" /> |
3.2 使用注解@Alias
為某個類指定新的類型
1
2
3
4
|
@Alias ( "emp" ) public class Employee { ...code... } |
4-配置多種MyBatis環(huán)境
- enviroments:配置的環(huán)境們都寫在里面,default指定這個環(huán)境的名稱
- environment:配置一個具體的環(huán)境信息,有id唯一標(biāo)識與transactionManager事務(wù)管理器
- id:唯一標(biāo)識
- transactionManager:事務(wù)管理器,它的有屬性type
- type:事務(wù)管理器的類型JDBC MANAGED 自定義事務(wù)管理器
- dataSource:數(shù)據(jù)源
- type:數(shù)據(jù)源類型 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:支持多數(shù)據(jù)庫
- databaseIdProvider:支持多數(shù)據(jù)庫,它的type為DB_VENDOR作用就是得到數(shù)據(jù)庫廠商的標(biāo)識(驅(qū)動getDatabaseProductName()),mybatis就能根據(jù)數(shù)據(jù)庫廠商標(biāo)識來執(zhí)行不同的sql;
- property:為數(shù)據(jù)庫起名字
- name:
- value:
1
2
3
4
5
6
|
<databaseIdProvider type= "DB_VENDOR" > <!-- 為不同的數(shù)據(jù)庫廠商起別名 --> <property name= "MySQL" value= "mysql" /> <property name= "Oracle" value= "oracle" /> <property name= "SQL Server" value= "sqlserver" /> </databaseIdProvider> |
最終,在mapper.xml中寫入查詢時的的語句,并申明使用到的數(shù)據(jù)庫是什么
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文件注冊進(jìn)入全局配置文件
6.1注冊配置文件:
- resource:引用類路徑下的sql映射文件例如:mybatis/mapper/EmployeeMapper.xml
- url:引用網(wǎng)路路徑或者磁盤路徑下的sql映射文件例如:file:///var/mappers/AuthorMapper.xml
6.2注冊接口:
有sql映射文件,映射文件名必須和接口同名,并且放在與接口同一目錄下;
1
|
<mapper resource= "mybatis/mapper/EmployeeMapper.xml" /> |
沒有sql映射文件,所有的sql都是利用注解寫在接口上,然后再mappers中進(jìn)行注冊;
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批量注冊:
本質(zhì)上,如果包名相同,不管是src內(nèi)還是src外的文件,實(shí)際存儲過程中會被存儲到同一個文件夾中
1
|
< package name= "com.atguigu.mybatis.dao" /> |
到此這篇關(guān)于關(guān)于MyBaties的基本配置標(biāo)簽總結(jié)的文章就介紹到這了,更多相關(guān)MyBaties的基本配置標(biāo)簽內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/Wang_Pro/article/details/118116945