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

Mybatis XML配置文件操作数据库

        Mybaits在操作数据库时,可以有两种方式;第一种是使用注解的方式操作,另一种是使用XML配置文件的方式:一般而言,若没有特别的要求,则编写一些简单的SQL语句,可以直接使用注解的方式;编写一些复杂的SQL语句则需要使用XML注解的方式

        用XML配置文件操作数据库,完成增删改查。

        MybatisMapper

package com.wzb.MybatisExercise20240926;import com.wzb.Pojo20240926.Emp;
import org.apache.ibatis.annotations.Mapper;import java.time.LocalDate;
import java.util.List;@Mapper
public interface MybatisMapper {// 增public void insertEmp(Emp emp);// 删public void deleteEmp(List<Integer> ids);// 改public void updateEmp(Emp emp);// 查public List<Emp> selectEmp(String name, short gender, LocalDate begin, LocalDate end);}

        Pojo

package com.wzb.Pojo20240926;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.time.LocalDate;
import java.time.LocalDateTime;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {private Integer id;private String username;private String password;private String name;private Short gender;private String image;private Short job;private LocalDate entrydate; //LocalDate类型对应数据表中的date类型private Integer deptId;private LocalDateTime createTime; //LocalDateTime类型对应数据表中的datetime类型private LocalDateTime updateTime;
}

        XML配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wzb.MybatisExercise20240926.MybatisMapper"><!--全类名就是全部包名 + 类名--><!--XML映射文件中的SQL语句的id必须和Mapper接口中的方法名完全一致,并且保持返回值一致--><!--完成条件查询--><!--    <select id = "selectEmp" resultType="com.wzb.Pojo20240926.Emp">--><!--        select * from emp--><!--        where name like concat('%', #{name}, '%')--><!--        and gender = #{gender}--><!--        and entrydate between #{begin} and #{end}--><!--        order by update_time desc--><!--    </select>--><!-- 但是有一个弊端,可见中间都是and连接的,那么当后面的参数(如gender)都是null的时候,即使传递了前面的参数(如name)也不会有查询结果,因为不管传递参数没有,都会拼接进SQL语句,这不符合业务逻辑————传递了参数,才进行拼接;若没有参数,则不拼接 --><!-- 增 --><insert id="insertEmp">insert into emp (username, name, gender, image, job, entrydate, dept_id, create_time, update_time)values<trim prefix="(" suffix=")" suffixOverrides=","><if test="username != null">#{username},</if><if test="name != null">#{name},</if><if test="gender != -1">#{gender},</if><if test="image != null">#{image},</if><if test="job != null">#{job},</if><if test="entrydate != null">#{entrydate},</if><if test="deptId != -1">#{deptId},</if>#{createTime}, #{updateTime}</trim></insert><!-- 删 --><delete id="deleteEmp">delete from emp where id in<foreach collection="ids" item="id" separator="," open="(" close=")">#{id}</foreach></delete><!-- 改 --><update id="updateEmp">update emp<set><if test="username != null">username=#{username},</if><if test="name != null">name=#{name},</if><if test="gender != -1">gender=#{gender},</if><if test="image != null">image=#{image},</if><if test="job != null">job=#{job},</if><if test="entrydate != null">entrydate=#{entrydate},</if><if test="deptId != null">dept_id=#{deptId},</if><if test="updateTime != null">update_time=#{updateTime}</if></set>where id=#{id}</update><select id="selectEmp" resultType="com.wzb.Pojo20240926.Emp">select * from emp<where><if test="name != null">name like concat('%', #{name}, '%')</if><if test="gender!=-1">and gender=#{gender}</if><if test="begin!=null and end!=null">and entrydate between begin and end</if></where>order by entrydate desc</select></mapper>

