Mybatis 中Xml文件标签
xml文件返回值类型有result和resultMap
nameSpace :namespace 中的包名要与接口名一致
id:就是对应的namespace的方法名
resultType:sql语句执行返回值的类型
parameter:参数类型
1.resultType :指定输出结果的类型 ,注意的是sql查询的列名必须和resultType指定pojo属性名相同,否则使用resultMap
1.使用自定义的JavaBean,必须保证结果列的key和model属性一致
2.使用Mybatis 内置容器,比如返回一个Map,int ,String
2.resultMap:将sql查询结果映射为java对象,如果sql查询的列名和最终要映射的文件不一致,需要将resultMap 列名和pojo属性名做一个对应关系
Mybatis中设置useGeneratekeys
1.settings元素在
2.xml映射器
3.接口映射器中
Mybatis xml文件
choose 标签,when 标签 ,otherwise标签 组合起来就相当于java中的switch语句
当满足于一个when标签中的条件时,将不在执行其他的when 标签,和otherwise标签
当所以的when标签中的条件均未满足时,执行otherwise 标签
Mybatis中 标签
使用where,官方解释:where元素,只会在于元素返回任何内容的情况下插入“where”子句。
而且,若子句的开头为“and”或“or”,where 元素 也会将他们去除
Mybatis 中 标签
用于动态更新语句的类似解决方案叫set
set元素可以用于动态包含需要的列,忽略其他不更新的列
Mybatis Update语句
语法:update 表名 set 字段 =值 where 条件 not | or | and
数据的更新就一种条件:update
其标准格式:update 表名 set 字段=值 where 条件,不过根据数据源的不同,还是有所区别的
//1.从外部输入
//2.一些内部变量,函数,比如时间,直接赋值给字段
select tb set LastDate=date()where userId ="aasdd"
//3.对某些字段变量+1,比如:点击率,下载次数
update tb set clickcount=clickcount+1 where id=xxx
//4.将同一记录的字段赋值给另一个字段
update tb set lastDate=regdate where xxx
//5.子条件,将同一个表中某一个记录更新到另一个表中
·update a (select *from a where month=2)as b set a.price=b.price where a.E_ID=b.E_ID and a.month=1 //先查出需要更新的数据
·update a,a as b set a.price=b.price where a.E_Id=b.E_Id and a.month=1 and b.month=2
Page<className> page =new Page<className>();
page.setRecords(list); //将集合放在里面,相当于装载容器
page.setCurrend(1); //当前页
page.setSize(); //每页显示多少条数据
page.setTotal(); //数据总数
Mybatis-Plus 条件构造器
QueryWrapper <类名> qw=new QueryWrapper<>();
qw.in(colimn,value); //sql中的in
qw.eq(column,params); //sql中的where
qw.groupBy(columns); //sql中的分组
qw.ge(column,params); //大于等于
qw.le(column,params); //小于等于
qw.like(column,value); //模糊查询
qw.having(Sqlhaving,params);//条件过滤
qw.orderBy(columns,params); //排序
qw.exists(valus); //sql中的exists查询
qw.notExists(value); //sql中的notexists
Mybatis-Plus 中删除方法 deleteByid,deleteBatchIds,deleteByMap
//通过删除操作,根据Id删除
deleteById
//通过删除操作,map要写列名条件,不能是实体属性名
Map<String,Object> colum =new HashMap<>();
colum.put("name","更新测试");
mapper.deleteByMap("colum");
//通过查询操作,通过多个id删除
List<Integer> idList=new ArrayList<>();
idList.add(5);
idList.add(6);
mapper.deleteBatchIds(idList);
Mybatis-Plus 中新增方法 save,saveBatch,updateById,saveOrUpdate,saveOrUpdateBatch
//使用内置方法save(插入),单条数据
mapper.save(id);
//批量插入数据save,多条数据
mapper.saveBatch(id);
//使用内置方法updateById(更新)
mapper.updateById(id);
//使用内置方法saveOrUpdate(插入或更新)
mapper.saveOrUpdate(id);
//批量插入或更新数据
mapper.saveOrUpdateBatch(id);
Mybatis-plus分页查询
IPage 和Page 的区别
page:用于定义每页的规格
IPage:以规格和其他内容为参数将记录进行分页
IPage<泛型> ipage=mapper.queryList(page,id name).get Records();
在Ipage 里面可以使用很多自己定义的属性,其对象调用getRecords()得到分页数据
getRecords() 将查询来的数放在一个容器中
Mybatis-Plus 中查询方法 selectById,selectList,selectBatchIds,selectPage
//根据主键id去查询单个结果
userMapper.selectById(1);
//查询多条数据库中的记录
userMapper.selectList(null);
//查询多条数据库中的记录—条件查询 selectList(wrapper)
userMapper.selectList(wrapper);
//根据主键的id集合进行多条数据的查询 selectBatchIds
userMapper.selectBatchIds(list1);
//分页查询 selectPage
userMapper.selectPage(page, null);
Mybatis-Plus 中修改方法 updateById,selectList,selectBatchIds,selectPage
//根据ID更新
userMapper.updateById(user);
//条件构造器作为参数进行更新
userMapper.update(user, updateWrapper);
//条件构造器Set方法
//假设只更新一个字段在使用updateWrapper 的构造器中也需要构造一个实体对象,这样比较麻烦。可以使用updateWrapper的set方法
userMapper.update(null, updateWrapper);
//lambda构造器
userMapper.update(null, lambdaUpdateWrapper);
Mybatis Plus allEq 详解
param(代表代入的参数列表)
boolean null= is null,这个参数是说当参数中有值为null的参数,会不会对该参数进行查询。如果值有true 就进行查询,如果为false 则不查询
Mybatis Plus ListObjs
objs:该方法用于取出通过Mybatis-plus 查询到的数据,并放到list中,其中取出的数据并不包括对象所有的字段,最多只能返回一个字段