MybatisPlus常用方法 分页page,范围查询between,模糊查询like,条件查询eq,ne,gt,排序 orderBy以及MybatisPlus生成代码等

时间:2025-02-16 07:54:00
  • //查询所有
 List<PmsProduct> list1 = (new QueryWrapper<PmsProduct>());
  • //根据条件查询 eq
 List<PmsProduct> list2 = (new QueryWrapper<PmsProduct>().eq("id",2));
  • //关键字查询 like // 左 likelift %t ,右 likeRight t% //notLike不包含
 List<PmsProduct> list3 = (new QueryWrapper<PmsProduct>().eq("id",2).like("name","小王"));
  • //排序 orderBy
 List<PmsProduct> list4 = (new QueryWrapper<PmsProduct>().eq("id",2).like("name","小王").orderByDesc("age"));
  • //不等于 ne 大于gt 大于等于ge 小于lt
 List<PmsProduct> list5 = (new QueryWrapper<PmsProduct>().eq("id",2).like("name","小王").orderByDesc("age").ne("name","www"));
  • 查询不为空的某字段 isNotNull
List<PmsProduct> list1 = (Wrappers.<PmsProduct>lambdaQuery().isNotNull(PmsProduct::getBrandName));
  • //分页page
 Page<PmsProduct> pmsProductPage = (new Page<PmsProduct>(1, 10), new QueryWrapper<>(new PmsProduct()));
  • //筛选对像的某一个具体属性 select
Integer countyId = (new QueryWrapper<SysArea>().select("id").eq("name", ()), o -> (Integer) o);
  • //在某个范围内 between
// 分页查询 10 条姓名为‘张三’、性别为男,且年龄在18至50之间的用户记录
List<User> userList = (
        new Page<User>(1, 10),
        new EntityWrapper<User>().eq("name", "张三")
                .eq("sex", 0)
                .between("age", "18", "50")
);
  • 根据某个属性批量查询
(Wrappers.<PmsProduct>lambdaQuery().in(PmsProduct::getId, productIds).select(PmsProduct::getOutProductId), o -> (List<Long>) o);

代码自动生成

// 代码自动生成器
public class generateCode {
   public static void main(String[] args) {
    // 需要构建一个 代码自动生成器 对象
    AutoGenerator mpg = new AutoGenerator();
    // 配置策略
    // 1、全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = ("");
    (projectPath+"/src/main/java");
    ("kwhua");//作者名称
    (false);
    (false); // 是否覆盖
    (IdType.ID_WORKER);
    (DateType.ONLY_DATE);
    gc.setSwagger2(true);//实体属性 Swagger2 注解

    // 自定义文件命名,注意 %s 会自动填充表实体属性!
    ("%sService"); 
    ("%sController");
    ("%sService");
    ("%sServiceImpl");
    ("%sMapper");
    ("%sMapper");

    (gc);

    //2、设置数据源
    DataSourceConfig dsc = new DataSourceConfig();
    ("jdbc:mysql://localhost:3306/kwhua_test?
useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
    ("");
    // (""); //mysql5.6以下的驱动
    ("root");
    ("root");
    ();
    (dsc);
    //3、包的配置
    PackageConfig pc = new PackageConfig();
    (""); //包名
    ("model"); //模块名
    ("entity");
    ("mapper");
    ("service");
    ("controller");
    (pc);

    //4、策略配置
    StrategyConfig strategy = new StrategyConfig();
    ("user","course"); // 设置要映射的表名
    (NamingStrategy.underline_to_camel);
    (NamingStrategy.underline_to_camel);
    (true); // 自动lombok;
    ("deleted");
    // 自动填充配置
    TableFill gmtCreate = new TableFill("gmt_create", );
    TableFill gmtModified = new TableFill("gmt_modified",FieldFill.INSERT_UPDATE);
    ArrayList<TableFill> tableFills = new ArrayList<>();
    (gmtCreate);
    (gmtModified);
    (tableFills);
    // 乐观锁
    ("version");
    //根据你的表名来建对应的类名,如果你的表名没有下划线,比如test,那么你就可以取消这一步
    ("t_");
    (true); //rest请求
    //自动转下划线,比如localhost:8080/hello_id_2
    (true); 
    (strategy);
    (); //执行
 }
}

更多用法前往MybatisPlus官方文档