1,Mapper接口
/**
* 获取自定义SQL 简化自定义XML复杂情况
* 使用方法
* `自定义sql` + ${ew.customSqlSegment}
* 1.逻辑删除需要自己拼接条件 (之前自定义也同样)
* 2.不支持wrapper中附带实体的情况 (wrapper自带实体会更麻烦)
* 3.用法 ${ew.customSqlSegment} (不需要where标签包裹,切记!)
* 4.ew是wrapper定义别名,可自行替换
*/
public interface UserMapper extends BaseMapper {
// 自定义SQL:按输入条件查询数据库中数据
@Select("select * from user ${ew.customSqlSegment}")
List selectUsers(@Param(Constants.WRAPPER) Wrapper queryWrapper);
// 自定义SQL:查询数据库中所有的数据
@Select("select * from user")
List selectAllUsers();
}
2,测试方法
@Test
public void my_selectAllUsers() {
// List list = userMapper.selectAllUsers();
List list = userMapper.selectUsers(new QueryWrapper().eq("name", "zsf"));
System.out.println(list);
}
1,配置文件
#配置mapper.xml文件的位置
mybatis-plus:
mapper-locations:
- com/example/mapper/*
2,Mapper.xml
3,Mapper接口继承BaseMapper
public interface UserMapper extends BaseMapper {
List selectUsers(@Param(Constants.WRAPPER) Wrapper queryWrapper);
}
4,测试方法
@Test
public void my_selectAllUsers() {
// List list = userMapper.selectAllUsers();
List list = userMapper.selectUsers(new QueryWrapper().eq("name", "zsf"));
System.out.println(list);
}
5,自定义update
@Update("update student set ${ew.sqlSet} ${ew.customSqlSegment}")
void upateStu(@Param(Constants.WRAPPER)UpdateWrapper ew);
@Test
public void upateStu() {
studentMapper.upateStu(new UpdateWrapper().set("sname", "xxxxx").eq("id", 1));
}
普通add
insert into student(snum, sname, sage, ssex, sclass) values(#{snum},#{sname},#{sage},#{ssex},#{sclass})
delete
delete from student where id=#{id}
.