最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis入門指南之實現(xiàn)對數(shù)據(jù)庫增刪改查

 更新時間:2022年10月29日 10:14:43   作者:人間煙火617  
數(shù)據(jù)持久層主要負責數(shù)據(jù)的增、刪、改、查等功能,MyBatis 則是一款優(yōu)秀的持久層框架,下面這篇文章主要給大家介紹了關于Mybatis入門指南之實現(xiàn)對數(shù)據(jù)庫增刪改查的相關資料,需要的朋友可以參考下

前言

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

MyBatis

簡介

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

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

優(yōu)點

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

缺點

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

搭建第一個Mybatis程序

新建項目。

自定義項目名,我這里定義為myBatisDemo。

引入pom.xml依賴。

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.9</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.30</version>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.24</version>
</dependency>

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

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

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

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

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

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

新建Student實體類。

package com.xyj.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private long id;
    private String name;
    private int age;
}

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

配置Mybatis的配置文件。

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

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

<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test?
                useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

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

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

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

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xyj.mapper.StudentMapper">

    <!--    增   -->
    <insert id="add" parameterType="com.xyj.entity.Student">
        insert into students (id,name,age) values (#{id},#{name},#{age})
    </insert>

    <!--    刪   -->
    <delete id="delete" parameterType="int">
        delete from students where id = #{id};
    </delete>

    <!--    改   -->
    <update id="update" parameterType="com.xyj.entity.Student">
        update students set age = #{age} where id = #{id}
    </update>

    <!--    查   -->
    <select id="query" resultType="com.xyj.entity.Student">
        select * from students
    </select>
</mapper>

在mapper文件中增刪改查對應的標簽為insert、deleteupdate、select。標簽中id是我們之后調用的名字,parameterType表示我們需要傳遞的值的類型。我們這里傳值使用#{}的格式。

在config.xml中加入mapper。

<mappers>
    <mapper resource="com/xyj/mapper/StudentMapper.xml"></mapper>
</mappers>

調用Mybitis原生接口進行操作

public static void main(String[] args) {
    InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("config.xml");
    SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
    SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    SqlSession sqlSession = sqlSessionFactory.openSession();
}

我們首先使用輸入流對config.xml文件進行讀取,然后使用sqlSessionFactoryBuilderbuild方法創(chuàng)建sqlSessionFactory,再用sqlSessionFactoryopenSession方法創(chuàng)建sqlSession。

Student student1 = new Student(1L,"小明",16);
Student student2 = new Student(2L,"小紅",15);
sqlSession.insert("com.xyj.mapper.StudentMapper.add",student1);
sqlSession.insert("com.xyj.mapper.StudentMapper.add",student2);
sqlSession.commit();
sqlSession.close();

調用sqlSessioninsert方法,傳入第一個參數(shù)為我們實體類對應mapper文件中對應的操作id,第二個參數(shù)為數(shù)據(jù)。

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

執(zhí)行結果

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

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

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

執(zhí)行結果

刪除成功!

Student student  = new Student(1L,"小剛",19);
sqlSession.update("com.xyj.mapper.StudentMapper.update",student);

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

執(zhí)行結果

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

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

List<Student> studentList = sqlSession.selectList("com.xyj.mapper.StudentMapper.query");
System.out.println(studentList);

我們用List接收查詢的數(shù)據(jù)再打印出來。

打印結果

[Student(id=1, name=小剛, age=19), Student(id=2, name=小明, age=15), Student(id=3, name=小紅, age=17)]

查詢成功!

總結

關于Mybatis入門配置以及使用原生接口進行增刪改查操作就是這樣,接下來的內容我將來講解Mapper代理的方式進行操作。喜歡的小伙伴們多多支持,你們的支持就是我更新的動力。

到此這篇關于Mybatis入門指南之實現(xiàn)對數(shù)據(jù)庫增刪改查的文章就介紹到這了,更多相關Mybatis對數(shù)據(jù)庫增刪改查內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

册亨县| 大姚县| 阳春市| 鹤山市| 左权县| 睢宁县| 吴桥县| 陆河县| 宣汉县| 旬邑县| 苍溪县| 松滋市| 策勒县| 海原县| 修武县| 青川县| 丰县| 卢氏县| 涿州市| 乾安县| 安宁市| 铜山县| 巴马| 射阳县| 二手房| 曲麻莱县| 梨树县| 旬阳县| 阳原县| 灵山县| 宁夏| 高平市| 都安| 马山县| 磴口县| 思茅市| 庆云县| 武安市| 浠水县| 揭阳市| 宁阳县|