创新平台网站建设方案宁波网站建设公司推荐哪家
web/
2025/10/5 2:39:23/
文章来源:
创新平台网站建设方案,宁波网站建设公司推荐哪家,深圳推广公司,wordpress全屏博客Mybatis
mybatis查询
准备
准备工作 在目前的数据库中添加一张数据表emp 将资料中提供的day04-01-mybatis导入的目前的工程中 修改配置文件中的数据库的账户和密码 观察实体类中的属性和数据表中的字段的对应关系
查询结果封装
查询所有 SQL语句 select * from emp; …Mybatis
mybatis查询
准备
准备工作 在目前的数据库中添加一张数据表emp 将资料中提供的day04-01-mybatis导入的目前的工程中 修改配置文件中的数据库的账户和密码 观察实体类中的属性和数据表中的字段的对应关系
查询结果封装
查询所有 SQL语句 select * from emp; 接口方法 Select(select * from emp)public ListEmp findAll(); 数据封装 实体类属性名 和 数据库表查询返回的字段名一致mybatis会自动封装。 如果实体类属性名 和 数据库表查询返回的字段名不一致不能自动封装。 开启驼峰命名如果字段名与属性名符合驼峰命名规则mybatis会自动通过驼峰命名规则映射。 #开启驼峰命名自动映射即从数据库字段名 a_column 映射到Java 属性名 aColumn。 setting namemapUnderscoreToCamelCase valuetrue/ 起别名在SQL语句中对不一样的列名起别名别名和实体类属性名一样。 Select(select id, username, password, name, gender, image, job, entrydate ed, dept_id deptId, create_time createTime, update_time updateTime from emp)public Emp findAll(); 手动结果映射通过 Results及Result 进行手动结果映射。 Select(select * from emp)Results({ Result(column dept_id, property deptId), Result(column entrydate, property ed) })public Emp findAll();
条件查询
查询条件查询 SQL语句 select * from emp where name 张三丰 and gender 1 and entrydate between 2010-01-01 and 2020-01-01 ; 接口方法 Select(select * from emp where name #{name} and gender #{gender} and entrydate between #{begin} and #{end})ListEmp findList(Param(name) String name, Param(gender) Short gender, Param(begin) LocalDate begin, Param(end) LocalDate end);
Param Param 标注在方法参数的前面,用于声明参数在#{}中的名字 后面SpringBoot2.x整合mybatis之后,这个注解可以不再添加
模糊查询
查询模糊查询 SQL语句 select * from emp where name like %张% and gender 1 and entrydate between 2010-01-01 and 2020-01-01 ; 接口方法(性能低、不安全、存在SQL注入问题) Select(select * from emp where name like %${name}% and gender #{gender} and entrydate between #{begin} and #{end})ListEmp findList(Param(name) String name, Param(gender) Short gender, Param(begin) LocalDate begin, Param(end) LocalDate end);Select(select * from emp where name like concat(%,#{name},%) and gender #{gender} and entrydate between #{begin} and #{end})ListEmp findList(Param(name) String name, Param(gender) Short gender, Param(begin) LocalDate begin, Param(end) LocalDate end);
xml书写sql
XML映射文件 使用Mybatis的注解主要是来完成一些简单的增删改查功能。 如果需要实现复杂的SQL功能建议使用XML来配置映射语句。 官方说明入门_MyBatis中文网
规范 XML映射文件的名称与Mapper接口名称一致并且将XML映射文件和Mapper接口放置在相同包下同包同名。 XML映射文件的namespace属性为Mapper接口全限定名一致。 XML映射文件中sql语句的id与Mapper 接口中的方法名一致并保持返回类型一致。
经典报错 对于同一个方法,XML和注解中同时为其编写了sql 约定好的对应关系有误 MybatisX 是一款基于 IDEA 的快速开发Mybatis的插件为效率而生
动态sql
介绍
动态SQL 随着用户的输入或外部条件的变化而变化的SQL语句我们称为 动态SQL。
if where set
if if用于判断条件是否成立。使用test属性进行条件判断如果条件为true则拼接SQL。 wherewhere 元素只会在子元素有内容的情况下才插入where子句。而且会自动去除子句的开头的AND 或OR。
动态更新员工信息如果更新时传递有值则更新如果更新时没有传递值 set动态地在行首插入 SET 关键字并会删掉额外的逗号。用在update语句中
foreach
foreach SQL语句 delete from emp where id in (1,2,3); 接口方法 //批量删除 public void deleteByIds(Param(ids) ListInteger ids); XML映射文件 delete iddeleteByIdsdelete from emp where id inforeach collectionids itemid separator, open( close)#{id}/foreach/delete
属性 collection集合名称 item集合遍历出来的元素/项 separator每一次遍历使用的分隔符 open遍历开始前拼接的片段 close遍历结束后拼接的片段
sqlinclude
sql片段(了解) sql定义可重用的 SQL 片段。 include通过属性refid指定包含的sql片段。
配置文件
SqlMapConfig.xml
主配置文件 settings控制一些全局配置项的开闭 mappers用于指定Mapper接口的位置 environments: 配置事务管理器和数据库连接信息
数据库连接池 数据库连接池是个容器负责分配、管理数据库连接(Connection) 它允许应用程序重复使用一个现有的数据库连接而不是再重新建立一个 释放空闲时间超过最大空闲时间的连接来避免因为没有释放连接而引起的数据库连接遗漏
优势 资源重用 提升系统响应速度 避免数据库连接遗漏
标准接口DataSource 官方(sun)提供的数据库连接池接口由第三方组织实现此接口。 功能获取连接 归还连接
常见产品 Druid(德鲁伊): 阿里巴巴提供的数据库连接池技术国内使用率很高提供了完善的监控机制 HikariCP: 日本人开发的连接池技术号称性能之王速度最快SpringBoot2.0默认使用此连接池
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/87105.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!