一個(gè)框架的使用,必然離不開(kāi)其中的組件支持。我們?cè)谙螺d完mybatis框架后,因?yàn)榇蟛糠值膬?nèi)部結(jié)構(gòu)還沒(méi)有啟動(dòng),就要手動(dòng)的對(duì)其進(jìn)行配置。在之前有提到,mybatis框架的作用就有數(shù)據(jù)庫(kù)方面的,所以本篇文章帶來(lái)了數(shù)據(jù)庫(kù)和sql方面的配置方法,大家一起往下面看看具體操作。
1.配置數(shù)據(jù)庫(kù)
創(chuàng)建mybatis的配置文件,配置數(shù)據(jù)庫(kù)的信息。數(shù)據(jù)庫(kù)我們可以配置多個(gè),但是默認(rèn)的只能用一個(gè)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<?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> <!-- 加載類(lèi)路徑下的屬性文件 --> <properties resource= "db.properties" /> <!-- 設(shè)置一個(gè)默認(rèn)的連接環(huán)境信息 --> <environments default = "mysql_developer" > <!-- 連接環(huán)境信息,取一個(gè)任意唯一的名字 --> <environment id= "mysql_developer" > <!-- mybatis使用jdbc事務(wù)管理方式 --> <transactionmanager type= "jdbc" /> <!-- mybatis使用連接池方式來(lái)獲取連接 --> <datasource type= "pooled" > <!-- 配置與數(shù)據(jù)庫(kù)交互的 4 個(gè)必要屬性 --> <property name= "driver" value= "${mysql.driver}" /> <property name= "url" value= "${mysql.url}" /> <property name= "username" value= "${mysql.username}" /> <property name= "password" value= "${mysql.password}" /> </datasource> </environment> <!-- 連接環(huán)境信息,取一個(gè)任意唯一的名字 --> <environment id= "oracle_developer" > <!-- mybatis使用jdbc事務(wù)管理方式 --> <transactionmanager type= "jdbc" /> <!-- mybatis使用連接池方式來(lái)獲取連接 --> <datasource type= "pooled" > <!-- 配置與數(shù)據(jù)庫(kù)交互的 4 個(gè)必要屬性 --> <property name= "driver" value= "${oracle.driver}" /> <property name= "url" value= "${oracle.url}" /> <property name= "username" value= "${oracle.username}" /> <property name= "password" value= "${oracle.password}" /> </datasource> </environment> </environments> </configuration> |
2.配置sqlsessionfactory
mybatis 的sqlsessionfactory 接口除了使用基于 xml 的配置創(chuàng)建外也可以通過(guò) java api 編程式地被創(chuàng)建。每個(gè)在 xml 中配置的元素,都可以編程式的創(chuàng)建。
使用 java api 創(chuàng)建 sqlsessionfactory,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public static sqlsessionfactory getsqlsessionfactoryusingjavaapi() { if (javasqlsessionfactory == null ) { try { datasource datasource = datasourcefactory.getdatasource(); transactionfactory transactionfactory = new jdbctransactionfactory(); environment environment = new environment( "development" , transactionfactory, datasource); configuration configuration = new configuration(environment); configuration.gettypealiasregistry().registeralias( "student" , student. class ); configuration.gettypehandlerregistry().register(phonetypehandler. class ); configuration.addmapper(studentmapper. class ); javasqlsessionfactory = new sqlsessionfactorybuilder().build(configuration); } catch (exception e) { throw new runtimeexception(e); } } return javasqlsessionfactory; } |
這個(gè)配置里,加載了一個(gè)映射類(lèi)。映射類(lèi)是包含了 sql 映射注解的 java類(lèi),可以用來(lái)取代 xml。然而 ,由于 java 注解的一些限制和 mybatis 映射的復(fù)雜性,一些高級(jí)的映射還是要用 xml 來(lái)配置,比如嵌套映射等。由于這個(gè)原因,mybatis 會(huì)自動(dòng)查找和加載已經(jīng)存在的 xml。
內(nèi)容擴(kuò)展:
mybatis參數(shù)的設(shè)置
數(shù)據(jù)庫(kù)進(jìn)行添加操作需要注意的問(wèn)題
? ①.主鍵自增:在建立數(shù)據(jù)庫(kù)表的時(shí)候可以設(shè)置主鍵自增
? ②.主鍵不自增:可以自己手動(dòng)設(shè)置
1
2
3
|
<insert id= "save" parametertype= "book" keycolumn= "id" keyproperty= "id" usegeneratedkeys= "true" > insert into jpa_book (author,createtime,name,price,sales,stock) values (#{author},#{createtime},#{name},#{price},#{sales},#{stock}); </insert> |
keycolumn=“id” 指定數(shù)據(jù)庫(kù)表主鍵字段
? keyproperty=“id” 設(shè)置數(shù)據(jù)庫(kù)表對(duì)應(yīng)實(shí)體類(lèi)的屬性名
? usegeneratedkeys=“true” 開(kāi)啟主鍵自增
? ③.主鍵不支持自增:例如oracle數(shù)據(jù)庫(kù)就不支持自增
1
2
3
|
<selectkey keyproperty= "id" resulttype= "int" keycolumn= "id" order= "before" > select last_insert_id <!--或者使用 select uuid()生成--> </selectkey> |
order=“before” 表示先生成主鍵再進(jìn)行自增
? select last_insert_id 或者使用 select uuid() 通過(guò)函數(shù)生成主鍵值
到此這篇關(guān)于java mybatis框架配置詳解的文章就介紹到這了,更多相關(guān)java mybatis框架的配置內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://www.py.cn/java/jiaocheng/23495.html