蔚县网站建设wl17581,百度热门关键词排名,化妆品网页设计模板,离线wordpressJava中的XML映射文件主要用于将Java对象与XML文档之间进行转换。它通常用于处理数据交换和存储#xff0c;例如将Java对象转换为XML格式以便在网络上传输或保存到文件中#xff0c;或者将XML文档解析为Java对象以进行处理。这种转换可以通过Java的JAXB#xff08;Java Archi…Java中的XML映射文件主要用于将Java对象与XML文档之间进行转换。它通常用于处理数据交换和存储例如将Java对象转换为XML格式以便在网络上传输或保存到文件中或者将XML文档解析为Java对象以进行处理。这种转换可以通过Java的JAXBJava 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 ListEmp list(String name, Short gender, LocalDate start , LocalDate end);
}
xml映射文件
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.itheima.mapper.EmpMapperselect idlist resultTypecom.itheima.pojo.Empselect *from empwhereif testname!nullname like concat(%,#{name},%)/ifif testgender ! nulland gender #{gender}/ifif teststart!null and end!nulland entrydate between #{start} and #{end}/iforder 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(){ListEmp empList empMapper.list(张, (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
// ListEmp empList empMapper.list(张, null, null, null);System.out.println(empList);}
}
目录等级 其实以上XML,文件还有一个小bug如果查询的姓名为null此时编译会报错 观看控制台发现确实SQL语句编译后多了and因此使用where标签可以有效的解决这个问题set标签可以删除多余的 ,
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.itheima.mapper.EmpMapperselect idlist resultTypecom.itheima.pojo.Empselect *from empwhereif testname!nullname like concat(%,#{name},%)/ifif testgender ! nulland gender #{gender}/ifif teststart!null and end!nulland entrydate between #{start} and #{end}/if/whereorder by update_time desc/select/mapper