Java 多条件复合查询

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




前端页面:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://www.idaobin.com/js/jquery-3.2.1.js"></script>
</head>

<body>
    <input type="text" id="one" class="search" value="7">
    <input type="text" id="two" class="search" value="8">
    <input type="text" id="three" class="search" value="9">

    <input type="button" value="btn" id="btn">
</body>
<script>
    //通过class类名取input的所有值
    function searchCondition(className){
        var str = "";
        $("."+className).each(function () {
            var value=$(this).val().trim().replace("'", "");
            value="'"+value+"',";
            str+=value;
        })
        return str; }
    $("#btn").click(function () {
        var str=searchCondition("search");
        console.log(str); //传到后端的值
    });
</script>

</html>

Patient实体类

public class Patient {
	private String id;
	private String panme;
	private String page;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPanme() {
		return panme;
	}
	public void setPanme(String panme) {
		this.panme = panme;
	}
	public String getPage() {
		return page;
	}
	public void setPage(String page) {
		this.page = page;
	}
	
}

后台代码:

	/**
	 * 替换'字符防止错位
	 * @param cond
	 * @return
	 */
	private String replace(String cond) {
		String str="";
		String[] arr = cond.split(",");
		for(int i=0;i<arr.length;i++) {
			str+="'"+arr[i].trim().replace("'", "")+"',";
		}
		return str;
	}
	
	/**
	 * where条件后的复合查询语句拼接
	 * @param cond
	 * @return
	 */
	public String searchWhere(String cond) {
		cond=replace(cond);
		String[] arr = cond.split(",");
		StringBuilder where = new StringBuilder(" 1=1");
		Patient patient=new Patient();
		patient.setId(arr[0]);
		patient.setPanme(arr[1]);
		patient.setPage(arr[2]);
		
		if(!patient.getId().equals("''")) {
			where.append(" and id="+patient.getId());
		}
		if(!patient.getPanme().equals("''")) {
			where.append(" and pname="+patient.getPanme());
		}
		if(!patient.getPage().equals("''")) {
			where.append(" and page="+patient.getPage());
		}
		return where.toString();
	}

输出打印:

1=1 and id='7' and page='9'

原文链接:https://www.idaobin.com/archives/2085.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

81 − = 77