当前位置: 首页 > news >正文

【MyBatis】XML实现,配置方法和增、删、改、查

文章目录

  • 背景介绍
  • 配置连接字符串和 MyBatis
  • 持久层代码
    • 添加 mapper 接口
    • 添加 UserInfoXmlMapper. xml
  • 增删改查操作
      • @Param 设置参数名
      • 返回自增 id

背景介绍

MyBatis 的开发有两种方式:

  1. 注解
  2. XML

使用 MyBatis 注解的方式,主要是来完成一些简单的增删改查功能。如果需要实现复杂的 SQL,建议使用 XML 来配置映射语句,也就是将 SQL 语句写在 XML 配置文件中

MyBatis XML 的方式需要以下两步:

  1. 配置数据库连接字符串和 MyBatis
  2. 写持久层代码

配置连接字符串和 MyBatis

此步骤需要进行两项测试,数据库连接字符串设置和 MyBatisXML 配置

  • 如果是 application.yml 文件,配置内容如下
mybatis:  mapper-locations: classpath:mybatis/**Mapper.xml  configuration: # 配置打印 MyBatis⽇志  log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  
#    map-underscore-to-camel-case: true  # 开启驼峰自动转换

配置好之后,这个这个 static 目录下的这个文件名,要和 xml 里面的对应上image.png

持久层代码

持久层代码分两部分:

  1. 方法定义:Interface
  2. 方法实现:XXX.xml

image.png

添加 mapper 接口

数据持久层的接口定义

package com.glg.mybatis.mapper;  import org.apache.ibatis.annotations.Mapper;  @Mapper  
public interface UserInfoXmlMapper {  }

添加 UserInfoXmlMapper. xml

static 里面的 mybatis 文件中,创建一个 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.glg.mybatis.mapper.UserInfoXmlMapper">  <insert id="select">select * from userinfo;</insert>
</mapper>
  • <mapper> 标签:需要指定 namespace 属性,表示命名空间,值为 mapper 接口的全限定名,包括全包名、类名image.png|290
  • <select> 查询标签:是用来执行数据库的查询操作的:
    • id:是和 Interface (接口) 中定义的方法名称一样的,表示对接口的具体实现方法
    • resultType:是返回的数据类型,也就是开头我们定义的实体类

增删改查操作

UserInfoMapper 接口:

Integer insert(UserInfo userInfo);

UserInfoMapper.xml 实现

<insert id="insert">  insert into userinfo (username, password, age, gender)  values (#{username}, #{password}, #{age}, #{gender});
</insert>

@Param 设置参数名

如果使用 @Param 设置参数名称的话,使用方法和注解类似
UserInfoMapper 接口:

Integer insert(@Param("userinfo") UserInfo userInfo);

UserInfoMapper.xml 实现:

<insert id="insert">  insert into userinfo (username, password, age, gender)  values (#{userinfo.username}, #{userinfo.password}, #{userinfo.age}, #{userinfo.gender});
</insert>

返回自增 id

接口定义不变,Mapper.xml 实现设置 useGeneratedKeyskeyProperty 属性

<insert id="insert" useGeneratedKeys="true" keyProperty="id">  insert into userinfo (username, password, age, gender)  values (#{userinfo.username}, #{userinfo.password}, #{userinfo.age}, #{userinfo.gender});
</insert>

UserInfoMapper 接口

Integer delete(Integer id);

UserInfoMapper.xml 实现:

<delete id="delete">  delete from userinfo where id = #{id};  
</delete>

UserInfoMapper 接口:

Integer update(UserInfo userInfo);

UserInfoMapper.xml 实现:

<update id="update">  update userinfo set password = #{password} where id = #{id};  
</update>

同样的,使用 XML 的方式来进行查询,也存在数据封装的问题

  • 我们把 SQL 语句进行简单修改,查询更多的字段内容

UserInfoMapper 接口:

List<UserInfo> selectAllUser();

UserInfoMapper,xml 实现:

<select id="selectAllUser" resultType="com.glg.mybatis.module.UserInfo">  select * from userinfo  
</select>
  • resultType:查询需要加上这个

运行结果:image.png|393

结果显示:deleteFlagcreateTimeupdateTime 也没有进行赋值

解决方法和注解类似:

  1. 起别名
  2. 结果映射
  3. 开启驼峰命名
http://www.lryc.cn/news/582250.html

相关文章:

  • 第二届云计算与大数据国际学术会议(ICCBD 2025)
  • 物联网技术的关键技术与区块链发展趋势的深度融合分析
  • React Native 基础组件详解<一>
  • VSCODE创建JS项目
  • 常见问题与最佳实践——AI教你学Docker
  • 【力扣(LeetCode)】数据挖掘面试题0002:当面对实时数据流时您如何设计和实现机器学习模型?
  • EPLAN 电气制图:项目的创建(多功能天车系统案例)
  • 摄影后期:使用Photoshop进行暗角控制
  • 分布式生成 ID 策略的演进和最佳实践,含springBoot 实现(Java版本)
  • 【R语言】Can‘t subset elements that don‘t exist.
  • LastActivityView -查看电脑上的所有操作记录
  • 初识Neo4j之入门介绍(一)
  • 【Linux系统】Linux权限 | Shell命令以及运行原理
  • Python爬虫图片验证码和滑块验证码识别总结
  • Taro+Vue3实现微信小程序富文本编辑器组件开发指南
  • OpenCV人脸分析------绘制面部关键点函数drawFacemarks()
  • 虚幻引擎UE5 GAS开发RPG游戏-02 设置英雄角色-18 改成网络多人游戏
  • turborepo 如何解决git管理包过大的问题
  • 5、Receiving Messages:Message Listener Containers
  • Python实现文件夹中文件名与Excel中存在的文件名进行对比,并进行删除操作
  • 【无标题】三维拓扑量子色动力学模型:理论重构与实验验证
  • day16——Java集合进阶(Collection、List、Set)
  • windows安装python环境以及对应编辑器的详细流程
  • 从依赖地狱到依赖天堂PNPM
  • VmWare 安装 mac 虚拟机
  • 大模型在肾囊肿诊疗全流程预测及应用研究报告
  • 【保姆级喂饭教程】Git图形化客户端Sourcetree安装及使用教程
  • Linux系统从入门到精通!第四天(shell编程和Docker)
  • codeforces Round 1021-1030(部分题解)
  • 【Note】《Kafka: The Definitive Guide》第7章 Building Data Pipelines