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

Java中xml映射文件是干什么的

Java中的XML映射文件主要用于将Java对象与XML文档之间进行转换。它通常用于处理数据交换和存储,例如将Java对象转换为XML格式以便在网络上传输或保存到文件中,或者将XML文档解析为Java对象以进行处理。这种转换可以通过Java的JAXB(Java Architecture for XML Binding)库来实现。

如何实现XML映射实现对数据库的增删改,以及动态查询操作:

实体类对象: 

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {private Integer id; //IDprivate String username; //用户名private String password; //密码private String name; //姓名private Short gender; //性别, 1 男, 2 女private String image; //图像urlprivate Short job; //职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管, 5 咨询师'private LocalDate entrydate; //入职日期private Integer deptId; //部门IDprivate LocalDateTime createTime; //创建时间private LocalDateTime updateTime; //修改时间
}
package com.itheima.mapper;import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;
import org.yaml.snakeyaml.events.Event;import java.time.LocalDate;
import java.util.List;@Mapper
public interface EmpMapper {//根据ID删除数据@Delete("delete from emp where id = #{id}")public void delete(Integer id);//public int delete(Integer id);//新增员工@Options(useGeneratedKeys = true, keyProperty = "id")@Insert("insert into emp(username,name,gender,image,job,entrydate,dept_id,create_time,update_time) " +"values(#{username},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime})")public void insert(Emp emp);//更新数据@Update("update emp set username=#{username},gender=#{gender},name=#{name},image=#{image},job=#{job}" +",entrydate=#{entrydate},dept_id=#{deptId},update_time=#{updateTime} where id = #{id} ")public void update(Emp emp);//根据Id查询数据@Select("select * from emp where id = ${id} ")public Emp getById(Integer id);//动态查询sql语句public List<Emp> list(String name, Short gender, LocalDate start , LocalDate end);
}

xml映射文件: 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper"><select id="list" resultType="com.itheima.pojo.Emp">select *from empwhere<if test="name!=null">name like concat('%',#{name},'%')</if><if test="gender != null">and gender = #{gender}</if><if test="start!=null and end!=null">and entrydate between #{start} and #{end}</if>order by update_time desc</select></mapper>

设置启动类进行测试:

package com.itheima;import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.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.Arrays;
import java.util.List;@SpringBootTest
class SpringbootMybatisCrudApplicationTests {//    @Autowired
//    private EmpMapper empMapper;
//    @Test
//    public void testDelete(){
//        empMapper.delete(17);
//    }@Autowiredprivate EmpMapper empMapper;//根据ID删除@Testpublic void testDelete(){
//        int delete = empMapper.delete(17);
//        System.out.println(delete);empMapper.delete(16);}@Test//增加员工public void testInsert(){//构造员工对象Emp emp = new Emp();emp.setUsername("Tom2");emp.setName("汤姆3");emp.setGender((short)1);emp.setImage("1.jpg");emp.setJob((short)1);emp.setEntrydate(LocalDate.of(2000, 1, 1));emp.setCreateTime(LocalDateTime.now());emp.setUpdateTime(LocalDateTime.now());emp.setDeptId(1);//执行添加员工的操作empMapper.insert(emp);System.out.println(emp.getId());}//    更新员工信息@Testpublic void testUpdate(){//构造员工对象Emp emp = new Emp();emp.setId(18);emp.setUsername("Tom1");emp.setName("汤姆12");emp.setGender((short)1);emp.setImage("1.jpg");emp.setJob((short)1);emp.setEntrydate(LocalDate.of(2000, 1, 1));emp.setUpdateTime(LocalDateTime.now());emp.setDeptId(1);empMapper.update(emp);System.out.println(emp.getId());}//根据id查询员工信息@Testpublic void testGetById(){Emp emp = empMapper.getById(18);System.out.println(emp);}//根据条件查询员工信息@Testpublic void testList(){List<Emp> empList = empMapper.list("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
//        List<Emp> empList = empMapper.list("张", null, null, null);System.out.println(empList);}
}

