Mybatis?XML配置文件實現(xiàn)增刪改查的示例代碼
一、環(huá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> 標簽:需要指定 namespace 屬性,表?命名空間,值為 UserMapperXML 接?的全限定名,包括全包名.類名。
- <select> 查詢標簽:是?來執(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 = >
使用標簽< 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 >標簽來寫入數(shù)據(jù),只不過設(shè)置useGeneratedKeys 和keyProperty屬性 。
- useGeneratedKeys:這會令 MyBatis 使? JDBC 的 getGeneratedKeys ?法來取出由數(shù)據(jù)庫內(nèi)部?成的主鍵(?如:像 MySQL 和 SQL Server 這樣的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)的?動遞增字段),默認值:false.
- keyProperty:指定能夠唯?識別對象的屬性,MyBatis 會使? getGeneratedKeys 的返回值或 insert 語句的 selectKey ?元素設(shè)置它的值,默認值:未設(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 >標簽,加上刪除的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 = >
查詢我們只需要使用標簽即可。
但是我們也會遇見像前面注解的時候因為字段名和變量名不同而導(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)換
- 使用標簽result和resultMap。在resultMap標簽中放入result標簽數(shù)組,result標簽的column屬性對應(yīng)數(shù)據(jù)庫字段,property屬性對應(yīng)類屬性名。當其他查詢語句需要使用相同的映射時,這需要在select標簽的resultMap屬性寫上resultMap標簽的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)文章
Java多線程并發(fā)synchronized?關(guān)鍵字
這篇文章主要介紹了Java多線程并發(fā)synchronized?關(guān)鍵字,Java?在虛擬機層面提供了?synchronized?關(guān)鍵字供開發(fā)者快速實現(xiàn)互斥同步的重量級鎖來保障線程安全。2022-06-06

