SQL和动态SQL 2023.11.15 2026.3.20 1742 4 分钟1 SQL1.1 DDL(Data Definition Language) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // 1. 对数据库进行定义 CREATE DATABASE database1; //创建一个名为database1的数据库 DROP DATABASE database1; //删除一个名为database1的数据库 show databases; //查询所有数据库 select database(); //查询当前数据库 use database1; //使用数据库database1 // 2. 对数据库表进行定义 create table 表名(字段1 字段类型 [约束] [comment 字段1注释],...,) show tables //查询当前数据库所有表 desc 表名 //查询表结构 show create table 表名 //查询建表语句 ALTER TABLE player ADD (age int(11)); // 添加字段 ALTER TABLE player RENAME COLUMN age to player_age; // 修改字段名 ALTER TABLE player MODIFY (player_age float(3,1)); // 修改字段的数据类型,将player_age的数据类型设置为float(3,1) alter table 表名 change 旧字段名 新字段名 类型(长度)[comment 注释] [约束] rename table 表名 to 新表名 //改表名 drop table [if exists] 表名; //删除 2.2 DML(Data Manipulation Language)1 2 3 4 5 6 7 8 insert into 表名(字段名1,字段名2) values (值1,值2) //指定字段添加数据 insert into 表名 values (值1,值2,...) //全部字段添加数据 insert into 表名(字段名1,字段名2) values (值1,值2),(值1,值2) //批量添加数据(指定字段) insert into 表名 values (值1,值2,...),(值1,值2,...) //批量添加数据(全部字段) update 表名 set 字段名1 = 值1 , 字段名2 = 值2, ... [where 条件] //修改数据 delete from 表名 [where 条件] //删除数据 2.3 DQL(Data Query Language) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 SELECT 字段列表 FROM 表明列表 WHERE 条件列表 GROUP BY 分组字段列表 HAVING 分组后条件列表 ORDER BY 排序字段列表 LIMIT 分页参数 2.4 多表查询1 2 3 4 5 6 7 8 // 内连接 SELECT 字段列表 FROM 表1,表2 WHERE 条件 //隐式内连接 SELECT 字段列表 FROM 表1 [inner] JOIN 表2 on 连接条件 //显示内连接 // 外连接 SELECT 字段列表 FROM 表1 LEFT [OUTER] JOIN 表2 ON 连接条件 //左外连接,包含左表全部信息 SELECT 字段列表 FROM 表1 RIGHT [OUTER] JOIN 表2 ON 连接条件 ////右外连接,包含左表全部信息 2 动态SQL2.1 IF和WHERE 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!-- List<Employee> selectEmployeeByCondition(Employee employee); --> <select id="selectEmployeeByCondition" resultType="employee"> select emp_id,emp_name,emp_salary from t_emp <!-- where标签会自动去掉“标签体内前面多余的and/or” --> <where> <!-- 使用if标签,让我们可以有选择的加入SQL语句的片段。这个SQL语句片段是否要加入整个SQL语句,就看if标签判断的结果是否为true --> <!-- 在if标签的test属性中,可以访问实体类的属性,不可以访问数据库表的字段 --> <if test="empName != null"> <!-- 在if标签内部,需要访问接口的参数时还是正常写#{} --> or emp_name=#{empName} </if> <if test="empSalary > 2000"> or emp_salary>#{empSalary} </if> </where> </select> 2.2 SET 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <!-- void updateEmployeeDynamic(Employee employee) --> <update id="updateEmployeeDynamic"> update t_emp <!-- set emp_name=#{empName},emp_salary=#{empSalary} --> <!-- 使用set标签动态管理set子句,并且动态去掉两端多余的逗号 --> <set> <if test="empName != null"> emp_name=#{empName}, </if> <if test="empSalary < 3000"> emp_salary=#{empSalary}, </if> </set> where emp_id=#{empId} </update> 2.3 CHOOSE/WHEN/OTHERWISE 1 2 3 4 5 6 7 8 9 10 <!-- List<Employee> selectEmployeeByConditionByChoose(Employee employee) --> <select id="selectEmployeeByConditionByChoose" resultType="com.atguigu.mybatis.entity.Employee"> select emp_id,emp_name,emp_salary from t_emp where <choose> <!--在多个分支条件中,仅执行一个。--> <when test="empName != null">emp_name=#{empName}</when> <when test="empSalary < 3000">emp_salary < 3000</when> <otherwise>1=1</otherwise> </choose> </select> 2.4 FOREACH 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!-- collection属性:要遍历的集合 [arg0, collection, list] 或者@Param("empList")指定名字 item属性:遍历集合的过程中能得到每一个具体对象,在item属性中设置一个名字,将来通过这个名字引用遍历出来的对象 separator属性:指定当foreach标签的标签体重复拼接字符串时,各个标签体字符串之间的分隔符 open属性:指定整个循环把字符串拼好后,字符串整体的前面要添加的字符串 close属性:指定整个循环把字符串拼好后,字符串整体的后面要添加的字符串 index属性:这里起一个名字,便于后面引用 遍历List集合,这里能够得到List集合的索引值 遍历Map集合,这里能够得到Map集合的key --> <insert id = "insertEmp"> insert into emp (emp_name, emp_salary) <foreach collection="empList" item="emp" separator=","> (#{emp.empName},#{emp.empSalary}) </foreach> </insert> 2.5 SQL片段1 2 3 4 5 6 7 <!-- 使用sql标签抽取重复出现的SQL片段 --> <sql id="mySelectSql"> select emp_id,emp_name,emp_age,emp_salary,emp_gender from t_emp </sql> <!-- 使用include标签引用声明的SQL片段 --> <include refid="mySelectSql"/> 3 PageHelper插件引入依赖1 2 3 4 5 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.11</version> </dependency> 配置插件1 2 3 4 5 6 <!-- mybatis-config.xml--> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="helperDialect" value="mysql"/> </plugin> </plugins> 使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 @Test public void testTeacherRelationshipToMulti() { TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class); PageHelper.startPage(1,2); //pageNum pageSize // 查询Customer对象同时将关联的Order集合查询出来 List<Teacher> allTeachers = teacherMapper.findAllTeachers(); // 将数据封装到PageInfo实体类 PageInfo<Teacher> pageInfo = new PageInfo<>(allTeachers); System.out.println("pageInfo = " + pageInfo); long total = pageInfo.getTotal(); // 获取总记录数 System.out.println("total = " + total); int pages = pageInfo.getPages(); // 获取总页数 System.out.println("pages = " + pages); int pageNum = pageInfo.getPageNum(); // 获取当前页码 System.out.println("pageNum = " + pageNum); int pageSize = pageInfo.getPageSize(); // 获取每页显示记录数 System.out.println("pageSize = " + pageSize); List<Teacher> teachers = pageInfo.getList(); //获取查询页的数据集合 System.out.println("teachers = " + teachers); teachers.forEach(System.out::println); }