MyBatis 的XML实现方法

MyBatis 的XML实现方法

MyBatis 的XML实现方法

前情提示

关于mybatis的重要准备工作,请看MyBatis 的注解实现方法

创建mapper接口

java 复制代码
package com.example.demo.mapper;

import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserInfoXMLMapper {
    List<UserInfo> selectAll();
    List<UserInfo> selectAll2();
    Integer insert(UserInfo userInfo);
    Integer DeleteByID(Integer id);
    Integer Update(UserInfo userInfo);
    List<UserInfo> selectAllBYOrder(String sort);
}

添加配置

java 复制代码
mybatis:
  configuration:
    map-underscore-to-camel-case: true
  mapper-locations: classpath:mapper/**Mapper.xml

mapper-locations: classpath:mapper/**Mapper.xml中的mapper表示resources下的包,Mapper.xml表示mapper下的以Mapper.xml结尾的文件

创建xml文件

resources下创建mapper包,在mapper下创建以Mapper.xml结尾的文件

创建完成之后,在文件中,添加一些代码

java 复制代码
<?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.demo.mapper.UserInfoXMLMapper">
</mapper>

图中需要填写的是之前创建的mapper接口的全限定类名加方法名

操作数据库

xml中的标签对应前面mapper接口中定义的方法

insert标签

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

delete标签

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

select标签

java 复制代码
    <select id="selectAll" resultType="com.example.demo.model.UserInfo">
        select * from userinfo
    </select>

这里的resultType也是全限定类名

这里也存在注解中存在的问题,数据格式不同

使用下面的标签即可解决

resultMap标签

java 复制代码
    <resultMap id="xmlBaseMap" type="com.example.demo.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="selectAll2"  resultMap="xmlBaseMap">
        select * from userinfo
    </select>

update标签

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

sql标签,include标签

将一些重复率过高的代码通过sql标签包裹起来

使用时通过include来调用

java 复制代码
    <sql id="baseSelect">
        select * from userinfo
    </sql>
    <select id="selectAllBYOrder" resultMap="xmlBaseMap">
        <include refid="baseSelect"></include>
        order by id ${sort}
    </select>
相关推荐
Amagi.1 小时前
Redis的内存淘汰策略
数据库·redis·mybatis
执键行天涯6 小时前
【经验帖】JAVA中同方法,两次调用Mybatis,一次更新,一次查询,同一事务,第一次修改对第二次的可见性如何
java·数据库·mybatis
工业甲酰苯胺6 小时前
Spring Boot 整合 MyBatis 的详细步骤(两种方式)
spring boot·后端·mybatis
ggdpzhk8 小时前
Mybatis 快速入门(maven)
oracle·maven·mybatis
Java小白笔记18 小时前
关于使用Mybatis-Plus 自动填充功能失效问题
spring boot·后端·mybatis
计算机学姐1 天前
基于SpringBoot+Vue的篮球馆会员信息管理系统
java·vue.js·spring boot·后端·mysql·spring·mybatis
程序员大金1 天前
基于SpringBoot+Vue+MySQL的智能物流管理系统
java·javascript·vue.js·spring boot·后端·mysql·mybatis
惜.己1 天前
MyBatis中一对多关系的两种处理方法
java·开发语言·后端·sql·mysql·mybatis·idea
终末圆1 天前
MyBatis动态SQL中的`if`标签使用【后端 19】
java·数据结构·数据库·sql·算法·spring·mybatis
飞翔的佩奇1 天前
Java项目: 基于SpringBoot+mybatis+maven医院管理系统(含源码+数据库+任务书+开题报告+毕业论文)
java·数据库·spring boot·毕业设计·maven·mybatis·医院管理系统