Mybatisplus中BaseMapper下方法演示
一、BaseMapper下方法展示
二、SpringBoot项目结构
1.项目结构
其他的不重要
2.sql文件
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)
);
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]');
3.application.yml
# DataSource Config
spring:
datasource:
# 配置数据源信息
# type: com.zaxxer.hikari.HikariDataSource
type: com.alibaba.druid.pool.DruidDataSource
# 配置连接数据库的各个信息
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mytest?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
mybatis-plus:
configuration:
#默认不显示SQL日志
# log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
4.User
package com.cyl.entity;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* @Author cyl
* @create 2022/3/18
*/
@Data
@Accessors(chain = true)
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
5.UserMapper
package com.cyl.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cyl.entity.User;
import org.apache.ibatis.annotations.Mapper;
/**
* @Author cyl
* @create 2022/3/18
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
三、增
package com.cyl;
import com.cyl.entity.User;
import com.cyl.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* @Author cyl
* @create 2022/3/18
*/
@SpringBootTest
public class TestInsert {
@Autowired
UserMapper userMapper;
@Test
public void testInsert(){
System.out.println("----- insert method test ------");
User user = new User();
user.setName("cyl")
.setAge(23)
.setEmail("[email protected]");
// INSERT INTO user ( id, name, age, email ) VALUES ( 1504726979303559169, 'cyl', 23, '[email protected]' )
int result = userMapper.insert(user);
System.out.println("result="+result);
System.out.println("id="+user.getId());
}
}
四、删
package com.cyl;
import com.cyl.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author cyl
* @create 2022/3/18
*/
@SpringBootTest
public class TestDelete {
@Autowired
UserMapper userMapper;
/**
* 通过 id 删除
*/
@Test
public void testDeleteById(){
System.out.println("----- deleteById method test ------");
// DELETE FROM user WHERE id=1504726979303559169
int result = userMapper.deleteById(1504726979303559169L);
System.out.println("result="+result);
}
/**
* 通过 map 设置条件删除
* DELETE FROM user WHERE name = 'cyl' AND age = 22
*/
@Test
public void testDeleteByMap(){
System.out.println("----- deleteByMap method test ------");
Map<String,Object> map = new HashMap<>();
map.put("name","cyl");
map.put("age",22);
int result = userMapper.deleteByMap(map);
System.out.println("result="+result);
}
/**
* 通过 in( ) 批量删除
*/
@Test
public void testDeleteBatchIds(){
System.out.println("----- deleteBatchIds method test ------");
List<Long> idList = Arrays.asList(1L, 2L);
// DELETE FROM user WHERE id IN ( 1 , 2 )
int result = userMapper.deleteBatchIds(idList);
System.out.println("result="+result);
}
}
五、改
package com.cyl;
import com.cyl.entity.User;
import com.cyl.mapper.UserMapper;
import com.cyl.util.FunTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
/**
* @Author cyl
* @create 2022/3/18
*/
@SpringBootTest
public class TestUpdate {
@Autowired
UserMapper userMapper;
@Test
public void testUpdateById(){
System.out.println("----- updateById method test ------");
User user = new User();
user.setId(3L)
.setName("cyl")
.setEmail("[email protected]");
// UPDATE user SET name='cyl', email='[email protected]' WHERE id=3
int result = userMapper.updateById(user);
System.out.println(result);
}
}
六、查
package com.cyl;
import com.cyl.entity.User;
import com.cyl.mapper.UserMapper;
import com.cyl.util.FunTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author cyl
* @create 2022/3/18
*/
@SpringBootTest
public class TestSelect {
@Autowired
UserMapper userMapper;
@Test
public void testSelectAll(){
System.out.println("----- selectAll method test ------");
// SELECT id,name,age,email FROM user
List<User> userList = userMapper.selectList(null);
System.out.println("----- 测试函数式接口使用 :: 引用方法输出 ------");
userList.forEach(System.out::println);
}
@Test
public void testSelectById(){
System.out.println("----- selectById method test ------");
// SELECT id,name,age,email FROM user WHERE id=1
User user = userMapper.selectById(1L);
System.out.println(user);
}
@Test
public void testSelectBatchIds(){
System.out.println("----- selectBatchIds method test ------");
// SELECT id,name,age,email FROM user WHERE id IN ( 4 , 5 )
List<Long> idList = Arrays.asList(4L, 5L);
List<User> userList = userMapper.selectBatchIds(idList);
userList.forEach(System.out::println);
}
@Test
public void testSelectByMap(){
System.out.println("----- selectByMap method test ------");
// SELECT id,name,age,email FROM user WHERE id IN ( 4 , 5 )
Map<String,Object> map = new HashMap<>();
map.put("name","cyl");
map.put("age",23);
// SELECT id,name,age,email FROM user WHERE name = 'cyl' AND age = 23
List<User> userList = userMapper.selectByMap(map);
userList.forEach(System.out::println);
}
}