1. 首先我们要建立一个Maven项目,pom.xml:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.7.RELEASE
top.itreatment
mybatis-plus-study
0.0.1-SNAPSHOT
mybatis-plus-study
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
true
com.baomidou
mybatis-plus-boot-starter
3.1.2
mysql
mysql-connector-java
8.0.17
org.springframework.boot
spring-boot-maven-plugin
2. 配置文件application.yml
如下:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&useUnicode=true&useSSL=false&characterEncoding=utf8
username: root
password:
3. 实体类:
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
4. 实体类对应的mapper接口
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import top.itreatment.mybatisplusstudy.entity.User;
public interface UserMapper extends BaseMapper {
}
完成上面的准备就可以开始使用了
下面是使用内置对象的小例子:
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.minidev.json.JSONUtil;
import org.junit.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.SpringJUnit4ClassRunner;
import top.itreatment.entity.UserEntity;
import top.itreatment.mapper.UserDao;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class MainTest {
@Autowired
private UserDao userDao;
/**
* 测试插入操作
* Preparing: INSERT INTO user ( name, age, email ) VALUES ( ?, ?, ? )
* Parameters: zjb(String), 24(Integer), 5924(String)
*/
@Test
public void testInsert01() {
UserEntity zjb = UserEntity.builder().name("zjb").age(24).email("5924").build();
int insert = userDao.insert(zjb);
System.out.println("insert = " + insert);
System.out.println("zjb.getId() = " + zjb.getId());
}
/**
* 测试更新操作
* Preparing: UPDATE user SET name=?, age=?, email=? WHERE id=?
* Parameters: YJN(String), 24(Integer), 5924(String), 6(Long)
*/
@Test
public void testUpdate01() {
UserEntity zjb = UserEntity.builder().id(6L).name("YJN").age(24).email("5924").build();
int i = userDao.updateById(zjb);
System.out.println("i = " + i);
}
/**
* 测试更新操作02
* Preparing: UPDATE user SET name=? WHERE id=?
* Parameters: zjb(String), 10(Long)
*/
@Test
public void testUpdate02() {
UpdateWrapper userEntityUpdateWrapper = new UpdateWrapper<>();
userEntityUpdateWrapper.set("name", "zjb");
userEntityUpdateWrapper.setEntity(UserEntity.builder().id(10L).build());
userDao.update(userEntityUpdateWrapper.getEntity(), userEntityUpdateWrapper);
}
/**
* 测试删除操作01
* Preparing: DELETE FROM user WHERE id=?
* Parameters: 1(Long)
* Updates: 1
*/
@Test
public void testDelete01() {
userDao.deleteById(1L);
}
/**
* 测试删除操作02
* Preparing: DELETE FROM user WHERE id IN ( ? , ? )
* Parameters: 2(Long), 3(Long)
* Updates: 2
*/
@Test
public void testDelete02() {
userDao.deleteBatchIds(Arrays.asList(2L,3L));
}
/**
* 测试删除操作03
* Preparing: DELETE FROM user WHERE name = ?
* Parameters: zjb(String)
* Updates: 2
*/
@Test
public void testDelete03() {
HashMap hashMap = new HashMap<>();
hashMap.put("name","zjb");
userDao.deleteByMap(hashMap);
}
/**
* 测试删除操作04
* Preparing: DELETE FROM user WHERE name=?
* Parameters: YJN(String)
* Updates: 1
*/
@Test
public void testDelete04() {
UpdateWrapper wrapper = new UpdateWrapper<>();
wrapper.setEntity(UserEntity.builder().name("YJN").build());
userDao.delete(wrapper);
}
/**
* 测试查询操作01
* Preparing: SELECT id,name,age,email FROM user
* Parameters:
*/
@Test
public void testSelect01() {
List userEntities = userDao.selectList(null);
userEntities.forEach(System.out::println);
}
/**
* 测试查询操作02
* Preparing: SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
* Parameters: zjb(String), 24(Integer)
*
* Preparing: SELECT id,name,age,email FROM user WHERE name=? AND age=?
* Parameters: zjb(String), 24(Integer)
* SelectOne sql查到的结果只能是一条或是零条,多条会报错
*/
@Test
public void testSelect02() {
QueryWrapper wrapper = new QueryWrapper<>();
// wrapper.eq("name","zjb");
// wrapper.eq("age",24);
wrapper.setEntity(UserEntity.builder().name("zjb").age(24).build());
userDao.selectOne(wrapper);
}
/**
* 测试查询操作03
* Preparing: SELECT id,name,age,email FROM user WHERE name = ?
* Parameters: zjb(String)
* Total: 2
*/
@Test
public void testSelect03() {
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("name", "zjb");
userDao.selectList(wrapper);
}
/**
* 测试查询操作04
* Preparing: SELECT id,name,age,email FROM user WHERE name = ? AND email = ?
* Parameters: zjb(String), 5924(String)
* Total: 2
*/
@Test
public void testSelect04() {
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("name", "zjb");
wrapper.eq("email", "5924");
userDao.selectList(wrapper);
}
/**
* 测试查询操作05
* Preparing: SELECT id,name,age,email FROM user WHERE id IN ( ? , ? , ? )
* Parameters: 1(Long), 2(Long), 3(Long)
* Total: 3
*/
@Test
public void testSelect05() {
userDao.selectBatchIds(Arrays.asList(1L, 2L, 3L));
}
/**
* 测试查询操作06
* Preparing: SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
* Parameters: Parameters: zjb(String), 24(Integer)
* Total: 1
*/
@Test
public void testSelect06() {
Map emptyMap = new HashMap();
// key 为列名
emptyMap.put("name", "zjb");
emptyMap.put("age", 24);
userDao.selectByMap(emptyMap);
}
/**
* 测试查询操作07
* Preparing: SELECT id,name,age,email FROM user
* Parameters:
* Total: 11
*/
@Test
public void testSelect07() {
IPage userEntityIPage = userDao.selectPage(new Page<>(1, 5), null);
userEntityIPage.getRecords().forEach(System.out::println);
}
}
* Copyright (c) 2011-2020, baomidou ([email protected]).
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.core.enums;
/**
* MybatisPlus 支持 SQL 方法
*
* @author hubin
* @since 2016-01-23
*/
public enum SqlMethod {
/**
* 插入
*/
INSERT_ONE("insert", "插入一条数据(选择字段插入)", ""),
/**
* 删除
*/
DELETE_BY_ID("deleteById", "根据ID 删除一条数据", ""),
DELETE_BY_MAP("deleteByMap", "根据columnMap 条件删除记录", ""),
DELETE("delete", "根据 entity 条件删除记录", ""),
DELETE_BATCH_BY_IDS("deleteBatchIds", "根据ID集合,批量删除数据", ""),
/**
* 逻辑删除
*/
LOGIC_DELETE_BY_ID("deleteById", "根据ID 逻辑删除一条数据", ""),
LOGIC_DELETE_BY_MAP("deleteByMap", "根据columnMap 条件逻辑删除记录", ""),
LOGIC_DELETE("delete", "根据 entity 条件逻辑删除记录", ""),
LOGIC_DELETE_BATCH_BY_IDS("deleteBatchIds", "根据ID集合,批量逻辑删除数据", ""),
/**
* 修改
*/
UPDATE_BY_ID("updateById", "根据ID 选择修改数据", ""),
UPDATE_ALL_COLUMN_BY_ID("updateAllColumnById", "根据ID 选择修改数据", ""),
UPDATE("update", "根据 whereEntity 条件,更新记录", ""),
/**
* 逻辑删除 -> 修改
*/
LOGIC_UPDATE_BY_ID("updateById", "根据ID 修改数据", ""),
/**
* 查询
*/
SELECT_BY_ID("selectById", "根据ID 查询一条数据", "SELECT %s FROM %s WHERE %s=#{%s}"),
SELECT_BY_MAP("selectByMap", "根据columnMap 查询一条数据", ""),
SELECT_BATCH_BY_IDS("selectBatchIds", "根据ID集合,批量查询数据", ""),
SELECT_ONE("selectOne", "查询满足条件一条数据", ""),
SELECT_COUNT("selectCount", "查询满足条件总记录数", ""),
SELECT_LIST("selectList", "查询满足条件所有数据", ""),
SELECT_PAGE("selectPage", "查询满足条件所有数据(并翻页)", ""),
SELECT_MAPS("selectMaps", "查询满足条件所有数据", ""),
SELECT_MAPS_PAGE("selectMapsPage", "查询满足条件所有数据(并翻页)", ""),
SELECT_OBJS("selectObjs", "查询满足条件所有数据", ""),
/**
* 逻辑删除 -> 查询
*/
LOGIC_SELECT_BY_ID("selectById", "根据ID 查询一条数据", "SELECT %s FROM %s WHERE %s=#{%s} %s"),
LOGIC_SELECT_BATCH_BY_IDS("selectBatchIds", "根据ID集合,批量查询数据", "");
private final String method;
private final String desc;
private final String sql;
SqlMethod(String method, String desc, String sql) {
this.method = method;
this.desc = desc;
this.sql = sql;
}
public String getMethod() {
return method;
}
public String getDesc() {
return desc;
}
public String getSql() {
return sql;
}
}