-
指定数据库表名注解:@TableName
MP默认主键名是id
指定表的主键注解:@TableId
实体属性名和表中字段名对应注解:@TableField("")
查看全部 -
selct排除字段
查看全部 -
/**
* @date 2020年2月23日
* @author 翁东州
* @所有方法的代码点击查看全文,整理了一整天累死了
*/
package first;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
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.SpringRunner;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.mp.dao.UserMapper;
import com.mp.entity.User;
import com.mp.service.UserService;
/**
* @date 2020年2月23日
* @author 翁东州
* @方法中文名称:
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SimpleTest {
@Autowired
private UserMapper userMapper;
//展示所有
@Test
public void select() {
List<User> list = userMapper.selectList(null);
Assert.assertEquals(5, list.size());
list.forEach(System.out::println);
System.out.println();
}
//传统添加
public void insert() {
User user = new User();
user.setName("名字");
user.setAge(31);
user.setManagerId(123L);
user.setCreateTime(LocalDateTime.now());
int rows = userMapper.insert(user);
System.out.println("影响记录数"+rows);
}
//单体展示
public void selectById() {
User user = userMapper.selectById(123L);
System.out.println(user);
}
//多条展示
public void selectByIds() {
List<Long> idList = Arrays.asList(123L,234L,345L);
List<User> users = userMapper.selectBatchIds(idList);
users.forEach(System.out::println);
}
//map形式查询
public void selectByMap() {
Map<String, Object> columnMap = new HashMap<>();
columnMap.put("name", "王天风");
columnMap.put("age", 25);
List<User> users = userMapper.selectByMap(columnMap);
users.forEach(System.out::println);
}
//wrapper 名字包含雨且年龄小于40
public void selectByWrapper() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.eq("name", "雨").lt("age", 40);
List<User> userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//wrapper 名字包含雨,且年龄在20到40之间,email不为空
public void selectByWrapper2() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.eq("name", "雨").between("age", 20, 40).isNotNull("email");
List<User> userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//wrapper 名字包含王,年龄大于等于25,按年龄降序排序,而后再按id升序排序
public void selectByWrapper3() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.likeRight("name", "王").or().ge("age", 25).orderByDesc("age").orderByAsc("id");
List<User> userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//创建日期为2019年2月14日且直属上级名字为王姓
public void selectByWrapper4() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.apply("date_format(create_time,'%Y-%m-%d')={0}", "2019-02-14 or true or true")
.inSql("manager_id", "select id from user where name like '王%'");
List<User> userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//名字为王姓,并且(年龄小于40或邮箱不为空)
public void selectByWrapper5() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.likeRight("name", "王").and(wq -> wq.lt("age", 40).or().isNotNull("email"));
List<User> userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//名字为王姓,或者(年龄小于40并且年龄大于20并且邮箱不为空)
public void selectByWrapper6() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.likeRight("name", "王").or(wq -> wq.between("age", 20, 40).isNotNull("email"));
//queryWrapper.likeRight("name", "王").or(wq -> wq.lt("age", 40).gt("age", 20).isNotNull("email"));
List<User> userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//名字为王姓,并且(年龄小于40或邮箱不为空)
public void selectByWrapper7() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.nested(wq -> wq.lt("age", 40).or().isNotNull("email")).likeRight("name", "王");
//queryWrapper.likeRight("name", "王").and(wq -> wq.lt("age", 40).or().isNotNull("email"));
List<User> userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//年龄为30,31,34,35
public void selectByWrapper8() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.in("age", Arrays.asList(30,31,34,35));
List<User> userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//只返回满足条件的其中一条语句即可
public void selectByWrapper9() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.in("age", Arrays.asList(30,31,34,35)).last("limit 1");
List<User> userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//名字中包含雨并且年龄小于40,只展示id和名字
public void selectByWrapperSupper() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.select("id","name").like("name", "雨").lt("age", 40);
List<User> userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//名字中包含雨并且年龄小于40,展示不包括创建字段和上级id字段的其他字段
public void selectByWrapperSupper2() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.like("name", "雨").lt("age", 40)
.select(User.class, info -> !info.getColumn().equals("create_time")
&&!info.getColumn().equals("manager_id"));
List<User> userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//测试condition
@Test
public void testCondition() {
String name = "王";
String email = "";
condition1(name,email);
}
//名字中包含王,当email为空时不查询
public void condition1(String name,String email) {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
//传统方法
//if (StringUtils.isNotColumnName(name)) {
// queryWrapper.like("name", name);
//}
//if (StringUtils.isNotColumnName(email)) {
// queryWrapper.like("email", email);
//}
queryWrapper.like(StringUtils.isNotEmpty(name), "name",name)
.like(StringUtils.isNotEmpty(email), "email",email);
List<User> userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//查找姓名为刘红雨且年龄32岁的数据
public void selectByWrapperEntity() {
User whereUser = new User();
whereUser.setName("刘红雨");
whereUser.setAge(32);
QueryWrapper<User> queryWrapper = new QueryWrapper<User>(whereUser);
List<User> userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//allEq
public void selectByWrapperAllEq() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
Map<String, Object> params = new HashMap<String, Object>();
//姓名为王天风,年龄25
//params.put("name", "王天风");
//params.put("age", 25);
//queryWrapper.allEq(params);
//姓名为王天风,年龄为空则忽略
params.put("name", "王天风");
params.put("age", null);
//queryWrapper.allEq(params,false);
//剔除name查询条件
queryWrapper.allEq((k,v)->!k.equals("name"), params);
List<User> userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//Map格式,查询姓名包含雨,年龄小于40的数据
public void selectByWrapperMaps() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.like("name", "雨").lt("age", 40);
List<Map<String, Object>> userList = userMapper.selectMaps(queryWrapper);
userList.forEach(System.out::println);
}
//Map格式,按照直属上级id分组,查询每组的平均年龄,最大年龄,最小年龄,只去年龄总和小于500的组
public void selectByWrapperMaps2() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.select("avg(age) avg_age","min(age) min_age","max(age) max_age")
.groupBy("manager_id").having("sum(age)<{0}",500);
List<Map<String, Object>> userList = userMapper.selectMaps(queryWrapper);
userList.forEach(System.out::println);
}
//obj格式,查询姓名包含雨,年龄小于40的数据
public void selectByWrapperobjs() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.select("id","name").like("name", "雨").lt("age", 40);
List<Object> userList = userMapper.selectObjs(queryWrapper);
userList.forEach(System.out::println);
}
//查询姓名包含雨,年龄小于40的数据条数的总条数
public void selectByWrapperCount() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.like("name", "雨").lt("age", 40);
Integer count = userMapper.selectCount(queryWrapper);
System.out.println("总记录数"+ count);
}
//查询姓名包含雨,年龄小于40的数据条数的一条数据
public void selectByWrapperOne() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.like("name", "雨").lt("age", 40);
User user = userMapper.selectOne(queryWrapper);
System.out.println(user);
}
//lambda,三种起手
public void selectLambda() {
//LambdaQueryWrapper<User> lambda = new QueryWrapper<User>().lambda();
//LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<User>();
LambdaQueryWrapper<User> lambdaQuery = Wrappers.<User>lambdaQuery();
lambdaQuery.like(User::getName, "雨").lt(User::getAge, 40);
List<User> userList = userMapper.selectList(lambdaQuery);
userList.forEach(System.out::println);
}
//lambda,名字为王姓,并且(年龄小于40或邮箱不为空)
public void selectLambda2() {
LambdaQueryWrapper<User> lambdaQuery = Wrappers.<User>lambdaQuery();
lambdaQuery.likeRight(User::getName, "王")
.and(lqw ->lqw.lt(User::getAge, 40).or().isNotNull(User::getEmail));
List<User> userList = userMapper.selectList(lambdaQuery);
userList.forEach(System.out::println);
}
//lambda,名字为王姓,并且(年龄小于40或邮箱不为空),LambdaQueryChainWrapper一气呵成
public void selectLambda3() {
List<User> userList = new LambdaQueryChainWrapper<User>(userMapper).like(User::getName, "雨").ge(User::getAge, 20).list();
userList.forEach(System.out::println);
}
//分页
public void selectPage() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.ge("age", 26);
Page<User> page = new Page<User>(1,10);//1为第一页,10为一页10行
IPage<User> iPage = userMapper.selectPage(page, queryWrapper);
System.out.println("总页数"+iPage.getPages());
System.out.println("总记录数"+iPage.getTotal());
List<User> userList = iPage.getRecords();
userList.forEach(System.out::println);
}
//传统根据id更新,根据id更新数据
public void updateById() {
User user = new User();
user.setId(123L);
user.setAge(25);
user.setEmail("wtf2@baomidou.com");
int rows = userMapper.updateById(user);
System.out.println("影响记录数"+ rows);
}
//传统根据Wrapper更新,姓名为李艺伟且年龄为28的数据,更改其id,年龄和邮箱
public void updateByWrapper2() {
UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>();
updateWrapper.eq("name", "李艺伟").eq("age", 28);
User user = new User();
user.setId(123L);
user.setAge(25);
user.setEmail("wtf2@baomidou.com");
int rows = userMapper.update(user,updateWrapper);
System.out.println("影响记录数"+ rows);
}
//Wrapper更新,姓名为李艺伟且年龄为29的数据,更改年龄为30
public void updateByWrapper3() {
UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>();
updateWrapper.eq("name", "李艺伟").eq("age", 29).set("age", 30);
int rows = userMapper.update(null,updateWrapper);
System.out.println("影响记录数"+ rows);
}
//lambda的Wrapper更新,姓名为李艺伟且年龄为29的数据,更改年龄为30
public void updateByWrapperLambda() {
LambdaUpdateWrapper<User> lambdaUpdate = Wrappers.<User>lambdaUpdate();
lambdaUpdate.eq(User::getName, "李艺伟").eq(User::getAge, 30).set(User::getAge, 31);
int rows = userMapper.update(null,lambdaUpdate);
System.out.println("影响记录数"+ rows);
}
//lambda的Wrapper更新,姓名为李艺伟且年龄为29的数据,更改年龄为30,一气呵成
public void updateByWrapperLambdaChain() {
boolean update = new LambdaUpdateChainWrapper<User>(userMapper)
.eq(User::getName, "李艺伟").eq(User::getAge, 29).set(User::getAge, 31).update();
System.out.println(update);
}
//根据id删除数据
public void deleteById() {
int rows = userMapper.deleteById(123L);
System.out.println("删除条数"+rows);
}
//根据map中名称为向后,年龄25删除数据
public void deleteByMap() {
Map<String, Object> columnMap = new HashMap<>();
columnMap.put("name", "向后");
columnMap.put("age",25);
int rows = userMapper.deleteByMap(columnMap);
System.out.println("删除条数"+rows);
}
//根据id删除多条数据
public void deleteByBatchIds() {
int rows = userMapper.deleteBatchIds(Arrays.asList(123L,234L,345L));
System.out.println("删除条数"+rows);
}
//根据wrapper删除数据
public void deleteByWrapper() {
LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.<User>lambdaQuery();
lambdaQueryWrapper.eq(User::getAge, 27).or().gt(User::getAge, 41);
int rows = userMapper.delete(lambdaQueryWrapper);
System.out.println("删除条数"+rows);
}
//AR模式添加
public void insert2() {
User user = new User();
user.setName("刘华");
user.setAge(29);
user.setEmail("lh@baomidou.com");
user.setManagerId(123L);
user.setCreateTime(LocalDateTime.now());
boolean insert = user.insert();//类继承Model
System.out.println(insert);
}
//AR模式查找
public void selectById2() {
User user = new User();
user.setId(123L);
User selectById = user.selectById();
System.out.println(selectById);
}
//AR模式更新
public void updateById2() {
User user = new User();
user.setId(123L);
user.setName("刘华");
user.setAge(29);
user.setEmail("lh@baomidou.com");
user.setManagerId(123L);
user.setCreateTime(LocalDateTime.now());
boolean updateById = user.updateById();//类继承Model
System.out.println(updateById);
}
//AR模式添加或更新
public void insertOrUpdate() {
User user = new User();
user.setName("刘华");
user.setAge(29);
user.setEmail("lh@baomidou.com");
user.setManagerId(123L);
user.setCreateTime(LocalDateTime.now());
boolean insertOrUpdate = user.insertOrUpdate();//类继承Model
System.out.println(insertOrUpdate);
}
@Autowired
private UserService userService;
//使用service,获取一条数据
public void getOne() {
User one = userService.getOne(Wrappers.<User>lambdaQuery().gt(User::getAge, 25));//多条报错
System.out.println(one);
}
//使用service,获取一条数据
public void batch() {
User user1 = new User();
user1.setName("徐丽1");
user1.setAge(28);
User user2 = new User();
user2.setId(123L);
user2.setName("徐丽2");
user2.setAge(29);
List<User> users = Arrays.asList(user1,user2);
boolean saveBatch = userService.saveBatch(users);
//保存或更新
//userService.saveOrUpdateBatch(users);
System.out.println(saveBatch);
}
//使用service,lambda查询年龄大于25且名字包含雨的数据,一气呵成
public void chain1() {
List<User> users = userService.lambdaQuery().gt(User::getAge, 25).like(User::getName, "雨").list();
users.forEach(System.out::println);
}
//使用service,lambda更新年龄为25的数据为26,一气呵成
public void chain2() {
boolean update = userService.lambdaUpdate().eq(User::getAge, 25).set(User::getAge, 26).update();
System.out.println(update);
}
//使用service,lambda删除年龄为25的数据,一气呵成
public void chain3() {
boolean remove = userService.lambdaUpdate().gt(User::getAge, 25).remove();
System.out.println(remove);
}
}
查看全部 -
如果实体属性和数据库字段对不上:MP提供有三种方式排除非表字段使用方法:
1.在实体类使用transient标注成员变量,数据映射时就不会参与序列化
2.要序列化: 设置该属性位静态变量static
3.在该属性上面添加@TableFieId(exist=false):exist代表数据库是否有该字段,false代表没有
查看全部 -
insert():插入数据
查看全部 -
Spring mybatis配置查看全部
-
普通查询方法:
1、List<T> selectBatch(List<T> ids)
2、List<T> selectByMap(Map<key,value> map)
PS:map中的key为数据库中的列名(如果输入的是实体类中的属性名会报错)、value是列对应的值
查看全部 -
MP 实体类数据库字段对应规则
1、@TableId作用
MP默认找实体类属性名为id的字段作为主键,如果没找到会报错,此时在实体类对应主键字段上加@TableId注解就可以了
2、其他字段对应规则
MP默认实体类属性名(除主键外)与数据库字段名称对应规则为 :
realName -> real_name
3、@TableField("数据库列名")
private String name;
实体类属性名与数据库列名不一致时使用此注解
4、@TableField(exist=false)
private String remark;
此注解表明实体类中的某个属性不是数据库表中字段
查看全部 -
MyBatis-Plus特性:
无侵入,损耗小、强大的CRUD操作
支持Lambda形式调用、支持多种数据库
支持主键自动生成、支持ActiveRecord模式
查看全部 -
如果判定isNotEmpty为true,则进行该字段的对比查询
如果返回false,说明这个字段为空,不进行按此字段查询
查看全部 -
select(字段1,字段2) 选择字段返回,select可以写在queryWrapper后面:
查看全部 -
last(语句)
last只能在mp中使用一次,多次调用将以最后一次为准
查看全部 -
年龄在(30,31,34,35)的
使用in
查看全部 -
(年龄小于40或邮箱不为空)并且名字为王姓:
nested(wq->wq...)
nested为嵌套的意思
说明age合is not null字段为整体
查看全部 -
默认实体中不为null的字段会出现在set中
这里是更新name equals "李艺伟" 并且 age equals 28 的user数据
更新的数据为set的 email 和 set的 age
通用mapper的操作中update(操作对象,wrapper条件)
查看全部
举报