Mybatis的簡單搭建

2022-01-23 05:45:08 字數 3467 閱讀 7835

1.****

2.匯入jar包

3.根據官方文件,首先寫mybatis-config.xml檔案

xml version="1.0" encoding="utf-8" 

?>

doctype configuration

public "- config 3.0//en"

"">

<

configuration

>

<

properties

resource

="conf/jdbc.properties"

/>

<

typealiases

>

<

package

name

="com.entity"

/>

typealiases

>

<

environments

default

="development"

>

<

environment

id="test"

>

<

transactionmanager

type

="jdbc"

/>

<

datasource

type

="pooled"

>

<

property

name

="driver"

value

="$"

/>

<

property

name

="url"

value

="$"

/>

<

property

name

="username"

value

="$"

/>

<

property

name

="password"

value

="$"

/>

datasource

>

environment

>

<

environment

id="development"

>

<

transactionmanager

type

="jdbc"

/>

<

datasource

type

="pooled"

>

<

property

name

="driver"

value

="$"

/>

<

property

name

="url"

value

="$"

/>

<

property

name

="username"

value

="$"

/>

<

property

name

="password"

value

="$"

/>

datasource

>

environment

>

environments

>

<

databaseidprovider

type

="db_vendor"

>

<

property

name

="mysql"

value

="mysql"

/>

<

property

name

="oracle"

value

="oracle"

/>

<

property

name

="sql server"

value

="sqlserver"

/>

databaseidprovider

>

<

>

<

resource

/>

>

configuration

>

用來設定資料來源和對映實體的xml檔案,包括為返回值型別起別名,資料來源環境,資料庫廠商(移植性)等等,.,

4.編寫實體類

public

class

book

public

void

setid(integer id)

public

string gettitle()

public

void

settitle(string title)

public

string getprice()

public

void

setprice(string price)

public

string getpublishdate()

public

void

setpublishdate(string publishdate)

@override

public

string tostring()

}

xml version="1.0" encoding="utf-8" 

?>

>

<

namespace

>

<

select

id="selectbook"

resulttype

="book"

databaseid

="mysql"

>

select * from book where id = #

select

>

>

其中namespace是實體的類路徑,resulttype是返回值型別(全限定類名),databaseid是資料庫廠商id

6.編寫測試類

public

class

test

finally}}

7.執行結果:

book [id=1, title=2, price=3, publishdate=2016-11-02 20:44:20.0]

搭建簡單的單個Mybatis框架

最近幾天還是在看框架,都是為了找工作。另外,鄭州的工作是真的不好找。今天來寫乙個搭建單個框架的練習,搭建的是mybatis,主要的原因是自己對於單個框架的原理不太懂,雖然前面的部落格對於ssm框架進行了整合,但是最好還是自己在進行深入的了解。不再廢話了,直接開始。下面就說一說具體的搭建思路。1.複製...

mybatis框架的搭建

global logging configuration log4j.rootlogger debug,stdout console output.1 開發pojo類 2 建立全域性配置檔案 sqlmapconfig.xml xml version 1.0 encoding utf 8 beans ...

mybatis環境搭建

1.需要在src下面搭建乙個mybatis config.xml的檔案 2.需要配置當前的xml檔案 oracle.jdbc.driver.oracledriver 這裡是驅動 jdbc oracle thin localhost 1521 orcl 這是配置url xmlversion 1.0 e...