Code Monkey home page Code Monkey logo

mybatis-advance-query's Introduction

用MyBatis来实现高级查询+分页:

需求:按照员工的关键字(abc),工资范围,所属部门来查询

一些小的细节:(具体情况请看代码)

  • 在PageResult类中应有一个安全判断

    //这是做一个安全判断
    //如果当前页已经大于总页数了,就应该把总页数作为当前页,否则,就把当前页自己做自己.
    //比如:因为有时候是10页并且第10页只有一条数据,删掉了一条数据,就应当到第9页上去
    currentPage = currentPage > totalPage ? totalPage : currentPage;
    
  • 为了减少代码的冗余度,将下列三个方法提取到基类.(判断字符串是否为空串,该方法平时学习用的较多)

    @Getter
    @Setter
    public class QueryObject {
    	private int currentPage = 1;
    	private int pageSize = 3;
    
    	//分页查询: LIMIT  start,pageSize
    	public int getStart() {
    		return (currentPage - 1) * pageSize;
    	}
    
    	//如果字符串为空串,也应该设置为null
    	public String empty2null(String str) {
    		return hasLength(str) ? str : null;
    	}
    
    	public boolean hasLength(String str) {
    		return str != null && !"".equals(str.trim());
    	}
    
    }
    
  • 在xml文件中,小于号应表示为 &lt ;表示,大于号原样表示

    <if test="minSalary!=null">
    	AND salary >= #{minSalary}
    </if>
    <if test="maxSalary!=null">
    	AND salary &lt;= #{maxSalary}
    </if>
    

mybatis-advance-query's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.