当前位置: 首页 > news >正文

Day39(9)F:\硕士阶段\Java\课程代码\后端\web-ai-code\web-ai-project01\jdbc-demo+springboot-web-quickstart

DQL条件查询

image-20251115122807028

--  =================== DQL: 条件查询 ======================
-- 1. 查询 姓名 为 柴进 的员工
select * from emp where name = '柴进';-- 2. 查询 薪资小于等于5000 的员工信息
select * from emp where salary <=5000;-- 3. 查询 没有分配职位 的员工信息
select * from emp where job is null;-- 4. 查询 有职位 的员工信息
select * from emp where job is not null;-- 5. 查询 密码不等于 '123456' 的员工信息
select * from emp where password != '123456';select * from emp where password <> '123456';-- 6. 查询 入职日期 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之间的员工信息
select * from emp where entry_date between '2000-01-01' and '2010-01-01';# select * from emp where entry_date between '2010-01-01' and '2000-01-01';只能从小到大不能像这样-- 7. 查询 入职时间 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之间 且 性别为女 的员工信息
select * from emp where entry_date between '2000-01-01' and '2010-01-01' and gender = 2;-- 8. 查询 职位是 2 (讲师), 3 (学工主管), 4 (教研主管) 的员工信息
select * from emp where job = 2 or 3 or 4;select * from emp where job in (2,3,4);-- 9. 查询 姓名 为两个字的员工信息(_单个字符;%任意个字符)
select * from emp where name like '__';-- 10. 查询 姓 '李' 的员工信息
select * from emp where name like '李%';-- 11. 查询 姓名中包含 '二' 的员工信息
select * from emp where name like '%二%'

image-20251115124631297

image-20251115125737969

image-20251115130819949

image-20251115131822769

先执行where,然后执行聚合函数count,后才执行having的分组操作

--  =================== DQL: 分组查询 ======================
-- 聚合函数
-- 注意:所有的聚合函数不参与null值的统计-- 1. 统计该企业员工数量
-- count(字段)
select count(job) from emp;
-- count(*):推荐,性能最高
select count(*) from emp;
-- count(常量):其次推荐
select count(1) from emp;
-- 2. 统计该企业员工的平均薪资
select avg(emp.salary) from emp;-- 3. 统计该企业员工的最低薪资
select min(salary) from emp;-- 4. 统计该企业员工的最高薪资
select max(salary) from emp;-- 5. 统计该企业每月要给员工发放的薪资总额(薪资之和)
select sum(emp.salary) from emp;-- 分组-- 一旦进行了分组操作,select后的字段列表不能随意书写,能书写的一般是分组字段+聚合语句;
-- 1. 根据性别分组 , 统计男性和女性员工的数量
select gender,count(*) from emp group by gender;-- 2. 先查询入职时间在 '2015-01-01' (包含) 以前的员工 , 并对结果根据职位分组 , 获取员工数量大于等于2的职位
select job,count(*) from emp where entry_date <= '2015-01-01' group by job having count(*)>=2 ;

image-20251115132202404

where筛选分组前,having筛选分组后

image-20251115132959060

image-20251115133015153

--  =================== 排序查询 ======================
-- 1. 根据入职时间, 对员工进行升序排序 - asc默认
select * from emp order by entry_date;
select * from emp order by entry_date asc;-- 2. 根据入职时间, 对员工进行降序排序
select * from emp order by entry_date desc;-- 3. 根据 入职时间 对公司的员工进行 升序排序 , 入职时间相同 , 再按照 更新时间 进行降序排序
select * from emp order by entry_date,update_time desc;

image-20251115134431351

--  =================== 分页查询 ======================
-- 1. 从起始索引0开始查询员工数据, 每页展示5条记录
select * from emp limit 0,5;select * from emp limit 0,5;-- 2. 查询 第1页 员工数据, 每页展示5条记录
select * from emp limit 0,5;-- 3. 查询 第2页 员工数据, 每页展示5条记录
select * from emp limit 5,5;-- 4. 查询 第3页 员工数据, 每页展示5条记录
select * from emp limit 10,5;-- 页码
-- 起始索引 = (页码-1)* 每页展示记录数

