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

MyBatis XML 配置文件之從配置規(guī)范到 CRUD 開發(fā)實(shí)踐記錄

 更新時(shí)間:2026年01月04日 10:52:54   作者:?喜歡做夢  
本文介紹了MyBatis的XML配置文件的使用,包括數(shù)據(jù)庫連接配置、MyBatis配置、XML文件的核心組成成分以及增刪查改操作的示例,感興趣的朋友跟隨小編一起看看吧

??前言

MyBatis的開發(fā)方式有兩種:注解、XML。下來要將的就是XML,如果想要看MyBatis注解的,可以看我上一篇文章

??MyBatis XML配置文件的作用

MyBatis是一款持久層框架,他支持將SQL語句與Java代碼分離,XML文件就是用來編寫SQL語句,配置結(jié)果映射(ResultMap)等信息載體,讓代碼等清晰、維護(hù)更方便。

??配置數(shù)據(jù)庫連接字符串和MyBatis

??數(shù)據(jù)庫配置

application.yml 配置內(nèi)容

# 數(shù)據(jù)庫連接配置
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

application.properties 配置內(nèi)容

#驅(qū)動類名稱
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#數(shù)據(jù)庫連接的url
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false
#連接數(shù)據(jù)庫的用戶名
spring.datasource.username=root
#連接數(shù)據(jù)庫的密碼
spring.datasource.password=root
  • datasource.url: 數(shù)據(jù)庫連接地址;
  • datasource.username:數(shù)據(jù)庫登入用戶名(例root);
  • datasource.password: 數(shù)據(jù)庫密碼(例root);
  • datasource.driver-class-name:MySqL驅(qū)動類。

??MyBatis配置

application.yml 配置內(nèi)容

