网站建设需要关注什么,网站开发模块化开发,wordpress安装图片不显示,如何查企业做网站是否备案过在项目中我们时常需要写SQL语句#xff0c;或简单的使用注解直接开发#xff0c;或使用XML进行动态SQL之类的相对困难的SQL#xff0c;并在IDEA中操控我们的SQL#xff0c;但网上大都图方便或者觉得太简单了#xff0c;完全没一个涵盖两个方面的讲解。
单表#xff1a; … 在项目中我们时常需要写SQL语句或简单的使用注解直接开发或使用XML进行动态SQL之类的相对困难的SQL并在IDEA中操控我们的SQL但网上大都图方便或者觉得太简单了完全没一个涵盖两个方面的讲解。
单表 DDL表操作
创表语句 约束 常见的数据类型 for example(例子
create table address_book
(id bigint auto_increment comment 主键primary key,user_id bigint not null comment 用户id,consignee varchar(50) null comment 收货人,sex varchar(2) null comment 性别,phone varchar(11) not null comment 手机号,province_code varchar(12) charset utf8mb4 null comment 省级区划编号,province_name varchar(32) charset utf8mb4 null comment 省级名称,city_code varchar(12) charset utf8mb4 null comment 市级区划编号,city_name varchar(32) charset utf8mb4 null comment 市级名称,district_code varchar(12) charset utf8mb4 null comment 区级区划编号,district_name varchar(32) charset utf8mb4 null comment 区级名称,detail varchar(200) charset utf8mb4 null comment 详细地址,label varchar(100) charset utf8mb4 null comment 标签,is_default tinyint(1) default 0 not null comment 默认 0 否 1是
)comment 地址簿 collate utf8mb3_bin; 查询语句 修改语句 那么IDEA中是如何操作DDL语句的呢 需要特别说的是IDEA中想执行哪部分代码就左键选中代码块变色再点击绿色的执行按钮
DML
insert Insert(insert into category(type, name, sort, status, create_time, update_time, create_user, update_user) VALUES (#{type}, #{name}, #{sort}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser}))
insert idinsertBacthinsert into dish_flavor (dish_id, name, value)valueforeach collectionflavors itemdf separator,(#{df.dishId},#{df.name},#{df.value})/foreach/insert
Update update idupdate
# yml中配置文件加的开启驼峰命名只是java中Employee 类的 成员变量的驼峰命名 可以对应 数据库中的 参数名update employeesetif testname ! null name #{name},/ifif testusername ! null username #{username},/ifif testpassword ! null password #{password},/ifif testphone ! nullphone #{phone},/ifif testsex ! null sex #{sex},/ifif testidNumber ! null id_Number #{idNumber},/ifif teststatus ! null status #{status},/ifif testupdateTime ! nullupdate_Time #{updateTime},/ifif testupdateUser ! null update_User #{updateUser},/if/setwhere id #{id}/update
项目里一般都是动态SQL编辑数据简单的update直接使用MP根本没必要写
Delete Delete(delete from dish_flavor where dish_id #{dishid})void deleteByDishId(Long dishid);
delete iddeleteByIdsdelete from dish_flavor where dish_id inforeach collectiondishIds itemdishId open( close) separator,#{dishId}/foreach/delete
DQL: 基本查询 注意代码中*是通配符即查询所有
条件查询 聚合函数 分组查询 排序查询 分页查询 但分页查询不同于其他的DQL查询方式它是项目较为重要的部分我们一般会使用PageHelper这个插件来简化我们的代码以下是
分页查询的三点重要的步骤
First 先pom.xml导入maven对应依赖
dependencygroupIdcom.github.pagehelper/groupIdartifactIdpagehelper-spring-boot-starter/artifactId/dependency
Second 在impl中写模板式代码 public PageResult pageQuery( EmployeePageQueryDTO employeePageQueryDTO) {
// select * from employee limit 0,10
// 开始分页查询PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
// 这个是强制要求名字叫”page“不能改所以这需要创建对象PageEmployee page employeeMapper.pageQuery(employeePageQueryDTO);// long total1 employeeMapper.pageQuery(employeePageQueryDTO).getTotal();
// ListEmployee records1 employeeMapper.pageQuery(employeePageQueryDTO).getResult();long total page.getTotal();ListEmployee records page.getResult();return new PageResult(total,records);}
Third
select idpageQuery resultTypecom.sky.entity.Employeeselect * from employeewhereif testname ! null and name ! and name like concat(%,#{name},%)/if/whereorder by create_time desc/select多表
分为逻辑外键和物理外键见名知意前者并没有物理层面的约束后者则是有着物理层面的约束 以上是一份逻辑关联的SQL语句这应该是企业最常用的方式
但此方法也有缺点若你没考虑周全可能会误操作所以物理外键可能更利于你保持数据完整性和同一性。 IDEA修改方法 根据名称进行添加修改外键
多表查询
先简单介绍一下笛卡尔积两个集合一个集合数据量为2另一个数据量为4两个相乘则为8条这就是笛卡尔积
而我们肯定不需要冗余数据所以需要消除冗余项 分类 内连接就是取并集外连接就是取AorB子查询则是查询蓝色or橘色部分
内连接 外连接 子查询 总结代码
select idpageQuery resultTypecom.sky.vo.DishVOselect d.* , c.name as category_name from dish d left outer join category c on d.category_id c.idwhereif test name!nulland d.name like concat(%,#{name},%)/ifif test categoryId ! nulland d.category_id #{categoryId}/ifif test status ! nulland d.status #{status}/if/whereorder by d.create_time desc/select
事务 注意 ● 默认MySQL的事务是自动提交的也就是说当执行一条DML语句 MySQL会 立即隐式的提交事务。 索引
当数据库的表中数据量很大时DML等SQL语句会有很长的时耗。
索引(index)是帮助数据库高效获取数据的数据结构
。 ..
语法