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; //ID
private String username; //用户名
private String password; //密码
private String name; //姓名
private Short gender; //性别, 1 男, 2 女
private String image; //图像url
private Short job; //职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管, 5 咨询师'
private LocalDate entrydate; //入职日期
private Integer deptId; //部门ID
private 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 list(String name, Short gender, LocalDate start , LocalDate end);
}
xml映射文件:
设置启动类进行测试:
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);
// }
@Autowired
private EmpMapper empMapper;
//根据ID删除
@Test
public 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());
}
// 更新员工信息
@Test
public 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查询员工信息
@Test
public void testGetById(){
Emp emp = empMapper.getById(18);
System.out.println(emp);
}
//根据条件查询员工信息
@Test
public void testList(){
List empList = empMapper.list("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
// List empList = empMapper.list("张", null, null, null);
System.out.println(empList);
}
}
目录等级:
其实以上XML,文件还有一个小bug,如果查询的姓名为null,此时编译会报错:
观看控制台发现,确实SQL语句编译后多了,and,因此使用