目录等级:

其实以上XML,文件还有一个小bug,如果查询的姓名为null,此时编译会报错:

 

观看控制台发现,确实SQL语句编译后多了,and,因此使用<where>标签可以有效的解决这个问题:(<set>标签可以删除多余的 ',')

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper"><select id="list" resultType="com.itheima.pojo.Emp">select *from emp<where><if test="name!=null">name like concat('%',#{name},'%')</if><if test="gender != null">and gender = #{gender}</if><if test="start!=null and end!=null">and entrydate between #{start} and #{end}</if></where>order by update_time desc</select></mapper>

相关文章:

  • 开闭原则:提高扩展性的小技巧
  • 计算机视觉面试题-03
  • LeetCode算法题解(动态规划,背包问题)|LeetCode416. 分割等和子集
  • 【Spring之AOP底层源码解析】
  • vue 表格虚拟滚动
  • Web3之L2 ZK-Rollup 方案-StarkNet
  • 【Lustre相关】功能实践-01-Lustre集群部署配置
  • 鸿蒙4.0开发笔记之ArkTS装饰器语法基础@Extend扩展组件样式与stateStyles多态样式(十一)
  • FastDFS+Nginx - 本地搭建文件服务器同时实现在外远程访问「内网穿透」
  • 数据结构学习笔记——二叉树的遍历和链式存储代码实现二叉树
  • qt 容器QStringList的常见使用
  • 2023.11.27 使用anoconda搭建tensorflow环境
  • 计算机网络(二)
  • SpringBoot——Spring Security 框架
  • Linux文件查看命令
  • [微信小程序] 使用ES6特性Class后出现编译异常
  • 《深入 React 技术栈》
  • 【跃迁之路】【733天】程序员高效学习方法论探索系列(实验阶段490-2019.2.23)...
  • css系列之关于字体的事
  • Java方法详解
  • Java-详解HashMap
  • PaddlePaddle-GitHub的正确打开姿势
  • React 快速上手 - 07 前端路由 react-router
  • RxJS 实现摩斯密码(Morse) 【内附脑图】
  • seaborn 安装成功 + ImportError: DLL load failed: 找不到指定的模块 问题解决
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 关于 Linux 进程的 UID、EUID、GID 和 EGID
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 面试遇到的一些题
  • 目录与文件属性:编写ls
  • 写代码的正确姿势
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • #中国IT界的第一本漂流日记 传递IT正能量# 【分享得“IT漂友”勋章】
  • (C语言)求出1,2,5三个数不同个数组合为100的组合个数
  • (HAL库版)freeRTOS移植STMF103
  • (pojstep1.1.2)2654(直叙式模拟)
  • (ros//EnvironmentVariables)ros环境变量
  • (附源码)springboot家庭财务分析系统 毕业设计641323
  • (附源码)springboot家庭装修管理系统 毕业设计 613205
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理 第13章 项目资源管理(七)
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • (一)Dubbo快速入门、介绍、使用
  • (一)python发送HTTP 请求的两种方式(get和post )
  • (一)WLAN定义和基本架构转
  • (转)PlayerPrefs在Windows下存到哪里去了?
  • .naturalWidth 和naturalHeight属性,
  • .NET分布式缓存Memcached从入门到实战
  • @RequestBody的使用
  • [ Linux Audio 篇 ] 音频开发入门基础知识
  • [ vulhub漏洞复现篇 ] AppWeb认证绕过漏洞(CVE-2018-8715)
  • [100天算法】-实现 strStr()(day 52)
  • [20150629]简单的加密连接.txt
  • [ABP实战开源项目]---ABP实时服务-通知系统.发布模式
  • [CareerCup] 12.3 Test Move Method in a Chess Game 测试象棋游戏中的移动方法
  • [Dxperience.8.*]报表预览控件PrintControl设置