        SpringbootTest

package com.wzb;import com.wzb.MybatisExercise20240926.MybatisMapper;
import com.wzb.Pojo20240926.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;@SpringBootTest
class SpringbootExercise20240926ApplicationTests {@Autowiredprivate MybatisMapper mybatisMapper;//增@Testpublic void insertEmp() {Emp emp = new Emp(30, "leiyi", "123456", "雷伊", (short) 1, "3.jpg",(short) 1, LocalDate.of(2000, 1, 1),2, LocalDateTime.now(), LocalDateTime.now());mybatisMapper.insertEmp(emp);}// 删@Testpublic void deleteEmp() {List<Integer> ids = new ArrayList<>();Collections.addAll(ids, 30);mybatisMapper.deleteEmp(ids);}// 改@Testpublic void updateEmp() {Emp emp = new Emp();emp.setUsername("gaiya");emp.setName("盖亚");emp.setGender((short)1);emp.setUpdateTime(LocalDateTime.now());emp.setId(30);mybatisMapper.updateEmp(emp);}// 查@Testpublic void selectEmp() {List<Emp> empList = mybatisMapper.selectEmp(null, (short)-1, null, null);for (Emp emp : empList) {System.out.println(emp);}}}

 

         

 

 

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Python办公自动化之Excel
  • npm run serve报错提示js堆内存不足
  • Azure Kinect 人体跟踪关节
  • 【数据评估与清洗】对数据结构和内容进行清洗
  • SpringBoot 与 Maven 快速上手指南
  • 鹅​文​档​一​面​​
  • Python--循环
  • fastdfs开源分布式文件系统及迁移部署
  • 小学全科电子教材(全阶段全版本)
  • 回归预测|基于卷积神经网络-支持向量机的数据回归预测Matlab程序CNN-SVM 卷积提取特征与原始特征进行融合预测
  • ubuntu命令行设置wifi和宽带连接
  • 如何使用GitHub Desktop管理GitLab库
  • React Native中如何调用iOS的Face ID和Android的生物识别,react-native-biometrics
  • transformer中为什么使用不同的K 和 Q, 为什么不能使用同一个值?
  • SpringBoot-MybatisPlus项目中,在控制台查看sql执行日志的方法
  • JS中 map, filter, some, every, forEach, for in, for of 用法总结
  • 【跃迁之路】【733天】程序员高效学习方法论探索系列(实验阶段490-2019.2.23)...
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • Cumulo 的 ClojureScript 模块已经成型
  • ECMAScript 6 学习之路 ( 四 ) String 字符串扩展
  • Git 使用集
  • GitUp, 你不可错过的秀外慧中的git工具
  • JavaScript 基础知识 - 入门篇(一)
  • JavaScript设计模式系列一:工厂模式
  • Python利用正则抓取网页内容保存到本地
  • SQLServer之创建数据库快照
  • Tornado学习笔记(1)
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • vue总结
  • 闭包,sync使用细节
  • 从0搭建SpringBoot的HelloWorld -- Java版本
  • 前端每日实战:61# 视频演示如何用纯 CSS 创作一只咖啡壶
  • 想写好前端,先练好内功
  • 学习使用ExpressJS 4.0中的新Router
  • 自制字幕遮挡器
  • 湖北分布式智能数据采集方法有哪些?
  • ​iOS安全加固方法及实现
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • (~_~)
  • (C语言)strcpy与strcpy详解,与模拟实现
  • (MTK)java文件添加简单接口并配置相应的SELinux avc 权限笔记2
  • (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)讲解
  • (笔试题)分解质因式
  • (顺序)容器的好伴侣 --- 容器适配器
  • (转)chrome浏览器收藏夹(书签)的导出与导入
  • (轉貼) 蒼井そら挑戰筋肉擂台 (Misc)
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • (总结)Linux下的暴力密码在线破解工具Hydra详解
  • **python多态
  • *setTimeout实现text输入在用户停顿时才调用事件!*
  • .bat批处理(一):@echo off
  • .NET Standard / dotnet-core / net472 —— .NET 究竟应该如何大小写?
  • .NET 回调、接口回调、 委托
  • .net遍历html中全部的中文,ASP.NET中遍历页面的所有button控件
  • .NET高级面试指南专题十一【 设计模式介绍,为什么要用设计模式】