MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.5version>
dependency>
MP要求以面向对象的方式操作数据库.其中对象与表 属性与字段必须映射。所以利用注解进行绑定
MP增强了Mybatis, MP内部包含了Mybatis 所以将Mybatis的包删除,否则内部jar包异常
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>3.1.8version>
<relativePath/>
parent>
<groupId>com.jtgroupId>
<artifactId>springboot_demo_2artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>springboot_demo_2name>
<description>springboot_demo_2description>
<properties>
<java.version>17java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.20version>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.5version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>
project>
YML配置文件中将mybatis名称为mybatis-plus即可
利用userMapper 实现以对象的方式操作数据库
//测试mybatis-plus
@Test
public void testSelect(){
List<DemoUser> userList = userMapper.selectList(null);
System.out.println(userList);
}
之前操作数据库采用sql(面向过程的语言)方法进行编辑. 但是如果所有的单表操作都由程序员完成.则开发效率低. 能否开发一种机制可以实现以面向对象的方式操作数据库.
server:
port: 8090
#SpringBoot 开箱即用
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/jtadmin?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
#检查密码是否正确
password: root
#SpringBoot整合MybatisPlus配置
mybatis-plus:
#定义别名包
type-aliases-package: com.jt.pojo
mapper-locations: classpath:/mappers/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
#添加MP日志 打印执行的sql
logging:
level:
com.jt.mapper: debug
import com.jt.mapper.DemoUserMapper;
import com.jt.pojo.DemoUser;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class TestMP {
@Autowired
private DemoUserMapper userMapper;
@Test
public void insert(){
DemoUser user = new DemoUser();
user.setName("MP测试").setSex("男").setAge(19);
userMapper.insert(user);
}
}
//测试更新操作 修改id=231的数据 name="中午吃什么" age=18
//原则: 根据对象中不为null的属性当做set条件. set name="xxx"
// 如果ById的操作,则Id必须赋值 并且ID当做唯一where条件
@Test
public void updateById(){
DemoUser user = new DemoUser();
user.setName("中午吃什么").setAge(18).setId(231);
userMapper.updateById(user);
}
/**
* 1.查询id=21的用户 根据ID查询数据 1条记录
* 2.查询name="白骨精" sex=女 的用户 List
* 知识点:
* 1.queryWrapper 条件构造器 拼接where条件的.
* 原则: 根据对象中不为null的属性拼接where条件
*/
@Test
public void testSelect(){
//1.根据ID查询
DemoUser user = userMapper.selectById(21);
System.out.println(user);
//2.根据属性查询
DemoUser user2 = new DemoUser();
user2.setName("白骨精").setSex("女");
QueryWrapper<DemoUser> queryWrapper = new QueryWrapper<>(user2);
List<DemoUser> userList = userMapper.selectList(queryWrapper);
System.out.println(userList);
}
说明: 如果查询条件中有特殊关系符,则使用特殊转义字符查询 代码如下.
/**
* 需求: 查询age>18岁 并且性别为女的用户
* Sql: select * from demo_user where age > 18 and sex="女"
* 特殊字符: > gt < lt = eq
* >= ge <= le
* 默认链接符: and
*
* */
@Test
public void testSelect2(){
QueryWrapper<DemoUser> queryWrapper = new QueryWrapper<>();
queryWrapper.gt("age", 18) .eq("sex", "女");
List<DemoUser> userList = userMapper.selectList(queryWrapper);
System.out.println(userList);
}
/**
* 练习like关键字
* 查询name中包含"精"字的数据
* Sql: like "%精%"
* 以精开头 like "精%" likeRight
* 以精结尾 like "%精" likeleft
*/
@Test
public void testSelect3(){
QueryWrapper<DemoUser> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name", "精");
List<DemoUser> userList = userMapper.selectList(queryWrapper);
System.out.println(userList);
}
/**
* 查询sex=男的数据,以id倒序排列
* Sql: select * from demo_user where sex='男' order by id desc
*/
@Test
public void testSelect4(){
QueryWrapper<DemoUser> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("sex", "男").orderByDesc("id");
List<DemoUser> userList = userMapper.selectList(queryWrapper);
System.out.println(userList);
}
/**
* 5.查询id= 1,3,5,6,7的用户
* Sql: select * from demo_user where id in (xxx,xx,xx)
*/
@Test
public void testSelect5(){
//数组使用包装类型
Integer[] ids = {1,3,5,6,7};
//需要将数组转化为集合
List idList = Arrays.asList(ids);
QueryWrapper<DemoUser> queryWrapper = new QueryWrapper<>();
//queryWrapper.in("id", idList); //根据list查询 list集合功能丰富
queryWrapper.in("id", ids); //数组必须包装类型
List<DemoUser> userList = userMapper.selectList(queryWrapper);
System.out.println(userList);
}
/**
* 需求: 如果根据name属性和age属性查询数据. 有时某个数据可能为null,
* 要求动态查询 where name=xxx age>xxxx
* 伪Sql: select * from demo_user where name!=null name=xxx and age!=null age>xxx
* condition: 内部编辑一个判断的条件
* 如果返回值结果为true 则拼接该字段.
* 如果为false 则不拼接该字段
* StringUtils.hasLength(name) 判断字符串是否有效
*/
@Test
public void testSelect6(){
QueryWrapper<DemoUser> queryWrapper = new QueryWrapper<>();
String name = "";
int age = 18;
queryWrapper.eq(StringUtils.hasLength(name),"name",name).gt(age>0, "age",age);
List<DemoUser> userList = userMapper.selectList(queryWrapper);
System.out.println(userList);
}
/**
* 需求: 只想查询第一列数据 selectObjs
* 说明: queryWrapper=null 不需要where条件
* selectObjs:
* 1.一般根据条件查询Id的值,查询之后为后续的sql提供数据支持
* 2. 有时用户只需要查询ID的值,并不需要其他数据项时 使用objs.
*/
@Test
public void testSelect7(){
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq("sex","男");
List objs = userMapper.selectObjs(queryWrapper);
System.out.println(objs);
}
/**
* 需求: 想查询name/sex字段
* queryWrapper.select("name","sex"); 挑选执行字段
*/
@Test
public void testSelect8(){
QueryWrapper<DemoUser> queryWrapper = new QueryWrapper();
queryWrapper.select("name","sex");
List objs = userMapper.selectList(queryWrapper);
System.out.println(objs);
}
/**
* 更新数据
* 将name="中午吃什么" 改为name="晚上吃什么"
* 性别: 改为 其他
* Sql:
* update demo_user set name="xxx",sex="其他" where name="xxxx"
* 参数说明:
* 1.entity 实体对象 需要修改的数据进行封装
* 2.updateWrapper 条件构造器
*/
@Test
public void testSelect10(){
DemoUser user = new DemoUser();
user.setName("晚上吃什么").setSex("其他");
UpdateWrapper updateWrapper = new UpdateWrapper();
updateWrapper.eq("name", "中午吃什么");
userMapper.update(user,updateWrapper);
}