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>