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

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

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

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

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

		<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格式,也需要添加上跟使用注解時的配置。還要多加上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 #配置駝峰?動轉(zhuǎn)換

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

二、簡單啟動

我們先安裝一個插件MybatisX,可以幫我們更簡單實現(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 (接?)中定義的?法名稱?樣的,表?對接?的具體實現(xiàn)?法。
  • resultType :是返回的數(shù)據(jù)類型,也就是我們定義的實體類.

測試:

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對象的屬性名來獲取參數(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:這會令 MyBatis 使? JDBC 的 getGeneratedKeys ?法來取出由數(shù)據(jù)庫內(nèi)部?成的主鍵(?如:像 MySQL 和 SQL Server 這樣的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)的?動遞增字段),默認(rèn)值:false.
  • keyProperty:指定能夠唯?識別對象的屬性,MyBatis 會使? 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)簽即可。
但是我們也會遇見像前面注解的時候因為字段名和變量名不同而導(dǎo)致映射錯誤。解決方式與前面也相似。

  • 使用起別名的查詢語句,將數(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 #配置駝峰?動轉(zhuǎn)換
  • 使用標(biāo)簽result和resultMap。在resultMap標(biāo)簽中放入result標(biāo)簽數(shù)組,result標(biāo)簽的column屬性對應(yīng)數(shù)據(jù)庫字段,property屬性對應(yīng)類屬性名。當(dāng)其他查詢語句需要使用相同的映射時,這需要在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配置文件實現(xiàn)增刪改查的示例代碼的文章就介紹到這了,更多相關(guān)Mybatis XML增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • 使用IDEA如何導(dǎo)入SpringBoot項目

    使用IDEA如何導(dǎo)入SpringBoot項目

    這篇文章主要介紹了使用IDEA如何導(dǎo)入SpringBoot項目問題,具有很好的參考價值,希望對大家有所幫助,
    2023-12-12
  • 簡單了解JAVA構(gòu)造方法

    簡單了解JAVA構(gòu)造方法

    構(gòu)造方法作用就是對類進(jìn)行初始化。 如果你沒有定議任何構(gòu)造方法的形式,程式會為你取一個不帶任何參數(shù)的構(gòu)造函數(shù),那么你產(chǎn)生類的對像時只能用不帶參數(shù)的方法.下面小編和大家來一起學(xué)習(xí)一下吧
    2019-06-06
  • JAVA垃圾收集器與內(nèi)存分配策略詳解

    JAVA垃圾收集器與內(nèi)存分配策略詳解

    這篇文章介紹了JAVA垃圾收集器與內(nèi)存分配策略,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2015-07-07
  • Java異常處理的最佳實踐分享

    Java異常處理的最佳實踐分享

    在我多年的Java開發(fā)經(jīng)驗中,異常處理無疑是項目開發(fā)中必寫的模塊,雖然Java它本身提供了異常處理機(jī)制,但很多開發(fā)者在使用過程中往往會犯一些常見的錯誤,導(dǎo)致程序出現(xiàn)不必要的異常捕獲和性能問題,在本文中,我將分享一些關(guān)于Java異常處理的實用技巧,需要的朋友可以參考下
    2025-08-08
  • Java?熱更新?Groovy?實踐及踩坑指南(推薦)

    Java?熱更新?Groovy?實踐及踩坑指南(推薦)

    Apache的Groovy是Java平臺上設(shè)計的面向?qū)ο缶幊陶Z言,這門動態(tài)語言擁有類似Python、Ruby和Smalltalk中的一些特性,可以作為Java平臺的腳本語言使用,這篇文章主要介紹了Java?熱更新?Groovy?實踐及踩坑指南,需要的朋友可以參考下
    2022-09-09
  • java實現(xiàn)雙人五子棋游戲

    java實現(xiàn)雙人五子棋游戲

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)雙人五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Spring Boot集成MyBatis的方法

    Spring Boot集成MyBatis的方法

    今天小編就為大家分享一篇關(guān)于Spring Boot集成MyBatis的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 進(jìn)一步理解Java中的多態(tài)概念

    進(jìn)一步理解Java中的多態(tài)概念

    這篇文章主要介紹了進(jìn)一步理解Java中的多態(tài)概念,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-08-08
  • 深入剖析Java中的各種異常處理方式

    深入剖析Java中的各種異常處理方式

    這篇文章主要介紹了深入剖析Java中的各種異常處理方式,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-07-07
  • Java多線程并發(fā)synchronized?關(guān)鍵字

    Java多線程并發(fā)synchronized?關(guān)鍵字

    這篇文章主要介紹了Java多線程并發(fā)synchronized?關(guān)鍵字,Java?在虛擬機(jī)層面提供了?synchronized?關(guān)鍵字供開發(fā)者快速實現(xiàn)互斥同步的重量級鎖來保障線程安全。
    2022-06-06

最新評論

呼和浩特市| 凤翔县| 青田县| 岐山县| 托克托县| 遂平县| 常德市| 田林县| 镇原县| 宁德市| 湘潭市| 延庆县| 永兴县| 塔河县| 天柱县| 白山市| 册亨县| 永和县| 揭阳市| 石屏县| 启东市| 都昌县| 江源县| 密山市| 井冈山市| 金溪县| 涟源市| 金昌市| 乐平市| 观塘区| 手机| 诸城市| 二连浩特市| 凉城县| 靖远县| 新闻| 周宁县| 潼关县| 新绛县| 昌宁县| 邵东县|