MyBatis XML 配置文件之從配置規(guī)范到 CRUD 開發(fā)實(shí)踐記錄
??前言
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.Driverapplication.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)文章希望大家以后多多支持腳本之家!
- MyBatis Generator配置生成接口和XML映射文件的實(shí)現(xiàn)
- Mybatis mapper配置文件xml存放位置
- mybatis配置Mapper.xml文件時(shí)遇到的問題及解決
- SpringBoot集成Mybatis+xml格式的sql配置文件操作
- mybatis的dtd約束文件及配置文件xml自動提示操作
- 解決Spring boot整合mybatis,xml資源文件放置及路徑配置問題
- Mybatis中的config.xml配置文件詳細(xì)解析
- 基于Mybatis實(shí)現(xiàn)CRUD操作過程解析(xml方式)
- MybatisPlus,無XML分分鐘實(shí)現(xiàn)CRUD操作
相關(guān)文章
Java中的StopWatch計(jì)時(shí)利器使用指南
StopWatch通常用于測量一段代碼執(zhí)行所花費(fèi)的時(shí)間,它能夠精確地記錄開始時(shí)間、結(jié)束時(shí)間,并計(jì)算出這中間的時(shí)間差,下面給大家介紹Java中的StopWatch計(jì)時(shí)利器的深度解析與使用指南,感興趣的朋友一起看看吧2025-05-05
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ù)庫,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
背包問題-動態(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),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

