Mybatis入門指南之實(shí)現(xiàn)對數(shù)據(jù)庫增刪改查_第1頁
Mybatis入門指南之實(shí)現(xiàn)對數(shù)據(jù)庫增刪改查_第2頁
Mybatis入門指南之實(shí)現(xiàn)對數(shù)據(jù)庫增刪改查_第3頁
Mybatis入門指南之實(shí)現(xiàn)對數(shù)據(jù)庫增刪改查_第4頁
Mybatis入門指南之實(shí)現(xiàn)對數(shù)據(jù)庫增刪改查_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第Mybatis入門指南之實(shí)現(xiàn)對數(shù)據(jù)庫增刪改查目錄前言MyBatis簡介優(yōu)點(diǎn)缺點(diǎn)搭建第一個(gè)Mybatis程序新建項(xiàng)目。引入pom.xml依賴。新建數(shù)據(jù)庫數(shù)據(jù)表。新建Student實(shí)體類。配置Mybatis的配置文件。創(chuàng)建StudentMapper.xml文件。在config.xml中加入mapper。調(diào)用Mybitis原生接口進(jìn)行操作增刪改查總結(jié)

前言

我們關(guān)于Spring和SpringMVC的學(xué)習(xí)也有一段時(shí)間了,都還沒有進(jìn)行過數(shù)據(jù)庫的操作,而在實(shí)際項(xiàng)目中數(shù)據(jù)庫是必不可少的部分,所以我們接下來將來學(xué)習(xí)Mybatis框架來對數(shù)據(jù)庫進(jìn)行一些操作。

MyBatis

簡介

Mybatis原本是apache的一個(gè)開源項(xiàng)目iBatis,2010年這個(gè)項(xiàng)目由apachesoftwarefoundation遷移到了googlecode,并且改名為MyBatis,2013年11月遷移到Github。Mybatis是一個(gè)實(shí)現(xiàn)了數(shù)據(jù)持久化的開源框架,簡單理解就是對JDBC進(jìn)行封裝封裝再封裝。

所以當(dāng)看到iBatis的時(shí)候我們就應(yīng)該知道,iBatis就是Mybatis?,F(xiàn)在還有很多我們引入的包名還是寫的是iBatis。

優(yōu)點(diǎn)

與JDBC相比,減少了50%以上的代碼量。Mybatis是最簡單的持久化框架,小巧并且簡單易學(xué)。Mybatis靈活,不會對應(yīng)用程序或者數(shù)據(jù)庫的現(xiàn)有設(shè)計(jì)強(qiáng)加任何影響,SQL寫在XML里,從程序代碼中徹底分離,降低耦合度,便于統(tǒng)一管理和優(yōu)化,可重用。提供XML標(biāo)簽,支持編寫動(dòng)態(tài)SQL語句(XML中使用if,else)。提供映射標(biāo)簽,支持對象與數(shù)據(jù)庫的ORM字段關(guān)系映射(在XML中配置映射關(guān)系,也可以使用注解)。

缺點(diǎn)

SQL語句的編寫工作量較大。SQL語句依賴于數(shù)據(jù)庫,導(dǎo)致數(shù)據(jù)庫移植性差,不能隨意更換數(shù)據(jù)庫。

搭建第一個(gè)Mybatis程序

新建項(xiàng)目。

自定義項(xiàng)目名,我這里定義為myBatisDemo。

引入pom.xml依賴。

dependency

groupIdorg.mybatis/groupId

artifactIdmybatis/artifactId

version3.5.9/version

/dependency

dependency

groupIdmysql/groupId

artifactIdmysql-connector-java/artifactId

version8.0.30/version

/dependency

dependency

groupIdjectlombok/groupId

artifactIdlombok/artifactId

version1.18.24/version

/dependency

我們需要引入mybatis依賴、mysql依賴和lombok依賴。

lombok依賴我們前面就使用過,我們可以簡化實(shí)體類的開發(fā)。而mybatis依賴和mysql依賴是我們數(shù)據(jù)庫的依賴。

新建數(shù)據(jù)庫數(shù)據(jù)表。

我們對數(shù)據(jù)庫進(jìn)行操作首先就要有數(shù)據(jù)庫。所以我們來新建一個(gè)數(shù)據(jù)庫。我們這里使用工具創(chuàng)建,不需要手敲SQL命令。

新建一個(gè)數(shù)據(jù)庫名為test,庫中建一個(gè)名為Students的數(shù)據(jù)表。

現(xiàn)在我們的表中還沒有數(shù)據(jù),等下我們使用代碼來對數(shù)據(jù)庫進(jìn)行增刪改查的操作。

新建Student實(shí)體類。

packagecom.xyj.entity;

importlombok.AllArgsConstructor;

importlombok.Data;

importlombok.NoArgsConstructor;

