柚子快報邀請碼778899分享:MyBatis:XML操作
柚子快報邀請碼778899分享:MyBatis:XML操作
?專欄內(nèi)容:MyBatis?個人主頁:子夜的星的主頁?座右銘:前路未遠(yuǎn),步履不停
目錄
一、MyBatis XML方式1、配置數(shù)據(jù)庫2、指明XML路徑3、寫持久層代碼
二、基礎(chǔ)操作1、新增2、刪除3、更新4、查找Ⅰ、開啟駝峰命名Ⅱ、結(jié)果映射
一、MyBatis XML方式
使?Mybatis的注解?式,主要是來完成?些簡單的增刪改查功能。如果需要實現(xiàn)復(fù)雜的SQL功能,建議使?XML來配置映射語句,也就是將SQL語句寫在XML配置?件中。
MyBatis XML的方式需要以下兩步:
配置數(shù)據(jù)庫連接字符串指明XML的路徑寫持久層代碼
1、配置數(shù)據(jù)庫
和前面配置數(shù)據(jù)庫的方法一樣。
如果是application.yml文件,則配置如下內(nèi)容:
spring:
datasource:
url: jdbc:mysql://localhost:3306/mybatis_test?useSSL=false&allowPublicKeyRetrieval=true
username: root # 數(shù)據(jù)庫用戶名
password: 11111 # 數(shù)據(jù)庫密碼
driver-class-name: com.mysql.cj.jdbc.Driver
如果是application.properties文件,則配置如下內(nèi)容:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/數(shù)據(jù)庫名?characterEncoding=utf8&useSSL=false
# 數(shù)據(jù)庫名稱
spring.datasource.username=root
# 數(shù)據(jù)庫密碼
spring.datasource.password=11111
2、指明XML路徑
如果是application.yml?件,配置內(nèi)容如下:
mybatis:
mapper-locations: classpath:mapper/**Mapper.xml
如果是application.properties?件,配置內(nèi)容如下:
# 配置 mybatis xml 的?件路徑,在 resources/mapper 創(chuàng)建所有表的 xml ?件
mybatis.mapper-locations=classpath:mapper/**Mapper.xml
在mapper文件夾內(nèi),新建XXXMapper.xml文件
在該文件內(nèi)寫入MyBatis 的固定 xml 格式:
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
這里通常與 Mapper 接口的完全限定名相匹配。這樣 MyBatis 就能夠?qū)⒔涌谥械姆椒ㄅc映射器文件中的 SQL 語句關(guān)聯(lián)起來。
3、寫持久層代碼
持久層代碼分兩部分
?法定義 Interface?法實現(xiàn): XXXMapper.xml
新建一個接口:UserInfoXMlMapper
@Mapper
public interface UserInfoXMLMapper {
List
}
新建UserInfoXMLMapper用于實現(xiàn)該接口的方法。
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from userinfo
二、基礎(chǔ)操作
1、新增
UserInfoXMLMapper接口:
@Mapper
public interface UserInfoXMLMapper {
Integer insertUser(UserInfo userInfo);
}
UserInfoXMLMapper.xml進行實現(xiàn):
insert into userinfo (username, password, age, gender, phone)
values (#{username}, #{password}, #{age},#{gender},#{phone})
對應(yīng)的單元測試:
@Autowired
private UserInfoXMLMapper userInfoXMLMapper;
@Test
void insertUser() {
UserInfo userInfo = new UserInfo();
userInfo.setUsername("lisi");
userInfo.setPassword("123456");
userInfo.setAge(20);
userInfo.setPhone("12345678901");
userInfo.setGender(1);
Integer result = userInfoXMLMapper.insertUser(userInfo);
}
獲取插入數(shù)據(jù)的自增ID:接口定義不變,Mapper.xml 實現(xiàn)設(shè)置useGeneratedKeys 和keyProperty屬性:
insert into userinfo (username, password, age, gender, phone)
values (#{username}, #{password}, #{age},#{gender},#{phone})
2、刪除
UserInfoXMLMapper接口:
@Mapper
public interface UserInfoXMLMapper {
Integer deleteUser(Integer id);
}
UserInfoXMLMapper.xml進行實現(xiàn):
delete from userinfo where id = #{id}
3、更新
UserInfoXMLMapper接口:
@Mapper
public interface UserInfoXMLMapper {
Integer updateUser(UserInfo userInfo);
}
UserInfoXMLMapper.xml進行實現(xiàn):
update userinfo set username=#{username} where id=#{id}
4、查找
同樣的, 使?XML 的?式進?查詢, 也存在數(shù)據(jù)封裝的問題。解決辦法和注解類似:使用結(jié)果映射或者開啟駝峰命名。
Ⅰ、開啟駝峰命名
和前面一樣。在application.yml文件中加入下方配置:
mybatis:
configuration:
map-underscore-to-camel-case: true
Ⅱ、結(jié)果映射
select id, username, `password`, age, gender, phone, delete_flag,
create_time, update_time from userinfo
柚子快報邀請碼778899分享:MyBatis:XML操作
推薦閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。