image-20251115134549933

image-20251115140507662

JDBC

image-20251115140827610

image-20251115141640098

package com.itheima;import org.junit.Test;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;public class jdbcTest {/*** JDBC入门程序*/@Testpublic void testUpdate() throws Exception {//1.注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//2.获取数据库连接String url = "jdbc:mysql://localhost:3306/web01";String username = "root";String password = "1234";Connection connection = DriverManager.getConnection(url, username, password);//3.获取SQL语句执行对象Statement statement = connection.createStatement();//4.执行sql语句int i = statement.executeUpdate("update user set age = 25 where id =1");//DMLSystem.out.println("SQL语句执行完毕影响的记录数为:"+i);//5.释放资源statement.close();connection.close();}
}

image-20251115144833346

package com.itheima;import com.itheima.pojo.User;
import org.junit.Test;import java.sql.*;public class jdbcTest {/*** JDBC入门程序*/@Testpublic void testUpdate() throws Exception {//1.注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//2.获取数据库连接String url = "jdbc:mysql://localhost:3306/web01";String username = "root";String password = "1234";Connection connection = DriverManager.getConnection(url, username, password);//3.获取SQL语句执行对象Statement statement = connection.createStatement();//4.执行sql语句int i = statement.executeUpdate("update user set age = 25 where id =1");//DMLSystem.out.println("SQL语句执行完毕影响的记录数为:"+i);//5.释放资源statement.close();connection.close();}@Testpublic void testSelect() throws Exception{Class.forName("com.mysql.cj.jdbc.Driver");String url = "jdbc:mysql://localhost:3306/web01";String username = "root";String password = "1234";String sql = "SELECT id,username,password,name,age FROM user WHERE username=? AND password = ?";//预编译SQLConnection connection = DriverManager.getConnection(url, username, password);
//        Statement statement = connection.createStatement();
//        ResultSet resultSet = statement.executeQuery();PreparedStatement preparedStatement = connection.prepareStatement(sql);preparedStatement.setString(1,"daqiao");preparedStatement.setString(2,"123456");ResultSet resultSet = preparedStatement.executeQuery();//封装查询返回结果try {//处理结果信息while (resultSet.next()){User user = new User(resultSet.getInt("id"),resultSet.getString("username"),resultSet.getString("password"),resultSet.getString("name"),resultSet.getInt("age"));System.out.println(user);//使用Lombok的@Data自动生成toString方法}} catch (SQLException e) {throw new RuntimeException(e);} finally {//关闭资源try {if (resultSet!=null)resultSet.close();if (preparedStatement!=null )preparedStatement.close();if (connection!=null)connection.close();} catch (SQLException e) {throw new RuntimeException(e);}}}
}

image-20251115153522596

image-20251115153930346

SQL注入

image-20251115161616420

String sql = "select * from user where username = '" + username + "' and password = '" + password + "'";
// 若password输入为 "' or '1'='1",SQL会被拼接为:
// select * from user where username = 'daqiao' and password = '' or '1'='1'
// 条件恒成立,导致所有用户数据被查询
String sql = "select * from user where username = ? and password = ?";preparedStatement.setString(1, username);preparedStatement.setString(2, password);
// 即使password输入为 "' or '1'='1",数据库会将其当作普通字符串,SQL逻辑仍为:
// select * from user where username = 'daqiao' and password = '' or '1'='1' (但此处参数是纯数据,逻辑不生效)

image-20251115171026064

image-20251115171149158

image-20251115171523304

创建module

需要勾选三个依赖

image-20251115182010932

image-20251115181924862

数据库连接信息

spring.application.name=springboot-mybatis-quickstart#配置数据库的连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/web01
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=1234

准备实体类

package com.itheima.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data//lombok提供,说明定义的属性有了get和set方法
@AllArgsConstructor//全参构造
@NoArgsConstructor//无参构造
public class User {private Integer id;private  String username;private String password;private String name;private Integer age;}

定义Mapper接口,实现mybatis的功能(核心)

package com.itheima.mapper;import com.itheima.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.util.List;@Mapper//这个注解是说明这个接口是mybatis中的持久层接口
//应用程序在运行时,会自动的为该接口创建一个实现类对象(代理对象),并自动将该实现类对象放置到IOC容器中,成为bean对象
public interface UserMapper {/*** 查询所有用户*/@Select("select * from user")public List<User> findAll();}

image-20251115182221100

http://www.jsqmd.com/news/41238/

相关文章:

  • # Android Compose 实现 左滑删除
  • win10pro sn
  • 完整教程:PMBT2222A,215 开关晶体管功率二极管 NXP安世半导体 音频放大电路 LED驱动 应用
  • EFCore中巧妙利用ToQueryString()实现批插(不借助第三方包)
  • 2025 年 11 月门窗十大品牌综合实力权威推荐榜单,产能、专利、环保三维数据透视
  • 20232426 2025-2026-1 《网络与系统攻防技术》实验五实验报告
  • Springboot启动时记录进程ID
  • 019数据结构之栈——算法备赛 - 实践
  • 详细介绍:【Linux】07.Ubuntu开发环境部署
  • GESP考试报名附考试报名流程
  • 2025 最新电缆品牌权威推荐:耐火 / 阻燃 / 智能 / 光伏等全品类优质厂商榜单,附国际认证测评
  • 2025 最新电缆制造厂家推荐!电缆品牌权威榜单发布,耐火 / 智能 / 特种电缆优选企业全解析
  • 2025年Dynamics 365 CRM的工作行情如何?
  • iHaier2.0 智能协同办公模块(Doc-Collab)实现实用的方案详解
  • 字节序浅析
  • 2025 最新无缝钢管厂家推荐榜:国际测评认证 + 技术创新 + 全场景适配权威指南大口径无缝钢管/16Mn 无缝钢管/定制无缝钢管/厚壁无缝钢管公司推荐
  • 2025年硫酸钠流化床干燥机源头厂家权威推荐榜单:调味品振动流化床干燥机/大豆纤维流化床干燥机/味精振动流化床干燥机源头厂家精选
  • 2025 最新钢结构源头厂家推荐排行榜,聚焦优质供应与专业服务精选榜单美标 / 欧标钢结构 / 环保设备 / 水泥矿山 / 机械设备钢结构厂家推荐
  • 当下市面上靠谱的平移门服务商
  • 2025年11月中国伸缩门源头厂家口碑推荐榜单
  • 简单了解数组
  • 2025年目字扣订制厂家权威推荐榜单:塑料扣具/箱包插扣/五金插扣源头厂家精选
  • # 第10章 指针和结构体
  • 2025年步进式加热直饮水机订制厂家权威推荐榜单:奶茶店全自动烧水器/大型工业净水器/饭店专用开水器源头厂家精选
  • 2025 最新钢结构厂家推荐排行榜,涵盖全产业链服务与优质产能企业权威甄选钢结构建筑/钢结构房屋/钢结构屋面/钢结构网架/钢结构桁架/钢结构连廊公司推荐
  • 2025 年 11 月漆渣脱水系统,漆渣脱水机,漆渣脱水装置品牌最新推荐,产能、专利、环保三维数据透视!
  • Convex
  • 2025年全自动无屑切割倒角一体机实力厂家权威推荐榜单:自动化切割倒角一体机/切割倒角一体机/自动切割倒角一体机源头厂家精选
  • 2025 年 11 月喷漆废水处理工艺,喷漆废水处理技术改造,喷漆废水处理运维服务公司最新推荐,聚焦资质、案例、售后的五家机构深度解读
  • 2025 国内网架厂家最新推荐排行榜:聚焦钢结构 / 球形 / 螺栓球多场景,甄选技术服务双优的权威品牌指南