@Data

@AllArgsConstructor

@NoArgsConstructor

publicclassStudent{

privatelongid;

privateStringname;

privateintage;

}

我們實(shí)體類中的屬性要對應(yīng)我們數(shù)據(jù)庫表中的字段。

配置Mybatis的配置文件。

在resources資源文件夾下面創(chuàng)建config.xml配置文件。

在comfig.xml文件中添加以下配置:

xmlversion="1.0"encoding="UTF-8"

!DOCTYPEconfigurationPUBLIC"-////DTDConfig3.0//EN"

"/dtd/mybatis-3-config.dtd"

configuration

environmentsdefault="development"

environmentid="development"

transactionManagertype="JDBC"/transactionManager

dataSourcetype="POOLED"

propertyname="driver"value="com.mysql.cj.jdbc.Driver"/

propertyname="url"value="jdbc:mysql://localhost:3306/test

useUnicode=trueamp;characterEncoding=UTF-8"/

propertyname="username"value="root"/

propertyname="password"value="root"/

/dataSource

/environment

/environments

/configuration

driver、url、username、password分別對應(yīng)數(shù)據(jù)庫驅(qū)動(dòng)、路徑、用戶名和密碼。

創(chuàng)建StudentMapper.xml文件。

我們在開發(fā)中需要為每個(gè)實(shí)體類創(chuàng)建mapper文件,我們需要在其中寫我們對該該實(shí)體類進(jìn)行操作的SQL語句。

xmlversion="1.0"encoding="UTF-8"

!DOCTYPEmapperPUBLIC"-////DTDMapper3.0//EN"

"/dtd/mybatis-3-mapper.dtd"

mappernamespace="com.xyj.mapper.StudentMapper"

!--增--

insertid="add"parameterType="com.xyj.entity.Student"

insertintostudents(id,name,age)values(#{id},#{name},#{age})

/insert

!--刪--

deleteid="delete"parameterType="int"

deletefromstudentswhereid=#{id};

/delete

!--改--

updateid="update"parameterType="com.xyj.entity.Student"

updatestudentssetage=#{age}whereid=#{id}

/update

!--查--

selectid="query"resultType="com.xyj.entity.Student"

select*fromstudents

/select

/mapper

在mapper文件中增刪改查對應(yīng)的標(biāo)簽為insert、delete、update、select。標(biāo)簽中id是我們之后調(diào)用的名字,parameterType表示我們需要傳遞的值的類型。我們這里傳值使用#{}的格式。

在config.xml中加入mapper。

mappers

mapperresource="com/xyj/mapper/StudentMapper.xml"/mapper

/mappers

調(diào)用Mybitis原生接口進(jìn)行操作

publicstaticvoidmain(String[]args){

InputStreaminputStream=Test.class.getClassLoader().getResourceAsStream("config.xml");

SqlSessionFactoryBuildersqlSessionFactoryBuilder=newSqlSessionFactoryBuilder();

SqlSessionFactorysqlSessionFactory=sqlSessionFactoryBuilder.build(inputStream);

SqlSessionsqlSession=sqlSessionFactory.openSession();

}

我們首先使用輸入流對config.xml文件進(jìn)行讀取,然后使用sqlSessionFactoryBuilder的build方法創(chuàng)建sqlSessionFactory,再用sqlSessionFactory的openSession方法創(chuàng)建sqlSession。

Studentstudent1=newStudent(1L,"小明",16);

Studentstudent2=newStudent(2L,"小紅",15);

sqlSession.insert("com.xyj.mapper.StudentMapper.add",student1);

sqlSession.insert("com.xyj.mapper.StudentMapper.add",student2);

sqlSmit();

sqlSession.close();

調(diào)用sqlSession的insert方法,傳入第一個(gè)參數(shù)為我們實(shí)體類對應(yīng)mapper文件中對應(yīng)的操作id,第二個(gè)參數(shù)為數(shù)據(jù)。

我們執(zhí)行完sql之后還需要提交事務(wù),最后釋放資源。

執(zhí)行結(jié)果:

成功在數(shù)據(jù)表中添加了兩條數(shù)據(jù)。

sqlSession.delete("com.xyj.mapper.StudentMapper.delete",2);

我們這里調(diào)用delete方法,刪除id為2的學(xué)生數(shù)據(jù)。

執(zhí)行結(jié)果:

刪除成功!

Studentstudent=newStudent(1L,"小剛",19);

sqlSession.update("com.xyj.mapper.StudentMapper.update",student);

調(diào)用update方法修改數(shù)據(jù)。

執(zhí)行結(jié)果:

修改數(shù)據(jù)成功!

我們再添加兩條數(shù)據(jù)。

ListStudentstudentList=sqlSession.selectList("com.xyj.mapper.StudentMapper.query");

System.out.p

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論