Teach you how to use Mybatis generator generate exmaple and simple to use dynamic where clause! !

We must first know what is the generator, Mybatis generator is an exmaple class generation tools, including generating entity, the entity example (ps: where to operate statement queries, paging class, very easy to use, greatly saves to write complicated sql query time statements), mapper.xml file (sql statement stored position), mapper interface.

1, how to generate example file

Here is the example class generation tool, you can click a link to go download
link: https://pan.baidu.com/s/1ND2mUYVJ9kx5CIn2tPWkuw extraction code: mevp copy the contents of this open Baidu network disk phone App, the operation more convenient oh

1, after the download is successful, opening the file, as shown in FIG.

Here Insert Picture Description

2, using the open notebook or notepad ++ generator.xml, Parameter settings and save

Here Insert Picture Description

3. Click Generate statement, copy

java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite

4, in mybatis window click shift + right mouse click to open Powershell window here, and paste the generated statement execution is complete!

Here Insert Picture Description

5. Finally, you can find files example class in the generated file path just set in, copy the file to the project on the ok!

Here Insert Picture Description
Here Insert Picture Description
These are the use mybatis generator generate class example, it is not very simple!

Second, using the example implement paging, conditional query, sort the query, fuzzy query

1, open MmQuestionExample entity, in which the insertion tab get, set method, to paste in the

private int startRow;
private int pageSize;
public int getStartRow() {return startRow;}
public void setStartRow(int startRow) {this.startRow = startRow;}
public int getPageSize() {return pageSize;}
public void setPageSize(int pageSize) {this.pageSize = pageSize;}

2, MmQuestion entity class, here also need to get, set method here is not to write

public class MmQuestion {
    private Integer id;

    private Integer ctype;

    private String question;

    private String answer;

    private String ctime;

    private Integer del;

    private Integer creater;

    private Integer stu;

    private Integer site;

    public Integer getSite() {
        return site;
    }

    public void setSite(Integer site) {
        this.site = site;
    }

    public Integer getStu() { if(IntegerUtil.isNaturalNumber(stu)){ return EntityConstant.ONE; }return stu; }

    public void setStu(Integer stu) {
        this.stu = stu;
    }

    public Integer getCreater() {
        return creater;
    }

    public void setCreater(Integer creater) {
        this.creater = creater;
    }

    public String getCtime() {
        if(StringUtils.isBlank(ctime)){return this.ctime=TimeService.currentTimeMilliStr(); }return ctime;
    }

    public void setCtime(String ctime) {
        this.ctime = ctime;
    }

    public Integer getDel() {
        if(IntegerUtil.isNaturalNumber(del)){return EntityConstant.DEL_NORMAL;} return del; }

    public void setDel(Integer del) {
        this.del = del;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getCtype() {
        return ctype;
    }

    public void setCtype(Integer ctype) {
        this.ctype = ctype;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question == null ? null : question.trim();
    }

    public String getAnswer() {
        if(StringUtils.isBlank(answer)){
            return EntityConstant.STRING_DEFUALT;
        }
        return answer;
    }
    public void setAnswer(String answer) {
        this.answer = answer == null ? null : answer.trim();
    }
  }

3, the main way controller are on the inside, note

    @RequestMapping("/admin/api/questionList")
    public List<MmQuestion> getQuestionList(Integer page, Integer limit, Integer ctype, Integer site,String title) {
        //查询用例
        MmQuestionExample example = new MmQuestionExample();
        //添加分页
        if(IntegerUtil.naturalNumber(page) && IntegerUtil.naturalNumber(limit)){
            example.setPageSize(limit);
            example.setStartRow((page-1)*limit);
        }
        //添加查询条件
        MmQuestionExample.Criteria criteria = example.createCriteria();
        if(IntegerUtil.naturalNumber(ctype)){
        //查询ctype等于1的问题 除了等于还有不等于、小于、大于的方法都可以去使用
            criteria.andCtypeEqualTo(ctype);
        }
        //降序
   	    //example.setOrderByClause("ctype DESC");
      //升序
       //example.setOrderByClause("ctype ASC");
        criteria.andDelEqualTo(EntityConstant.DEL_NORMAL);
        
        //添加模糊查询
          if(!StringUtils.isNotBlank(title){
           criteria.andTitleLIke(‘%’+title+’%’);
      }
        if(IntegerUtil.naturalNumber(site))
        {
            //站点类型
            criteria.andSiteEqualTo(site);
        }
        //返回查询结果
        return mapper.selectByExample(example);
    }

Simply add the query must first create an entity example objects, then createCriteria will be able to operate up! !
The above is how to use the operating Mybatis generator generates exmaple and simple to use dynamic where clause, especially easy to use, saves a lot of time we go and try it! !

Guess you like

Origin blog.csdn.net/qq_43419029/article/details/89187752