MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
废话不多说,直接开干!!!!!!!!!!
创建数据库所有的增删改查都是围绕数据库在做的,所以建表是第一步。
DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键id', `name` varchar(256) NOT NULL COMMENT '名字', `age` int(5) NOT NULL COMMENT '年龄', `sex` int(1) NOT NULL COMMENT '性别', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=utf8mb4;创建项目,引入依赖
没有依赖怎么使用技术呢?pom文件如下:
配置文件4.0.0 org.springframework.boot 2.2.2.RELEASE spring-boot-starter-parentnet.zyy mybatis-test1.0-SNAPSHOT mysql mysql-connector-javacom.baomidou mybatis-plus-boot-starter3.3.1.tmp org.springframework.boot spring-boot-starter-weborg.projectlombok lombok
配置文件肯定就配置一下数据库之类的了:
类的编写pojo类:
@TableName("test")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Test {
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
private Integer age;
private Integer sex;
}
启动类: 一定要扫描到mapper类所在的包
@SpringBootApplication
@MapperScan("net.zyy.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
TestMapper类:直接继承BaseMapper类即可
public interface TestMapper extends BaseMapper数据插入:{ }
当前数据库是空空的,所以就先插入点数据吧:
@RestController
public class TestController {
@Autowired
TestMapper testMapper;
@RequestMapping("/save")
public void save(){
String source = "abcdefghijklmnopqrstuvwxyz";
Random random = new Random();
Test test = new Test();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100; i++) {
test.setAge(i);
if (i % 2 == 0){
test.setSex(0);
}else {
test.setSex(1);
}
for (int j = 0; j < 3; j++) {
int index = random.nextInt(source.length());
sb.append(source.charAt(index));
}
test.setName(sb.toString());
sb = new StringBuilder();
testMapper.insert(test);
}
}
}
直接@Autowire注入TestMapper,调用insert()方法即可插入数据,无需再编写SQL语句,xml文件,偷懒之必备神器,访问http://localhost:8000/save即可向数据库里插入一百条记录。
数据查找查找所有数据:
@GetMapping("getList")
public List getList(){
QueryWrapper queryWrapper = new QueryWrapper<>();
List tests = testMapper.selectList(queryWrapper);
return tests;
}
只需要调用selectList()方法,并传入QueryWrapper对象,因为是查找所有数据,所以无需添加查询条件,这样访问http://localhost:8000/getList即可查询所有数据。
根据Id查找数据:
@GetMapping("getOne")
public Test getOne(){
Test test = testMapper.selectById(1);
return test;
}
直接调用selectById,传入Id即可,简单又好用。
批量Id查询:
@GetMapping("getBatchIds")
public List getBatchIds(){
List list = testMapper.selectBatchIds(Arrays.asList(1,2,3));
return list;
}
根据条件批量查询:
查询sex为1的所有数据:
@GetMapping("selectList")
public List selectList(){
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("sex",1);
List list = testMapper.selectList(queryWrapper);
return list;
}
数据修改
@GetMapping("update")
public Integer update(){
//根据Id修改
testMapper.updateById();
//根据条件修改,都返回int类型,表示影响的行数
testMapper.update();
}
数据删除
@RequestMapping("delete")
public Integer delete(){
//根据wrapper条件删除
testMapper.delete();
//根据Id删除,都返回int类型,表示影响的行数
testMapper.deleteById();
//批量根据Id删除
testMapper.deleteBatchIds();
}
我们发现大多数的操作都与Wrapper建立着联系,下一章将透过源码仔细讲解Wrapper类的作用



