使用XML实现MyBatis的基础操作

目录

前言

1.准备工作

1.1⽂件配置

[1.2添加 mapper 接⼝](#1.2添加 mapper 接⼝)

2.增删改查操作

2.1增(Insert)

2.2删(Delete)

2.3改(Update)

2.4查(Select)


前言

接下来我们会使用的数据表如下:

对应的实体类为:UserInfo

所有的准备工作都在如下文章。

MyBatis 操作数据库入门-CSDN博客文章浏览阅读568次,点赞11次,收藏24次。什么是MyBatis?MyBatis是⼀款优秀的 持久层 框架,⽤于简化JDBC的开发Mybatis操作数据库的入门步骤:1.创建springboot⼯程2.数据库表准备、实体类3.引⼊Mybatis的相关依赖,配置Mybatis(数据库连接信息)4.编写SQL语句(注解/XML) ,进行测试了解更多MyBatis中文网1.创建springboot⼯程创建springboot⼯程,并导⼊ mybatis的起步依赖、mysql的驱动包。https://blog.csdn.net/WHabc2002/article/details/142743762


1.准备工作

1.1⽂件配置

如果是application.yml⽂件, 配置内容如下:

复制代码
mybatis:
  mapper-locations: classpath:mybatis/**Mapper.xml

如果是application.properties⽂件, 配置内容如下:

复制代码
mybatis.mapper-locations=classpath:mapper/**Mapper.xml

按照mybatis.mapper-locations设置文件

起名无所谓最后以Mapper.xml结尾就行

1.2添加 mapper 接⼝

数据持久层的接⼝定义:(接口名自已决定)

复制代码
import com.wh.myBatis.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserInfoXmlMapper {
    List<UserInfo> selectAllUser();
}

1.3添加MyBatis 的固定 xml 格式

在mybatis.mapper-locations的路径下

加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="com.wh.myBatis.mapper.UserInfoXmlMapper">

</mapper>

路径一定要正确


2.增删改查操作

为了增加开发效率,我们可以使用插件。

2.1增(Insert)

复制代码
UserInfoXmlMapper接口
复制代码
Integer insert(UserInfo userInfo);

UserInfoXmlMapper.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.wh.myBatis.mapper.UserInfoXmlMapper">
    <insert id="insert">
        insert into userinfo (username, password, age, gender)
        values (#{username},#{password},#{age},#{gender})
    </insert>
</mapper>

如果使⽤@Param设置参数名称的话, 使⽤⽅法和注解类似
返回⾃增 id
接⼝定义不变, UserInfoXmlMapper.xml 实现 设置useGeneratedKeys 和keyProperty属性

复制代码
    <insert id="insert2" useGeneratedKeys="true" keyProperty="id">
        insert into userinfo (username, password, age, gender)
        values (#{username},#{password},#{age},#{gender})
    </insert>

2.2删(Delete)

UserInfoXmlMapper接口

复制代码
Integer delete(Integer id);

UserInfoXmlMapper.xml的实现

复制代码
<delete id="delete">
        delete from userinfo where id = #{id}
    </delete>

2.3改(Update)

UserInfoXmlMapper接口

复制代码
Integer update(UserInfo userInfo);

UserInfoXmlMapper.xml的实现

复制代码
  <update id="update">
        update userinfo set username=#{username} where id=#{id}
    </update>

2.4查(Select)

UserInfoXmlMapper接口

复制代码
List<UserInfo> selectAllUser();

UserInfoXmlMapper.xml的实现

复制代码
    <select id="selectAllUser" resultType="com.wh.myBatis.model.UserInfo">
        select * from userinfo
    </select>

MyBatis 会获取结果中返回的列名并在 Java 类中查找相同名字的属性

小驼峰与蛇形的结果映射

UserInfoXmlMapper.xml的实现

复制代码
    <resultMap id="BaseMap" type="com.wh.myBatis.model.UserInfo">
        <id column="id" property="id"></id>
        <result column="delete_flag" property="deleteFlag"></result>
        <result column="create_time" property="createTime"></result>
        <result column="update_time" property="updateTime"></result>
    </resultMap>
    <select id="selectAllUser2" resultMap="BaseMap">
        select * from userinfo
    </select>

以上为我个人的小分享,如有问题,欢迎讨论!!!

都看到这了,不如关注一下,给个免费的赞

相关推荐
逸Y 仙X8 分钟前
文章十二:索引数据的写入和删除
java·大数据·spring boot·spring·elasticsearch·搜索引擎·全文检索
代码探秘者12 分钟前
【算法篇】5.链表
java·数据结构·人工智能·python·算法·spring·链表
1104.北光c°13 分钟前
Leetcode3.无重复字符的最长子串 HashSet+HashMap 【hot100算法个人笔记】【java写法】
java·开发语言·笔记·程序人生·算法·leetcode·滑动窗口
Binary-Jeff14 分钟前
Maven 依赖作用域详解:compile、provided、runtime、test
java·spring·spring cloud·servlet·java-ee·maven
QH_ShareHub16 分钟前
Rstudio 与 R 打开 Rdata (压缩文件) 差异
java·前端·r语言
spencer_tseng16 分钟前
apache-maven-3.9.6
java·maven
yashuk21 分钟前
springboot与springcloud对应版本
java·spring boot·spring cloud
strayCat2325522 分钟前
4. Spring Boot 数据持久化(JPA)
java·spring boot·后端
无风听海23 分钟前
LangGraph Thread 数据清理总结
java·开发语言·jvm·langchain·deep agents
strayCat2325523 分钟前
2. Spring Boot 自动配置原理深度解析
java·spring boot·后端