1,创建mysql数据表
DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
-- 真实开发中,version(乐观锁),deleted(逻辑删除)、gmt_create、gem_mo
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');
2,导入相应的依赖
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.4.2
com.sin
mybatis-plus
0.0.1-SNAPSHOT
mybatis-plus
Demo project for Spring Boot
1.8
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.4
mysql
mysql-connector-java
runtime
com.baomidou
mybatis-plus-boot-starter
3.4.2
com.h2database
h2
runtime
org.springframework.boot
spring-boot-starter-test
test
junit
junit
test
junit
junit
test
org.springframework.boot
spring-boot-maven-plugin
3,创建实例类com.sin.pojo
package com.sin.pojo;
@Data
public class User {
private long id;
private String name;
private long age;
private String email;
}
4,创建Mapper,com.sin.mapper
package com.sin.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sin.pojo.User;
//在对应的类上继承基本的类baseMapper
public interface UserMapper extends BaseMapper {
//所有的CRUD已经编写完成
//不需要像以前的配置一些xml
}
5,配置mysql数据库的相关信息
## 配置mysql数据库的相关信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/sin?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
password: 123456
username: root
## 运行时显示sql语句
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
6,在springboot启动类中扫描mapper文件夹
package com.sin;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.sin.mapper")//扫描mapper文件夹
public class MybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusApplication.class, args);
}
}
7,测试
package com.sin;
import com.sin.mapper.UserMapper;
import com.sin.pojo.User;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
class MybatisPlusApplicationTests {
@Autowired(required = false)
private UserMapper userMapper;
@Test
public void testSelect(){
System.out.println("-------selectAll方法开始测试 -------");
//查询出一个Wrapper,条件构造器,这里先不用填null
//查询所有用户,
/**
* selectlist()方法的参数为mybatis-plus内置的条件封装器wrapper,所以不填写就是无条件查询
*/
List userList = userMapper.selectList(null);
Assert.assertEquals(5,userList.size());
userList.forEach(System.out::println);
}
}
8,运行结果
-------selectAll方法开始测试 -------
User{id=1, name='Jone', age=18, email='[email protected]'}
User{id=2, name='Jack', age=20, email='[email protected]'}
User{id=3, name='Tom', age=28, email='[email protected]'}
User{id=4, name='Sandy', age=21, email='[email protected]'}
User{id=5, name='Billie', age=24, email='[email protected]'}
-------selectAll方法测试结束 -------
9,根据主键查询一个
/**
* 根据主键查询一个
*
*/
@Test
public void findById(){
System.out.println("--------selectById方法开始测试------------");
User user = userMapper.selectById("2");
System.out.println("user = "+user);
System.out.println("--------selectById方法测试结束------------");
}
10,运行结果
--------selectById方法开始测试------------
user = User{id=2, name='Jack', age=20, email='[email protected]'}
--------selectById方法测试结束------------
11,条件查询
/**
* 条件查询
*/
@Test
public void find(){
System.out.println("-------find方法开始测试 -------");
QueryWrapper queryWrapper=new QueryWrapper<>();
queryWrapper.eq("age",20);//查询出“age”字段,为20的相应数据
queryWrapper.lt("age",20);//查询出“age”字段,小于20的相应数据
queryWrapper.le("age",20);//查询出“age”字段,小于等于20的相关数据
//gt大于,ge大于等于
queryWrapper.gt("age",20);//查询出“age”字段,大于20的相关数据
queryWrapper.ge("age",20);//查询出“age”字段,大于等于20的相关数据
List userList=userMapper.selectList(queryWrapper);
userList.forEach( System.out::println);
System.out.println("-------find方法测试结束 -------");
}
12,运行结果
-------find方法开始测试 -------
User{id=2, name='Jack', age=20, email='[email protected]'}
-------find方法测试结束 -------
13,模糊查询
/**
* 模糊查询
*/
@Test
public void testFindLike(){
System.out.println("-------findLike方法开始测试 -------");
QueryWrapper queryWrapper=new QueryWrapper<>();
//like %?%
// likeLeft是%? 以XXX结尾
// likeRight是?% 以XXX开头
queryWrapper.likeRight("name","J");
List userlist = userMapper.selectList(queryWrapper);
userlist.forEach(user-> System.out.println(" user = "+user));
System.out.println("-------findLike方法测试结束 -------");
}
14,运行结果
-------findLike方法开始测试 -------
user = User{id=1, name='Jone', age=18, email='[email protected]'}
user = User{id=2, name='Jack', age=20, email='[email protected]'}
-------findLike方法测试结束 -------
15,添加操作
/**
* 添加操作
*/
@Test
public void Save(){
System.out.println("-------insert方法开始测试 -------");
User user=new User();
user.setName("张三");
user.setAge(23);
user.setEmail("[email protected]");
userMapper.insert(user);
System.out.println("-------insert方法测试结束 -------");
}
16,运行结果
user = User{id=6, name='张三', age=23, email='[email protected]'}
17,修改操作(根据id查询)
/**
* 修改操作
* 基于主键id进行数据的修改
*/
@Test
public void Update(){
System.out.println("-------updateById方法测试结束 -------");
User user=userMapper.selectById("2");
user.setName("王麻子");
userMapper.updateById(user);
System.out.println("-------updateById方法测试结束 -------");
}
18,修改操作(批量修改)
/**
* 批量修改
*/
@Test
public void UpdateTest(){
User user=userMapper.selectById("3");
user.setName("里斯");
QueryWrapper userQueryWrapper=new QueryWrapper<>();
userQueryWrapper.eq("age",20);//将“age”为20的数据,name修改为里斯
userMapper.update(user,userQueryWrapper);
System.out.println(user);
}
19,运行结果
User{id=3, name='里斯', age=28, email='[email protected]'}
20,删除操作(基于id删除)
/**
* 删除方法(基于id删除)
*/
@Test
public void DeleteById(){
int i = userMapper.deleteById(6);
System.out.println("删除了"+ i +"条数据");
}
21,运行结果
删除了1条数据
22,分页查询
1,创建com.sin.config.MybatisPlusConfig
package com.sin.config;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@Configuration
@MapperScan("com.sin.mapper")
public class MybatisPlusConfig {
//分页的拦截器对象
@Bean
public PaginationInnerInterceptor paginationInnerInterceptor(){
PaginationInnerInterceptor paginationInnerInterceptor=new PaginationInnerInterceptor();
return paginationInnerInterceptor;
}
}
2,分页测试
/**
* 分页查询
*/
@Test
public void FindPage(){
//参数1:当前页:默认为1, 参数2:每页显示的记录数,默认为10
IPage userIPage=new Page<>(1,2);//这里改为当前页为1,每页记录数为2条数据
IPage userIPage1=userMapper.selectPage(userIPage,null);
long total = userIPage1.getTotal();//获取总条数
System.out.println("总记录数"+total);
userIPage1.getRecords().forEach(user -> System.out.println("user="+user));//遍历数据
}