mybatis dao新增案例

2021-07-09 23:27:15 字數 3321 閱讀 5178

// mybatis工作流程

1)通過reader物件讀取src目錄下的mybatis.

xml配置檔案(該文字的位置和名字可任意)

2)通過sqlsessionfactorybuilder物件建立sqlsessionfactory物件

3)從當前執行緒中獲取sqlsession物件

4)事務開始,在mybatis中預設

xml對映檔案中的操作編號,從而讀取sql語句

6)事務提交,必寫

7)關閉sqlsession物件,並且分開當前執行緒與sqlsession物件,讓gc盡早**

//sql:

create

table students(

id int(5) primary

key,

name varchar(10),

sal double(8,2)

);

/**

* 學生

*@author admintc

*/public

class

student

public

student(integer id, string name, double sal)

public integer getid()

public

void

setid(integer id)

public string getname()

public

void

setname(string name)

public double getsal()

public

void

setsal(double sal)

}

<?xml version="1.0" encoding="utf-8"?>

resource="db.properties"/>

alias="student"/>

typealiases>

default="mysql_developer">

id="mysql_developer">

type="jdbc"/>

type="pooled">

name="driver"

value="$"/>

name="url"

value="$"/>

name="username"

value="$"/>

name="password"

value="$"/>

datasource>

environment>

id="oracle_developer">

type="jdbc"/>

type="pooled">

name="driver"

value="$"/>

name="url"

value="$"/>

name="username"

value="$"/>

name="password"

value="$"/>

datasource>

environment>

environments>

configuration>

/**

* 工具類

*@author admintc

*/public

class

mybatisutil catch (ioexception e)

}/**

* 禁止外界通過new方法建立

*/private

mybatisutil(){}

/*** 獲取sqlsession

*/public

static sqlsession getsqlsession()

//返回sqlsession物件

return sqlsession;

}/**

* 關閉sqlsession與當前執行緒分開

*/public

static

void

closesqlsession()

}/**

* 測試

*/public

static

void

main(string args)

}

<?xml version="1.0" encoding="utf-8" ?>

type="student"

id="studentmap">

property="id"

column="id"/>

property="name"

column="name"/>

property="sal"

column="sal"/>

resultmap>

id="add1">

insert into students(id,name,sal) values(1,'哈哈',7000)

insert>

id="add2"

parametertype="student">

insert into students(id,name,sal) values(#,#,#)

insert>

id="add3"

parametertype="student">

insert into students(id,name,sal) values(#,#,#)

insert>

/**

* 持久層

*@author admintc

*/public

class

studentdao catch(exception e)finally

}/**

* 增加學生

*/public

void

add2(student student) throws exceptioncatch(exception e)finally

}/**

* 增加學生

*/public

void

add3(student student) throws exceptioncatch(exception e)finally

}//測試:

public

static

void

main(string args) throws exception

}

sql server新增列阻塞案例

if db id test is null create database test gouse test ifdb id test1 is notnull begin drop table test1 end withtas select 1as id,abcdef as str1 union a...

案例 新增陣列方法

foreach 迭代 遍歷 陣列 vararr 1,2,3 varsum 0 arr.foreach function value,index,array console.log sum console.log 分隔行 filter 篩選陣列 vararr1 12,66,4,88,3,7 varne...

Mybatis Dao層引數傳遞的三種方式

userdao介面有乙個方法 public user selectuser string name,string area 第一種 userdao介面有乙個方法 public user selectuser string name,string area select id selectuser r...