亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Mybatis?XML配置文件實(shí)現(xiàn)增刪改查的示例代碼

 更新時(shí)間:2025年03月25日 08:31:56   作者:鴿鴿程序猿  
本文主要介紹了Mybatis?XML配置文件實(shí)現(xiàn)增刪改查的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、環(huán)境準(zhǔn)備

在使用XML來實(shí)現(xiàn)的數(shù)據(jù)庫操作的時(shí)候,我們的依賴下載與前面的使用注解時(shí)的依賴是一樣的。

		<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.4</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        
		<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter-test</artifactId>
            <version>3.0.4</version>
            <scope>test</scope>
        </dependency>

在配置文件yml格式,也需要添加上跟使用注解時(shí)的配置。還要多加上mybatis. mapper-locations: classpath:mapper/**Mapper.xml

# 數(shù)據(jù)庫連接配置 
spring:
  application:
    name: spring-mybatis-demo

  datasource:
    url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false
    username: root
    password: 1234
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  configuration: # 配置打印 MyBatis?志
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true #配置駝峰?動(dòng)轉(zhuǎn)換

# 配置 mybatis xml 的?件路徑,在 resources/mapper 創(chuàng)建所有表的 xml ?件 
  mapper-locations: classpath:mapper/**Mapper.xml

二、簡單啟動(dòng)

我們先安裝一個(gè)插件MybatisX,可以幫我們更簡單實(shí)現(xiàn)xml文件與接口之間的跳轉(zhuǎn)。

mapper接口:

package com.example.springmybatisdemo.mapper;

import com.example.springmybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
@Mapper
public interface UserMapperXML {
    List<UserInfo> selectAll();
}

xml文件:

<?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.example.springmybatisdemo.mapper.UserMapperXML">

    <select id="selectAll" resultType="com.example.springmybatisdemo.model.UserInfo">
        select * from user_info;
    </select>

</mapper>

  • <mapper> 標(biāo)簽:需要指定 namespace 屬性,表?命名空間,值為 UserMapperXML 接?的全限定名,包括全包名.類名。
  • <select> 查詢標(biāo)簽:是?來執(zhí)?數(shù)據(jù)庫的查詢操作的:
  • id :是和 Interface (接?)中定義的?法名稱?樣的,表?對(duì)接?的具體實(shí)現(xiàn)?法。
  • resultType :是返回的數(shù)據(jù)類型,也就是我們定義的實(shí)體類.

測試:

package com.example.springmybatisdemo.mapper;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class UserMapperXMLTest {
    @Autowired
    private UserMapperXML userMapperXML;
    @BeforeEach
    void setUp() {
    }

    @AfterEach
    void tearDown() {
    }

    @Test
    void selectAll() {
        System.out.println(userMapperXML.selectAll());
    }
}

結(jié)果:

三、增< insert id = >

使用標(biāo)簽< Insert >來寫入數(shù)據(jù),直接使?UserInfo對(duì)象的屬性名來獲取參數(shù)。

    <insert id="insertOne">
        insert into user_info (username, password, age) values (#{username},#{password},#{age})
    </insert>

測試函數(shù):

    @Test
    void insertOne() {
        UserInfo userInfo = new UserInfo();
        userInfo.setAge(8);
        userInfo.setPassword("888");
        userInfo.setUsername("888");
        Integer result = userMapperXML.insertOne(userInfo);
        System.out.println("增加函數(shù):"+ result);
    }

測試結(jié)果:

四、返回主鍵

還是使用< insert >標(biāo)簽來寫入數(shù)據(jù),只不過設(shè)置useGeneratedKeys 和keyProperty屬性 。

  • useGeneratedKeys:這會(huì)令 MyBatis 使? JDBC 的 getGeneratedKeys ?法來取出由數(shù)據(jù)庫內(nèi)部?成的主鍵(?如:像 MySQL 和 SQL Server 這樣的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)的?動(dòng)遞增字段),默認(rèn)值:false.
  • keyProperty:指定能夠唯?識(shí)別對(duì)象的屬性,MyBatis 會(huì)使? getGeneratedKeys 的返回值或 insert 語句的 selectKey ?元素設(shè)置它的值,默認(rèn)值:未設(shè)置(unset)
<insert id="insertOne" useGeneratedKeys="true" keyProperty="id">
        insert into user_info (username, password, age) values (#{username},#{password},#{age})
    </insert>

測試方法:

    @Test
    void insertOne() {
        UserInfo userInfo = new UserInfo();
        userInfo.setAge(9);
        userInfo.setPassword("999");
        userInfo.setUsername("999");
        Integer result = userMapperXML.insertOne(userInfo);
        System.out.println("增加函數(shù):"+ result+", 增加數(shù)據(jù)的id:"+userInfo.getId());
    }

結(jié)果:

五、刪<delete id = >

使用< delete >標(biāo)簽,加上刪除的SQL語句即可。

    <delete id="deleteOne">
        delete from user_info where id = #{id}
    </delete>

測試方法:

    @Test
    void deleteOne() {
        userMapperXML.deleteOne(9);
    }

結(jié)果:

六、改<update id = >

修改數(shù)據(jù)直接使用< update >注解,加上修改SQL語句即可。

    <update id="updateOne">
        update user_info set delete_flag = #{deleteFlag} where id = #{id}
    </update>

測試方法:

   @Test
    void updateOne() {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(8);
        userInfo.setDeleteFlag(1);
        userMapperXML.updateOne(userInfo);
    }

結(jié)果:

七、查< select id = resultType = >

查詢我們只需要使用標(biāo)簽即可。
但是我們也會(huì)遇見像前面注解的時(shí)候因?yàn)樽侄蚊妥兞棵煌鴮?dǎo)致映射錯(cuò)誤。解決方式與前面也相似。

  • 使用起別名的查詢語句,將數(shù)據(jù)庫不同字段名取別名為屬性名。
<?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.example.springmybatisdemo.mapper.UserMapperXML">

    <select id="selectAll" resultType="com.example.springmybatisdemo.model.UserInfo">
    
        select username , password, age, gender, phone,
        delete_flag as deleteFlag , create_time as createTime, update_time as updateTime
        from user_info
        
    </select>

</mapper>
  • 使用配置文件將數(shù)據(jù)庫字段中使用下劃線的蛇形命名轉(zhuǎn)換為小駝峰命名。mybatis.configuration.map-underscore-to-camel-case: true
mybatis:
  configuration:
    map-underscore-to-camel-case: true #配置駝峰?動(dòng)轉(zhuǎn)換
  • 使用標(biāo)簽result和resultMap。在resultMap標(biāo)簽中放入result標(biāo)簽數(shù)組,result標(biāo)簽的column屬性對(duì)應(yīng)數(shù)據(jù)庫字段,property屬性對(duì)應(yīng)類屬性名。當(dāng)其他查詢語句需要使用相同的映射時(shí),這需要在select標(biāo)簽的resultMap屬性寫上resultMap標(biāo)簽的id屬性即可。
	<resultMap id="UserMap" type="com.example.springmybatisdemo.model.UserInfo">
        <result column="delete_flag" property="deleteFlag"></result>
        <result column="create_time" property="createTime"></result>
        <result column="update_time" property="updateTime"></result>
    </resultMap>
    
    <select id="selectAll" resultType="com.example.springmybatisdemo.model.UserInfo" resultMap="UserMap">
        select * from user_info
    </select>

到此這篇關(guān)于Mybatis XML配置文件實(shí)現(xiàn)增刪改查的示例代碼的文章就介紹到這了,更多相關(guān)Mybatis XML增刪改查內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • Java接口和抽象類用法實(shí)例總結(jié)

    Java接口和抽象類用法實(shí)例總結(jié)

    這篇文章主要介紹了Java接口和抽象類用法,結(jié)合實(shí)例形式總結(jié)分析了Java接口與抽象類的具體定義、使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2015-12-12
  • Spring?AOP原理及動(dòng)態(tài)代理

    Spring?AOP原理及動(dòng)態(tài)代理

    這篇文章主要介紹了Spring?AOP原理及動(dòng)態(tài)代理,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • springboot一個(gè)自定義注解如何搞定多線程事務(wù)

    springboot一個(gè)自定義注解如何搞定多線程事務(wù)

    文章介紹了Spring?Boot中使用`@Async`注解進(jìn)行聲明式多線程編程的方法,以及如何通過自定義注解和AOP實(shí)現(xiàn)多線程事務(wù)控制,同時(shí),還解釋了`CountDownLatch`的使用場景及其工作原理
    2024-12-12
  • Java Volatile關(guān)鍵字你真的了解嗎

    Java Volatile關(guān)鍵字你真的了解嗎

    這篇文章主要為大家介紹了Java Volatile關(guān)鍵字,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • mybatis-plus使用@EnumValue處理枚舉類型的示例代碼

    mybatis-plus使用@EnumValue處理枚舉類型的示例代碼

    這篇文章主要介紹了mybatis-plus使用@EnumValue處理枚舉類型的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Eclipse 出現(xiàn)A configuration with this name already exists問題解決方法

    Eclipse 出現(xiàn)A configuration with this name already exists問題解決方

    這篇文章主要介紹了Eclipse 出現(xiàn)A configuration with this name already exists問題解決方法的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • 解決SpringBoot跨域的三種方式

    解決SpringBoot跨域的三種方式

    前后端分離是目前的趨勢,解決跨域問題也是老生常談的話題了,我們了解一下什么是域和跨域。域:協(xié)議 + 域名 + 端口;三者完全相同則為同域,反之有其一不同均為不同域??缬蛘?qǐng)求:當(dāng)前【發(fā)起請(qǐng)求】的域和【請(qǐng)求指向】的域?qū)儆诓煌驎r(shí),該次請(qǐng)求稱之為跨域請(qǐng)求
    2021-06-06
  • SpringBoot快速遷移至Quarkus的方法步驟

    SpringBoot快速遷移至Quarkus的方法步驟

    這篇文章主要介紹了SpringBoot快速遷移至Quarkus的方法步驟。文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • JAVA計(jì)算兩個(gè)日期相差的實(shí)例

    JAVA計(jì)算兩個(gè)日期相差的實(shí)例

    在java中我找了一下關(guān)于計(jì)算兩個(gè)日期之間相差的天數(shù)方法有10多種實(shí)現(xiàn)方法,下面總結(jié)一下常用的幾種計(jì)算兩個(gè)日期之間相差的天數(shù)實(shí)例
    2013-11-11
  • java基于Apache FTP實(shí)現(xiàn)文件上傳、下載、修改文件名、刪除

    java基于Apache FTP實(shí)現(xiàn)文件上傳、下載、修改文件名、刪除

    本篇文章主要介紹了Apache FTP實(shí)現(xiàn)文件上傳、下載、修改文件名、刪除,實(shí)現(xiàn)了FTP文件上傳(斷點(diǎn)續(xù)傳)、FTP文件下載、FTP文件重命名、FTP文件刪除等功能,有需要的可以了解一下。
    2016-11-11

最新評(píng)論