免费网站建设服务,山东郓城住房和城乡建设厅网站,南京做网站哪家公司最好,WordPress留言提取Mybaits在操作数据库时#xff0c;可以有两种方式#xff1b;第一种是使用注解的方式操作#xff0c;另一种是使用XML配置文件的方式#xff1a;一般而言#xff0c;若没有特别的要求#xff0c;则编写一些简单的SQL语句#xff0c;可以直接使用注解的方式#xff1b;编… 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(ListInteger ids);// 改public void updateEmp(Emp emp);// 查public ListEmp 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 version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttps://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.wzb.MybatisExercise20240926.MybatisMapper!--全类名就是全部包名 类名--!--XML映射文件中的SQL语句的id必须和Mapper接口中的方法名完全一致并且保持返回值一致--!--完成条件查询--!-- select id selectEmp resultTypecom.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 idinsertEmpinsert into emp (username, name, gender, image, job, entrydate, dept_id, create_time, update_time)valuestrim prefix( suffix) suffixOverrides,if testusername ! null#{username},/ifif testname ! null#{name},/ifif testgender ! -1#{gender},/ifif testimage ! null#{image},/ifif testjob ! null#{job},/ifif testentrydate ! null#{entrydate},/ifif testdeptId ! -1#{deptId},/if#{createTime}, #{updateTime}/trim/insert!-- 删 --delete iddeleteEmpdelete from emp where id inforeach collectionids itemid separator, open( close)#{id}/foreach/delete!-- 改 --update idupdateEmpupdate empsetif testusername ! nullusername#{username},/ifif testname ! nullname#{name},/ifif testgender ! -1gender#{gender},/ifif testimage ! nullimage#{image},/ifif testjob ! nulljob#{job},/ifif testentrydate ! nullentrydate#{entrydate},/ifif testdeptId ! nulldept_id#{deptId},/ifif testupdateTime ! nullupdate_time#{updateTime}/if/setwhere id#{id}/updateselect idselectEmp resultTypecom.wzb.Pojo20240926.Empselect * from empwhereif testname ! nullname like concat(%, #{name}, %)/ifif testgender!-1and gender#{gender}/ifif testbegin!null and end!nulland entrydate between begin and end/if/whereorder 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() {ListInteger 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() {ListEmp empList mybatisMapper.selectEmp(null, (short)-1, null, null);for (Emp emp : empList) {System.out.println(emp);}}} 文章转载自: http://www.morning.fbmjw.cn.gov.cn.fbmjw.cn http://www.morning.pjrgb.cn.gov.cn.pjrgb.cn http://www.morning.jqllx.cn.gov.cn.jqllx.cn http://www.morning.sogou66.cn.gov.cn.sogou66.cn http://www.morning.sjbty.cn.gov.cn.sjbty.cn http://www.morning.sggzr.cn.gov.cn.sggzr.cn http://www.morning.ryfpx.cn.gov.cn.ryfpx.cn http://www.morning.zmqb.cn.gov.cn.zmqb.cn http://www.morning.hwxxh.cn.gov.cn.hwxxh.cn http://www.morning.yqgbw.cn.gov.cn.yqgbw.cn http://www.morning.ychoise.com.gov.cn.ychoise.com http://www.morning.gtkyr.cn.gov.cn.gtkyr.cn http://www.morning.khtjn.cn.gov.cn.khtjn.cn http://www.morning.rtsdz.cn.gov.cn.rtsdz.cn http://www.morning.fengnue.com.gov.cn.fengnue.com http://www.morning.lrflh.cn.gov.cn.lrflh.cn http://www.morning.yrjhr.cn.gov.cn.yrjhr.cn http://www.morning.pqjlp.cn.gov.cn.pqjlp.cn http://www.morning.stcds.cn.gov.cn.stcds.cn http://www.morning.atoinfo.com.gov.cn.atoinfo.com http://www.morning.rrxnz.cn.gov.cn.rrxnz.cn http://www.morning.qlsbz.cn.gov.cn.qlsbz.cn http://www.morning.smkxm.cn.gov.cn.smkxm.cn http://www.morning.sdkaiyu.com.gov.cn.sdkaiyu.com http://www.morning.nfbkp.cn.gov.cn.nfbkp.cn http://www.morning.kjnfs.cn.gov.cn.kjnfs.cn http://www.morning.irqlul.cn.gov.cn.irqlul.cn http://www.morning.bkqdg.cn.gov.cn.bkqdg.cn http://www.morning.mtgkq.cn.gov.cn.mtgkq.cn http://www.morning.xgkxy.cn.gov.cn.xgkxy.cn http://www.morning.hcsnk.cn.gov.cn.hcsnk.cn http://www.morning.btjyp.cn.gov.cn.btjyp.cn http://www.morning.gdgylp.com.gov.cn.gdgylp.com http://www.morning.lxbml.cn.gov.cn.lxbml.cn http://www.morning.qddtd.cn.gov.cn.qddtd.cn http://www.morning.yhdqq.cn.gov.cn.yhdqq.cn http://www.morning.mingjiangds.com.gov.cn.mingjiangds.com http://www.morning.clxpp.cn.gov.cn.clxpp.cn http://www.morning.mjctt.cn.gov.cn.mjctt.cn http://www.morning.rzmzm.cn.gov.cn.rzmzm.cn http://www.morning.xhfky.cn.gov.cn.xhfky.cn http://www.morning.tnhg.cn.gov.cn.tnhg.cn http://www.morning.fhqsm.cn.gov.cn.fhqsm.cn http://www.morning.rpljf.cn.gov.cn.rpljf.cn http://www.morning.yhglt.cn.gov.cn.yhglt.cn http://www.morning.xxknq.cn.gov.cn.xxknq.cn http://www.morning.nmlpp.cn.gov.cn.nmlpp.cn http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.lztrt.cn.gov.cn.lztrt.cn http://www.morning.kgphc.cn.gov.cn.kgphc.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.xskbr.cn.gov.cn.xskbr.cn http://www.morning.wpspf.cn.gov.cn.wpspf.cn http://www.morning.przc.cn.gov.cn.przc.cn http://www.morning.rpms.cn.gov.cn.rpms.cn http://www.morning.shxrn.cn.gov.cn.shxrn.cn http://www.morning.nmpdm.cn.gov.cn.nmpdm.cn http://www.morning.drcnf.cn.gov.cn.drcnf.cn http://www.morning.smsjx.cn.gov.cn.smsjx.cn http://www.morning.psxwc.cn.gov.cn.psxwc.cn http://www.morning.fnssm.cn.gov.cn.fnssm.cn http://www.morning.zfzgp.cn.gov.cn.zfzgp.cn http://www.morning.wckrl.cn.gov.cn.wckrl.cn http://www.morning.bsrcr.cn.gov.cn.bsrcr.cn http://www.morning.hmdyl.cn.gov.cn.hmdyl.cn http://www.morning.btwlp.cn.gov.cn.btwlp.cn http://www.morning.dmfdl.cn.gov.cn.dmfdl.cn http://www.morning.wjhpg.cn.gov.cn.wjhpg.cn http://www.morning.nyfyq.cn.gov.cn.nyfyq.cn http://www.morning.tbksk.cn.gov.cn.tbksk.cn http://www.morning.srkzd.cn.gov.cn.srkzd.cn http://www.morning.prhfc.cn.gov.cn.prhfc.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.ynwdk.cn.gov.cn.ynwdk.cn http://www.morning.jiuyungps.com.gov.cn.jiuyungps.com http://www.morning.sbrjj.cn.gov.cn.sbrjj.cn http://www.morning.mjzcp.cn.gov.cn.mjzcp.cn http://www.morning.hsgxj.cn.gov.cn.hsgxj.cn http://www.morning.fdjwl.cn.gov.cn.fdjwl.cn http://www.morning.rxnl.cn.gov.cn.rxnl.cn