mybatis框架的搭建

2022-08-21 11:30:16 字數 4041 閱讀 3021

# global logging configuration

log4j.rootlogger=debug, stdout

# console output...

1) 開發pojo類

2) 建立全域性配置檔案 sqlmapconfig.xml

<?

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

?>

<

beans

xmlns

=""xmlns:xsi

=""xmlns:mvc

=""xmlns:context

=""xmlns:aop

=""xmlns:tx

=""xsi:schemalocation

=" /spring-beans-3.2.xsd

/spring-mvc-3.2.xsd

/spring-context-3.2.xsd

/spring-aop-3.2.xsd

/spring-tx-3.2.xsd "

>

<?

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

?>

doctype configuration

public "- config 3.0//en"

"">

<

configuration

>

<

environments

default

="development"

>

<

environment

id="development"

>

<

transactionmanager

type

="jdbc"

>

transactionmanager

>

<

datasource

type

="pooled"

>

<

property

name

="driver"

value

="com.mysql.jdbc.driver"

/>

<

property

name

="url"

value

="jdbc:mysql://localhost:3306/mydb2?useunicode=true&characterencoding=utf8"

/>

<

property

name

="username"

value

="root"

/>

<

property

name

="password"

value

="root"

/>

datasource

>

environment

>

environments

>

<

-->

<

>

<

package

name

/>

>

configuration

>

beans

>

**檔案介面

4) 在全域性配置檔案中載入對映檔案

5) 編寫測試**

a) 讀取配置檔案;

b) 通過sqlsessionfactorybuilder建立sqlsessionfactory會話工廠。

c) 通過sqlsessionfactory建立

sqlsession。

d) 呼叫sqlsession的運算元據庫方法。

e) 關閉sqlsession。

public

class

test1

}

<?

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

?>

>

<

namespace

>

<

select

id="getallusers"

resulttype

="user"

>

select user_name username from t_user

select

>

<

select

id="getuserbyuid"

parametertype

="int"

resulttype

="com.domain.user"

>

select

* from t_user where uid = #

select

>

<

select

id="getuserlikename"

parametertype

="string"

resulttype

="com.domain.user"

>

select * from t_user where username like '%$%'

select

>

<

select

id="getuserbyuser"

parametertype

="com.domain.user"

resulttype

="com.domain.user"

>

select * from t_user where username like '%$%'

and gender =

#select

>

<

delete

id="deleteuserbyuid"

parametertype

="int"

>

delete from t_user

where uid = #

delete

>

<

update

id="updateuserbyuid"

parametertype

="com.domain.user"

>

update t_user

setusername = #,

phone = #

where

uid = #

update

>

<

insert

id="insertuser"

parametertype

="com.domain.user"

>

insert into t_user

(uid ,

username , password,gender)

values

(null,#,#,#)

insert

>

<

select

id="getcountuser"

resulttype

="int"

>

select count(*) from t_user

select

>

>

5在config配置

db.properties

檔案

db.driver=com.mysql.jdbc.driver

db.url=jdbc:mysql://

localhost:3306/big?useunicode=true&characterencoding=utf8

db.username=root

db.password=1234

搭建簡單的單個Mybatis框架

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

Mybatis 框架環境的搭建

首先mbaits 框架是乙個開源的免費框架,它支援定製化 sql 儲存過程以及高階對映的優秀的持久層框架。它的底層是對jdbc 的封裝。在src 下配置全域性的xml 檔案 一般我們將名字設定為 mybatis config.xml 2.1 全域性配置檔案的詳解 mybatis config.xml...

MyBatis 框架的搭建和配置

mybatis是支援定製化sql 儲存過程以及高階對映的優秀持久層框架。mybatis 避免了幾乎所有的jdbc 和手動設定引數以及獲取結果集。mybatis可以對配置和原生map使用簡單的xml或註解,將介面和j a的pojos plain old j a objects,普通的j a物件 對映成...