网站群建设方案6,汉服网页设计作品,中江门户网站,58找工作招聘信息Mybatis基础操作
功能列表#xff1a;
查询 根据主键ID查询 条件查询新增更新删除 根据主键ID删除 根据主键ID批量删除
准备 实施前的准备工作#xff1a; 准备数据库表创建一个新的 springboot 工程#xff0c;选择引入对应的起步依赖#xff08;mybatis、mysql 驱动、…Mybatis基础操作
功能列表
查询 根据主键ID查询 条件查询新增更新删除 根据主键ID删除 根据主键ID批量删除
准备 实施前的准备工作 准备数据库表创建一个新的 springboot 工程选择引入对应的起步依赖mybatis、mysql 驱动、lombokapplication.properties 中引入数据库连接信息创建对应的实体类 Emp实体类属性采用驼峰命名准备 Mapper 接口 EmpMapper 删除 功能实现
当我们点击后面的删除按钮时前端页面会给服务端传递一个参数也就是该行数据的 ID。我们接收到 ID 后根据 ID 删除数据即可。 SQL语句:
接口方法: 注意如果mapper接口方法形参只有一个普通类型的参数#{…} 里面的属性名可以随便写如#{id}、#{value}。 完整代码
Mapper
public interface EmpMapper {//根据ID删除数据Delete(delete from emp where id #{id})public void delete(Integer id);//public int delete(Integer id);
}SpringBootTest
class SpringbootMybatisCrudApplicationTests {Autowiredprivate EmpMapper empMapper;//根据ID删除Testpublic void testDelete(){//int delete empMapper.delete(16);//System.out.println(delete);empMapper.delete(18);}
}日志输入 在 Mybatis 当中我们可以借助日志查看到 sql 语句的执行、执行传递的参数以及执行结果。具体操作如下 打开 application.properties 文件开启 mybatis 的日志并指定输出到控制台 #指定mybatis输出日志的位置,输出控制台
mybatis.configuration.log-implorg.apache.ibatis.logging.stdout.StdOutImpl预编译 SQL 预编译 SQL 有两个优势 性能更高更安全(防止 SQL 注入) SQL 注入 SQL 注入是通过操作输入的数据来修改事先定义好的 SQL 语句以达到执行代码对服务器进行攻击的方法。 参数占位符 在 Mybatis 中提供的参数占位符有两种${…} 、#{…} #{…} 执行 SQL 时会将#{…}替换为?生成预编译 SQL会自动设置参数值 使用时机参数传递都使用#{…} ${…} 拼接 SQL。直接将参数拼接在 SQL 语句中存在 SQL 注入问题 使用时机如果对表名、列表进行动态设置时使用 插入新增
SQL语句:
insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)
values (songyuanqiao,宋远桥,1,1.jpg,2,2012-10-09,2,2022-10-01 10:00:00,2022-10-01 10:00:00);接口方法: 新增主键返回 描述在数据添加成功后需要获取插入数据库数据的主键。如添加套餐数据时还需要维护套餐菜品关系表数据。 默认情况下执行插入操作时是不会主键值返回的。如果我们想要拿到主键值需要在 Mapper 接口中的方法上添加一个 Options 注解 并在注解中指定属性 useGeneratedKeys true 和 keyProperty 实体类属性名 实现 更新
SQL语句(根据ID更新员工信息)接口方法
Mapper
public interface EmpMapper {//更新员工Update(update emp set username #{username}, name #{name}, gender #{gender}, image #{image}, job #{job}, entrydate #{entrydate}, dept_id #{deptId},update_time #{updateTime} where id #{id})public void update(Emp emp);
}查询
根据ID查询
SQL语句根据ID查询接口方法
Mapper
public interface EmpMapper {Select(select * from emp where id #{id})public Emp getById(Integer id);
}数据封装 实体类属性名 和 数据库表查询返回的字段名一致mybatis会自动封装。 如果实体类属性名 和 数据库表查询返回的字段名不一致不能自动封装。 数据封装---解决方法
起别名在SQL语句中对不一样的列名起别名别名和实体类属性名一样。
手动结果映射通过 Results及Result 进行手动结果映射。
开启驼峰命名如果字段名与属性名符合驼峰命名规则mybatis会自动通过驼峰命名规则映射。
条件查询
SQL语句条件查询
select * from emp where name like %张% and gender 1 and entrydate between 2010-01-01 and 2020-01-01 order by update_time desc;接口方法 方式一 模糊查询使用${…}进行字符串拼接这种方式呢由于是字符串拼接并不是预编译的形式所以效率不高、且存在sql注入风险。 方式二解决SQL注入风险 使用 MySQL 提供的字符串拼接函数concat(‘%’ , ‘关键字’ , ‘%’) Mapper
public interface EmpMapper {//条件查询员工//方式一Select(select * from emp where name like %${name}% and gender #{gender} and entrydate between #{begin} and #{end} order by update_time desc )public ListEmp list(String name, Short gender, LocalDate begin , LocalDate end);
}Mapper
public interface EmpMapper {//条件查询员工//方式二Select(select * from emp where name like concat(%,#{name},%) and gender #{gender} and entrydate between #{begin} and #{end} order by update_time desc )public ListEmp list(String name, Short gender, LocalDate begin , LocalDate end);
}参数名说明 Mybatis 的 XML 配置文件
XML 配置文件规范 规范 XML映射文件的名称与Mapper接口名称一致并且将XML映射文件和Mapper接口放置在相同包下同包同名。 XML映射文件的namespace属性为Mapper接口全限定名一致。 XML映射文件中sql语句的id与Mapper 接口中的方法名一致并保持返回类型一致。 XML 配置文件实现
Mapper
public interface EmpMapper {//动态条件查询public ListEmp list(String name, Short gender, LocalDate begin , LocalDate end);
}?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.EmpMapper!--resultType: 单条记录封装的类型--select idlist resultTypecom.itheima.pojo.Empselect * from emp where name like concat(%,#{name},%) and gender #{gender} and entrydate between #{begin} and #{end} order by update_time desc/select/mapperXML映射文件 MybatisX MybatisX 是一款基于 IDEA 的快速开发Mybatis的插件为效率而生。 使用 Mybatis 的注解主要是来完成一些简单的增删改查功能。如果需要实现复杂的 SQL 功能建议使用 XML 来配置映射语句。 官方说明入门_MyBatis中文网 Mybatis动态SQL
什么是动态SQL SQL 语句会随着用户的输入或外部条件的变化而变化我们称为动态 SQL。 Mybatis动态SQL if if用于判断条件是否成立。使用 test 属性进行条件判断如果条件为 true则拼接 SQL。wherewhere 元素只会在子元素有内容的情况下才插入 where 子句。而且会自动去除子句的开头的 AND 或 OR。 ?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.EmpMapper!--resultType: 单条记录封装的类型--select idlist resultTypecom.itheima.pojo.Empselect * from emp whereif testname ! nullname like concat(%, #{name}, %)/ifif testgender ! nulland gender #{gender}/ifif testbegin ! null and end ! nulland entrydate between #{begin} and #{end}/if/whereorder by update_time desc/select/mapper更新员工 set动态地在行首插入 SET 关键字并会删掉额外的逗号。用在update语句中 ?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.EmpMapper!-- 动态更新员工--update idupdate2update empsetif testusername ! nullusername #{username},/ifif testname ! nullname #{name},/ifif testgender ! nullgender #{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}/update/mapper动态 SQL-foreach foreach
批量删除员工 (18,19,20) Mapper
public interface EmpMapper {//批量删除员工public void deleteByIds(ListInteger ids);
}?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.EmpMapper!--批量删除员工 (18,19,20)--!--collection: 遍历的集合item: 遍历出来的元素separator: 分隔符open: 遍历开始前拼接的SQL片段close: 遍历结束后拼接的SQL片段--delete iddeleteByIdsdelete from emp where id inforeach collectionids itemid separator, open( close)#{id}/foreach/delete/mapper动态 SQL-sqlinclude sql定义可重用的 SQL 片段。include通过属性 refid指定包含的 sql 片段。 上一节
Mybatis入门day08-CSDN博客
下一节