金华市网站建设最低价预约支付wordpress
web/
2025/9/28 19:52:21/
文章来源:
金华市网站建设最低价,预约支付wordpress,外贸企业的网站建设,template是什么意思目录
一、准备工作
二、常用配置 三、尝试 四、增删改查
1、增加
2、删除
3、修改
4、查询
五、XML的映射方法 一、准备工作
实施前的准备工作#xff1a; 准备数据库表 创建一个新的springboot工程#xff0c;选择引入对应的起步依赖#xff08;mybatis、mysql驱动…目录
一、准备工作
二、常用配置 三、尝试 四、增删改查
1、增加
2、删除
3、修改
4、查询
五、XML的映射方法 一、准备工作
实施前的准备工作 准备数据库表 创建一个新的springboot工程选择引入对应的起步依赖mybatis、mysql驱动、lombok application.properties中引入数据库连接信息 创建对应的实体类 Emp实体类属性采用驼峰命名 准备Mapper接口 EmpMapper
SQL文件emp的sql文件 二、常用配置 #指定mybatis输出日志的位置, 输出控制台
mybatis.configuration.log-implorg.apache.ibatis.logging.stdout.StdOutImpl# 查询的时候mybatis驼峰命名法
mybatis.configuration.map-underscore-to-camel-casetrue 三、尝试
在Mybatis中提供的参数占位符有两种${...} 、#{...} #{...} 执行SQL时会将#{…}替换为?生成预编译SQL会自动设置参数值 使用时机参数传递都使用#{…} ${...} 拼接SQL。直接将参数拼接在SQL语句中存在SQL注入问题 使用时机如果对表名、列表进行动态设置时使用 注意事项在项目开发中建议使用#{...}生成预编译SQL防止SQL注入安全。 四、增删改查
1、增加 // 新增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}))int insert(Emp emp); 测试 // 新增Testpublic void empAdd(){//创建员工对象Emp emp new Emp();emp.setUserName(小明);emp.setName(小将);emp.setImage(sadasdasd.jpg);emp.setGender((short)1);emp.setJob(1);emp.setEntryDate(LocalDate.of(2000,1,1));emp.setCreateTime(LocalDate.now());emp.setUpdateTime(LocalDate.now());emp.setDeptId(1);empMapper.insert(emp);}
2、删除 // 删除Delete(delete from emp where id #{id})int delete(int id);test // 删除测试Testpublic void empDelete() {var s empMapper.delete(17);System.out.printf(删除%s\n,s);}
3、修改 // 修改Update(update emp set username #{userName}, name #{name}, gender 3 where id 18;)void update(Emp emp); Test // 修改Testpublic void update(){Emp emp new Emp();emp.setName(大卫);emp.setUserName(daadasd);emp.setGender(2);empMapper.update(emp);}
4、查询 // 查询Select(select * from emp where name like concat(%,#{name},%) and gender #{gender} and entrydate between #{begin} and #{end} order by update_time desc)ListEmp list(String name, Short gender, LocalDate begin, LocalDate end); Test // 查询Testpublic void search(){ListEmp emp empMapper.list(汤姆, (short) 1,LocalDate.of(2000,8,15),LocalDate.of(2023,8,5));System.out.println(emp);}
五、XML的映射方法 sql定义可重用的SQL片段 include通过属性refid指定包含的SQL片段 if 用于判断条件是否成立如果条件为true则拼接SQL 形式 if testname ! null … /if where where元素只会在子元素有内容的情况下才插入where子句而且会自动去除子句的开头的AND或OR set 动态地在行首插入 SET 关键字并会删掉额外的逗号。用在update语句中 foreach遍历deleteByIds方法中传递的参数ids集合 foreach collection集合名称 item集合遍历出来的元素/项 separator每一次遍历使用的分隔符 open遍历开始前拼接的片段 close遍历结束后拼接的片段
/foreach
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttps://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.demo.crud.mapper.EmpMapper!--提取重复代码 --sql idcommonSelectselect * from emp /sql!-- 查询--
!-- resultType单条记录封装的类型 --select idlist resultTypecom.demo.crud.pojo.Empinclude refidcommonSelect/whereif testname ! nullname like concat(%,#{name},%)/iforder by update_time desc/where/select!--删除操作--delete iddeleteByIdsdelete from emp where id inforeach collectionids itemid separator, open( close)#{id}/foreach/delete/mapper
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/83498.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!