mybatis条件构造器(二)
1 准备工作
1.1 建表sql语句(Emp表)
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
`EMPNO` int NOT NULL AUTO_INCREMENT,
`ENAME` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`JOB` varchar(9) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`MGR` double NULL DEFAULT NULL,
`HIREDATE` date NULL DEFAULT NULL,
`SAL` double NULL DEFAULT NULL,
`COMM` double NULL DEFAULT NULL,
`DEPTNO` int NULL DEFAULT NULL,
PRIMARY KEY (`EMPNO`) USING BTREE,
INDEX `DEPTNO`(`DEPTNO`) USING BTREE,
CONSTRAINT `emp_ibfk_1` FOREIGN KEY (`DEPTNO`) REFERENCES `dept` (`DEPTNO`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 8899 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `emp` VALUES (7369, '老张123', 'CLERK', 7902, '1980-12-30', 1200, 0, 20);
INSERT INTO `emp` VALUES (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1700, 300, 30);
INSERT INTO `emp` VALUES (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30);
INSERT INTO `emp` VALUES (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 3975, NULL, 20);
INSERT INTO `emp` VALUES (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30);
INSERT INTO `emp` VALUES (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, NULL, 30);
INSERT INTO `emp` VALUES (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, NULL, 10);
INSERT INTO `emp` VALUES (7788, 'SCOTT', 'ANALYST', 7566, '1987-07-13', 4000, NULL, 20);
INSERT INTO `emp` VALUES (7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000, NULL, 10);
INSERT INTO `emp` VALUES (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30);
INSERT INTO `emp` VALUES (7876, 'ADAMS', 'CLERK', 7788, '1987-07-13', 2100, NULL, 20);
INSERT INTO `emp` VALUES (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, NULL, 30);
INSERT INTO `emp` VALUES (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 4000, NULL, 20);
INSERT INTO `emp` VALUES (8896, '小明', 'MANAGER', 7698, '2023-01-02', 6666, 555, 10);
INSERT INTO `emp` VALUES (8897, '小红', 'SALESMAN', 7698, '2023-02-08', 5000, 1000, 10);
INSERT INTO `emp` VALUES (8898, 'zhang1', 'SALESMAN', 7698, '2023-01-31', 1243, 234, 10);
INSERT INTO `emp` VALUES (8900, '雄安', 'MANAGER', 7698, '2023-02-05', 13, 123, 10);
SET FOREIGN_KEY_CHECKS = 1;
1.2 准备好测试环境
import com.zlz.ShiroStart2;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(classes = ShiroStart2.class,webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class DemoTest {
}
2 使用QueryMapper实现修改功能
2.1 题目内容
将员工姓名中包含a,薪水在1500-2000之间或者津贴为555的员工信息进行修改
2.2 对应的sql语句
UPDATE emp SET comm=800.0 WHERE (ename LIKE '%a%' AND sal BETWEEN 1500 AND 2000 OR comm = 555)
2.3 示例代码
@Test
public void a4(){
QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
queryWrapper.like("ename",'a')
.between("sal", 1500, 2000)
.or()
.eq("comm",555.0);
Emp emp=new Emp();
emp.setComm(800.0);
int result = empMapper.update(emp,queryWrapper);
System.out.println("受影响行数: "+result);
}
2.4 运行截图
2.4.1 实际执行的sql语句截图
2.4.2 数据显示截图
2.4.3 删除前数据库的数据
2.4.4 删除后数据库的数据
3 条件优先级处理
3.1 题目内容
将员工姓名中包含all并且(薪水在1500-2000之间或者津贴为555)的员工信息进行修改,修改津贴comm为1200
3.2 对应的sql语句
UPDATE emp SET comm=1200.0 WHERE (ename LIKE "%all%" AND (sal BETWEEN 1500 AND 2000 OR comm = 555))
3.3 示例代码
@Test
public void a5(){
QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
queryWrapper.like("ename","all")
.and(i->i.between("sal", 1500, 2000).or().eq("comm", 555));
Emp emp=new Emp();
emp.setComm(1200.0);
int result = empMapper.update(emp,queryWrapper);
System.out.println("受影响行数: "+result);
}
3.4 运行截图
3.4.1 实际执行的sql语句截图
3.4.2 数据显示截图
3.4.3 删除前数据库的数据
3.4.4 删除后数据库的数据
4 查询表中的部分字段(map集合)
4.1 题目内容
#查询所有员工的员工姓名、入职日期、薪水
4.2 对应的sql语句
SELECT ename,hiredate,sal FROM emp
4.3 示例代码
@Test
public void a6(){
QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
queryWrapper.select("ename","hiredate","sal");
List<Map<String, Object>> maps =empMapper.selectMaps(queryWrapper);
maps.forEach(System.out::println);
}
4.4 运行截图
4.4.1 实际执行的sql语句截图
4.4.2 数据显示截图
5 实现子查询
5.1 题目内容
#查询员工薪水小于2000的所有员工信息(使用子查询的方式)
5.2 对应的sql语句
SELECT EMPNO,ename,job,mgr,hiredate,sal,comm,deptno FROM emp WHERE (empno IN (SELECT empno FROM emp WHERE sal<2000))
5.3 示例代码
@Test
public void a7(){
QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
queryWrapper.inSql("empno","SELECT empno FROM emp WHERE sal<2000");
List<Emp> emps = empMapper.selectList(queryWrapper);
emps.forEach(System.out::println);
}
5.4 运行截图
5.4.1 实际执行的sql语句截图
5.4.2 数据显示截图