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入门程序
     */
    @Test
    public 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");//DML
        System.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入门程序
     */
    @Test
    public 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");//DML
        System.out.println("SQL语句执行完毕影响的记录数为:"+i);

        //5.释放资源
        statement.close();
        connection.close();
    }
    @Test
    public 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 = ?";//预编译SQL
        Connection 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

posted @ 2025-11-15 18:25  David大胃  阅读(0)  评论(0)    收藏  举报