1、建库建表
create database `mybatis-example`;
use `mybatis-example`;
create table emp
(
empNo varchar(40),
empName varchar(100),
sal int,
deptno varchar(10)
);
insert into emp values('e001','张三',8000,'d001');
insert into emp values('e002','李四',9000,'d001');
insert into emp values('e003','王五',10000,'d001');
insert into emp values('e004','赵六',11000,'d002');
insert into emp values('e005','田七',12000,'d002');
insert into emp values('e006','周八',13000,'d003');
insert into emp values('e007','吴九',14000,'d004');
insert into emp values('e008','郑十',15000,'d004');
insert into emp values('d8f9b794-95ba-11ee-ab75-84a93ea22bfb','张三丰',16000,'d001');
2、pom.xml
org.mybatis
mybatis
mysql
mysql-connector-java
org.junit.jupiter
junit-jupiter-api
org.projectlombok
lombok
ch.qos.logback
logback-classic
3、Emp.java
package com.atguigu.mybatis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
private String empNo;
private Integer sal;
private String empName;
private String deptno;
}
4、EmpMapper.java
package com.atguigu.mybatis.mapper;
import com.atguigu.mybatis.pojo.Emp;
import java.util.List;
public interface EmpMapper {
//1.演示where-if标签
List getEmpList(Emp emp);
//2、演示set标签
void updateEmp(Emp emp);
}
5、mybatis-config.xml
6、mappers/EmpMapper.xml
select * from emp
empNo=#{empNo}
and empName=#{empName}
and sal < #{sal}
and deptno=#{deptno}
update emp
empName=#{empName},
sal=#{sal},
deptno=#{deptno}
where empNo=#{empNo}
7、logback.xml
[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger] [%msg]%n
UTF-8
8、MybatisTest.java
package mybatis;
import com.atguigu.mybatis.mapper.EmpMapper;
import com.atguigu.mybatis.pojo.Emp;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
public class MybatisTest {
SqlSessionFactory sqlSessionFactory;
SqlSession sqlSession;
EmpMapper empMapper;
@BeforeEach
public void setup() throws IOException {
// 获取资源流,读取"mybatis-config.xml"文件
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
// 使用资源流创建SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 使用SqlSessionFactory打开一个Session
sqlSession = sqlSessionFactory.openSession();
//基于Mapper接口编程
empMapper=sqlSession.getMapper(EmpMapper.class);
}
// 在每个测试用例之后执行的清理方法
@AfterEach
public void teardown() {
sqlSession.commit(); // 提交事务
sqlSession.close(); // 关闭SqlSession
}
@Test
public void selectWhereIFTest() {
/**
* 1、所有条件都不满足
*/
Emp emp = new Emp();
empMapper.getEmpList(emp).forEach(System.out::println);//select * from emp
//Emp(empNo=e001, sal=8000, empName=张三, deptno=d001)
//Emp(empNo=e002, sal=9000, empName=李四, deptno=d001)
//Emp(empNo=e003, sal=10000, empName=王五, deptno=d001)
//Emp(empNo=e004, sal=11000, empName=赵六, deptno=d002)
//Emp(empNo=e005, sal=12000, empName=田七, deptno=d002)
//Emp(empNo=e006, sal=13000, empName=周八, deptno=d003)
//Emp(empNo=e007, sal=14000, empName=吴九, deptno=d004)
//Emp(empNo=e008, sal=15000, empName=郑十, deptno=d004)
//Emp(empNo=d8f9b794-95ba-11ee-ab75-84a93ea22bfb, sal=16000, empName=张三丰, deptno=d001)
/**
* 2、一个条件满足
*/
Emp emp1 = new Emp();
emp1.setEmpNo("e002");
empMapper.getEmpList(emp1).forEach(System.out::println);//select * from emp WHERE empNo=?
//Emp(empNo=e002, sal=9000, empName=李四, deptno=d001)
Emp emp2 = new Emp();
emp2.setSal(11000);
empMapper.getEmpList(emp2).forEach(System.out::println);//select * from emp WHERE sal < ?
//Emp(empNo=e001, sal=8000, empName=张三, deptno=d001)
//Emp(empNo=e002, sal=9000, empName=李四, deptno=d001)
//Emp(empNo=e003, sal=10000, empName=王五, deptno=d001)
Emp emp3 = new Emp();
emp3.setEmpName("张三丰");
empMapper.getEmpList(emp3).forEach(System.out::println);//select * from emp WHERE empName=?
//Emp(empNo=d8f9b794-95ba-11ee-ab75-84a93ea22bfb, sal=16000, empName=张三丰, deptno=d001)
Emp emp4 = new Emp();
emp4.setDeptno("d001");
empMapper.getEmpList(emp4).forEach(System.out::println);//select * from emp WHERE deptno=?
//Emp(empNo=e001, sal=8000, empName=张三, deptno=d001)
//Emp(empNo=e002, sal=9000, empName=李四, deptno=d001)
//Emp(empNo=e003, sal=10000, empName=王五, deptno=d001)
//Emp(empNo=d8f9b794-95ba-11ee-ab75-84a93ea22bfb, sal=16000, empName=张三丰, deptno=d001)
/**
* 2、两个条件满足
*/
Emp emp5 = new Emp();
emp5.setEmpNo("e002");
emp5.setSal(11000);
empMapper.getEmpList(emp5).forEach(System.out::println);//select * from emp WHERE empNo=? and sal
//Emp(empNo=e002, sal=9000, empName=李四, deptno=d001)
/**
* 3、三个条件都满足,以次类推
*/
Emp emp6 = new Emp();
emp6.setEmpNo("e002");
emp6.setSal(11000);
emp6.setEmpName("张三丰");
empMapper.getEmpList(emp6).forEach(System.out::println);//select * from emp WHERE empNo=? and empName=? and sal < ?
/**
* 4、全部条件满足
*/
Emp emp100 = new Emp("e001",8001,"张三","d001");
empMapper.getEmpList(emp100).forEach(System.out::println);//select * from emp WHERE empNo=? and empName=? and sal < ? and deptno=?
//Emp(empNo=e001, sal=8000, empName=张三, deptno=d001)
}
@Test
public void updateSetIfTest() {
/**
* 1、所有条件都满足
*/
Emp emp = new Emp("e001",4000,"张三","d001");
empMapper.updateEmp(emp);//update emp SET empName=?, sal=?, deptno=? where empNo=?
/**
* 2、部分条件满足
*/
Emp emp1 = new Emp();
emp1.setEmpNo("e002");
emp1.setEmpName("张无忌");
empMapper.updateEmp(emp1);//update emp SET empName=? where empNo=?
/**
* 3、所有条件都不满足,报错SQLSyntaxErrorException
*/
Emp emp2 = new Emp();
empMapper.updateEmp(emp2);//update emp where empNo=?
}
}