Spring -- 使用XML开发MyBatis


T04BF
👋专栏: 算法|JAVA|MySQL|C语言
🫵 今天你敲代码了吗

文章目录

MyBatis XML配置文件开发

实际上,除了使用注解的方式,MyBatis开发还能使用XML的方式,也就是将SQL语句写在XML配置文件里面

配置连接字符串和MyBatis

yaml 复制代码
# 数据库连接配置
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false
username: root
password: '051215'
driver-class-name: com.mysql.cj.jdbc.Driver


# 配置 mybatis xml 的⽂件路径,在 resources/mapper 创建所有表的 xml ⽂件
mybatis:
mapper-locations: classpath:mapper/**Mapper.xml

写Mapper层代码

分为两个部分

  1. 方法定义Interface
  2. 方法实现:XXX.xml
添加mapper接口
java 复制代码
package org.jwcb.je0725_mybatis_xml.mapper;

import org.jwcb.je0725_mybatis_xml.entity.UserInfo;

import java.util.List;

public interface UserInfoMapper {
    List<UserInfo> queryAllUser(); 
}
添加UserInfoXmLMapper.xml

MyBatis固定xml格式:

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="org.jwcb.je0725_mybatis_xml.mapper.UserInfoMapper">
</mapper>

尝试一下执行select操作:

xml 复制代码
<mapper namespace="org.jwcb.je0725_mybatis_xml.mapper.UserInfoMapper">
  <select id="queryAllUser" resultType="org.jwcb.je0725_mybatis_xml.entity.UserInfo">
    select * from userinfo
  </select>
</mapper>

执行结果:

  • <mapper>标签:需要指定namespace属性,表示命名空间,值为mapper接口的**全限定类名, **包括全包名.类名
  • <select>查询标签:用来执行数据库的查询操作
    • id:是和interface中定义的方法名一致,表示对接口的具体实现方法
    • resultType:是返回的数据类型,也就是UserInfo

我们会发现,出现了返回的一些值为null的情况,这种情况与上一篇文章一致,是由于数据库列名和java实体类属性名不一致导致的

此时我们同样需要对起来

在XML中:

xml 复制代码
<mapper namespace="org.jwcb.je0725_mybatis_xml.mapper.UserInfoMapper">

  <resultMap id="XMLBaseMap" type="org.jwcb.je0725_mybatis_xml.entity.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="queryAllUser2" resultMap="XMLBaseMap">
    select * from userinfo
  </select>
</mapper>

此时的执行结果就是正确的了

建议将其余的,即使命名一致,也都写上对应关系

同时,上一篇文章提到的起别名/开启驼峰命名一样适用

操作数据库

INSERT
xml 复制代码
<insert id="insertUser">
    insert into userinfo (username,`password`,age,gender,phone) values (#{userinfo.username},
    #{userinfo.password},#{userinfo.age},#{userinfo.gender},#{userinfo.phone})
</insert>

在接口中:

java 复制代码
@Mapper
public interface UserInfoMapper {
    Integer insertUser(@Param("userinfo") UserInfo userInfo);
}

测试用例:

返回自增ID

XML文件设置 useGeneratedKeys 和 keyProperty 属性

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

此时返回值直接在userinfo.id里面取就行了

DELETE & UPDATE
xml 复制代码
<delete id="deleteUser">
  delete from userinfo where id = #{id}
</delete>


<update id="updateUser">
  update userinfo set phone = #{phone} where id = #{id}
</update>
java 复制代码
Integer updateUser(UserInfo userInfo);
Integer deleteUser(UserInfo userInfo);



感谢您的访问!!期待您的关注!!!


T04BF
🫵 今天记得敲代码

相关推荐
大家都说我身材好20 分钟前
Spring缓存注解深度实战:3大核心注解解锁高并发系统性能优化‌
spring·缓存·性能优化
都叫我大帅哥34 分钟前
Spring AI中的ChatClient:从入门到精通,一篇搞定!
java·spring·ai编程
都叫我大帅哥35 分钟前
《@SpringBootApplication:Spring Boot的"一键启动"按钮,还是程序员的"免死金牌"?》
java·后端·spring
-曾牛1 小时前
Spring AI 快速入门:从环境搭建到核心组件集成
java·人工智能·spring·ai·大模型·spring ai·开发环境搭建
啊松同学1 小时前
【Mybatis】MyBatisPlus的saveBatch真的是批量插入吗?深度解析与性能优化
java·后端·性能优化·mybatis
阿里小阿希1 小时前
解决 Spring Boot + MyBatis 项目迁移到 PostgreSQL 后的数据类型不匹配问题
spring boot·postgresql·mybatis
給妳一生緈諨3 小时前
11.AOP开发
java·spring boot·spring·springboot3
HanhahnaH3 小时前
Spring集合注入Bean
java·spring
.生产的驴5 小时前
SpringBoot 封装统一API返回格式对象 标准化开发 请求封装 统一格式处理
java·数据库·spring boot·后端·spring·eclipse·maven
时间之城5 小时前
笔记:记一次使用EasyExcel重写convertToExcelData方法无法读取@ExcelDictFormat注解的问题(已解决)
java·spring boot·笔记·spring·excel