mybatis:
  # 配置 mybatis xml 的文件路徑,在 resources/mapper 創(chuàng)建所有表的 xml 文件
  mapper-locations: classpath:mapper/**Mapper.xml
   configuration: # 配置打印 MyBatis日志
     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
     map-underscore-to-camel-case: true #配置駝峰自動轉(zhuǎn)換

application.properties 配置內(nèi)容

# 配置 mybatis xml 的文件路徑,在 resources/mapper 創(chuàng)建所有表的 xml 文件
mybatis.mapper-locations=classpath:mapper/**Mapper.xml
# 配置打印 MyBatis 日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# 配置駝峰自動轉(zhuǎn)換
mybatis.configuration.map-underscore-to-camel-case=true
  • mybatis.mapper-locations:指定映射文件(XML)位置。classpath:mapper/**Mapper.xml表示resource/mapper目錄以下所有以Mapper.xml為結(jié)尾的文件;
  • mybatis.configuration.log-impl:配置MyBatis的日志實(shí)現(xiàn);
  • mybatis.configuration.map-underscore-to-camel-case:開啟駝峰命名。

??XML文件的核心組成成分

以映射文件(UserInfoMapper.xml)為例

??MyBatis的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="對應(yīng)的Mapper接口的全限定類名">
</mapper>
  • 示例對應(yīng)的Mapper接口的全限定類名:
<mapper namespace="com.mybatis.demo.mapper.UserInfoXMLMapper">
      <!-- SQL語句   -->
</mapper>

??文件創(chuàng)建

創(chuàng)建Mapper接口

@Mapper
public interface UserInfoXMLMapper {
}

創(chuàng)建xml文件

??增刪查改操作

??增(Insert)

UserInfoMapper.xml

 <insert id="insertUserInfo" parameterType="com.mybatis.demo.model.UserInfo">
        insert into user_info(username,password,age) values (#{username},#{password},#{age})
    </insert>
  • <insert>的id必須與UserInfoXMLMapper中的insertUserInfo一致;
  • parameType:入?yún)㈩愋停墒÷裕?,如果要寫,要寫全類名?/li>

UserInfoXMLMapper接口

@Mapper
public interface UserInfoXMLMapper {
       Integer insertUserInfo(UserInfo userInfo);
}

測試

@SpringBootTest
class UserInfoXMLMapperTest {
     @Autowired
     private UserInfoXMLMapper userInfoXMLMapper;
    @Test
    void insertUserInfo() {
        UserInfo userInfo=new UserInfo();
        userInfo.setUsername("張三");
        userInfo.setPassword("123");
        userInfo.setAge(13);
        System.out.println(userInfoXMLMapper.insertUserInfo(userInfo));
    }
}

結(jié)果

添加自增主鍵

 </insert>
    <insert id="insertUserInfo2" parameterType="com.mybatis.demo.model.UserInfo" useGeneratedKeys="true" keyProperty="id">
        insert into user_info(username,password,age) values (#{username},#{password},#{age})
    </insert>

??刪除(delete)

UserInfoMapper.xml

<delete id="deleteUser">
        delete from user_info where username=(#{username})
    </delete>

UserInfoXMLMapper接口

Integer deleteUser(UserInfo userInfo);

測試

@Test
    void deleteUser() {
        UserInfo userInfo=new UserInfo();
        userInfo.setUsername("張三");
        userInfoXMLMapper.deleteUser(userInfo);
    }

結(jié)果

??查(select)

UserInfoMapper.xml

    <select id="selectUserById" resultType="com.mybatis.demo.model.UserInfo">
        select username,password,age,phone from user_info where id=(#{id})
    </select>
  • resultType:返回值類型,如果缺少,那么參數(shù)占位符寫法不規(guī)范。

UserInfoXMLMapper接口

UserInfo selectUserById(Integer id);

測試

@Test
    void selectUserById() {
        userInfoXMLMapper.selectUserById(1);
    }

結(jié)果

??改(update)

UserInfoMapper.xml

Integer updateUser(String username,Integer id);

UserInfoXMLMapper接口

<update id="updateUser">
        update user_info set username=#{username} where id=#{id}
    </update>

測試

 @Test
    void updateUser() {
        userInfoXMLMapper.updateUser("lisi",2);
    }

結(jié)果

到此這篇關(guān)于MyBatis XML 配置文件之從配置規(guī)范到 CRUD 開發(fā)實(shí)踐記錄的文章就介紹到這了,更多相關(guān)MyBatis XML 配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中的StopWatch計(jì)時(shí)利器使用指南

    Java中的StopWatch計(jì)時(shí)利器使用指南

    StopWatch通常用于測量一段代碼執(zhí)行所花費(fèi)的時(shí)間,它能夠精確地記錄開始時(shí)間、結(jié)束時(shí)間,并計(jì)算出這中間的時(shí)間差,下面給大家介紹Java中的StopWatch計(jì)時(shí)利器的深度解析與使用指南,感興趣的朋友一起看看吧
    2025-05-05
  • 詳解SpringBoot中如何使用布隆過濾器

    詳解SpringBoot中如何使用布隆過濾器

    這篇文章主要為大家詳細(xì)介紹了在SpringBoot中如何簡單在代碼中使用布隆過濾器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-09-09
  • JavaMail郵件簡介及API概述第一篇

    JavaMail郵件簡介及API概述第一篇

    這篇文章主要為大家詳細(xì)介紹了JavaMail郵件簡介及API概述第一篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • 了解Java線程池執(zhí)行原理

    了解Java線程池執(zhí)行原理

    那么有沒有一種辦法使得線程可以復(fù)用,就是執(zhí)行完一個任務(wù),并不被銷毀,而是可以繼續(xù)執(zhí)行其他的任務(wù)?在Java中可以通過線程池來達(dá)到這樣的效果。下面我們來詳細(xì)了解一下吧
    2019-05-05
  • Java實(shí)現(xiàn)圖片百葉窗效果(附源碼)

    Java實(shí)現(xiàn)圖片百葉窗效果(附源碼)

    在數(shù)字圖像處理領(lǐng)域,各種特效的實(shí)現(xiàn)不僅能夠提升圖片的美觀性,也能為后續(xù)的視頻合成、動畫制作提供基礎(chǔ)素材,使用 Java 語言結(jié)合 AWT/Swing 或 JavaFX,可以實(shí)現(xiàn)對圖像的像素級操作,以達(dá)到百葉窗特效,本文給大家介紹的非常詳細(xì),感興趣的小伙伴跟著小編一起來看看吧
    2025-05-05
  • SpringBoot整合Web開發(fā)之文件上傳與@ControllerAdvice

    SpringBoot整合Web開發(fā)之文件上傳與@ControllerAdvice

    @ControllerAdvice注解是Spring3.2中新增的注解,學(xué)名是Controller增強(qiáng)器,作用是給Controller控制器添加統(tǒng)一的操作或處理。對于@ControllerAdvice,我們比較熟知的用法是結(jié)合@ExceptionHandler用于全局異常的處理,但其作用不止于此
    2022-08-08
  • SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫

    SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫

    這篇文章主要介紹了SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • 一文掌握Spring的創(chuàng)建與使用

    一文掌握Spring的創(chuàng)建與使用

    這篇文章詳細(xì)介紹了spring的創(chuàng)建與使用,文章中有詳細(xì)的代碼示例和圖片介紹,對學(xué)習(xí)有一定的而參考價(jià)值,需要的同學(xué)可以參考一下
    2023-04-04
  • 背包問題-動態(tài)規(guī)劃java實(shí)現(xiàn)的分析與代碼

    背包問題-動態(tài)規(guī)劃java實(shí)現(xiàn)的分析與代碼

    這篇文章主要給大家介紹了關(guān)于背包問題動態(tài)規(guī)劃java實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Java 信號量Semaphore的實(shí)現(xiàn)

    Java 信號量Semaphore的實(shí)現(xiàn)

    這篇文章主要介紹了Java 信號量Semaphore的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09

最新評論

延寿县| 新竹市| 朝阳区| 博爱县| 陵水| 巴彦县| 基隆市| 龙山县| 揭西县| 云龙县| 麻江县| 平和县| 台中市| 潜山县| 水城县| 井研县| 杭锦后旗| 汉阴县| 桑日县| 仪陇县| 额济纳旗| 云阳县| 启东市| 资中县| 贺兰县| 平远县| 承德县| 楚雄市| 平昌县| 玉田县| 广州市| 桂东县| 鹿泉市| 芜湖市| 广昌县| 漳浦县| 卫辉市| 锦屏县| 万山特区| 启东市| 时尚|