`
huangyongxing310
  • 浏览: 499116 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

mybatis 通用查询实现

 
阅读更多
package com.oceano.modity.entity;

import java.util.ArrayList;
import java.util.List;

public class CriteriaExample {
    protected List<OrderByCase> orderByList;

    protected boolean distinct;

    protected List<Criteria> criteriaList;

    public List<OrderByCase> getOrderByList() {
	return orderByList;
    }

    public void setOrderByList(List<OrderByCase> orderByList) {
	this.orderByList = orderByList;
    }

    public boolean isDistinct() {
	return distinct;
    }

    public void setDistinct(boolean distinct) {
	this.distinct = distinct;
    }

    public List<Criteria> getCriteriaList() {
	return criteriaList;
    }

    public void setCriteriaList(List<Criteria> criteriaList) {
	this.criteriaList = criteriaList;
    }

    @Override
    public String toString() {
	return "CriteriaExample [orderByList=" + orderByList + ", distinct=" + distinct + ", criteriaList="
		+ criteriaList + ", getOrderByList()=" + getOrderByList() + ", isDistinct()=" + isDistinct()
		+ ", getCriteriaList()=" + getCriteriaList() + ", getClass()=" + getClass() + ", hashCode()="
		+ hashCode() + ", toString()=" + super.toString() + "]";
    }

    public CriteriaExample addOrderByCase(String field, String direction) {
	if (this.orderByList == null) {
	    this.orderByList = new ArrayList<>();
	}
	this.orderByList.add(new OrderByCase(field, direction));
	return (CriteriaExample) this;
    }

    public CriteriaExample addCriteria(Criteria criteria) {
	if (this.criteriaList == null) {
	    this.criteriaList = new ArrayList<>();
	}
	this.criteriaList.add(criteria);
	return (CriteriaExample) this;
    }

    public static class OrderByCase {
	public static String ASC = "ASC";
	public static String DESC = "DESC";
	private String condition;
	private String direction; // ASC,DESC

	public String getCondition() {
	    return condition;
	}

	public void setCondition(String condition) {
	    this.condition = condition;
	}

	public String getDirection() {
	    return direction;
	}

	public void setDirection(String direction) {
	    this.direction = direction;
	}

	public OrderByCase(String condition, String direction) {
	    super();
	    this.condition = condition;
	    this.direction = direction;
	}

	@Override
	public String toString() {
	    return "OrderByCase [condition=" + condition + ", direction=" + direction + "]";
	}
    }

    protected abstract static class GeneratedCriteria {
	protected List<Criterion> criteria;

	protected GeneratedCriteria() {
	    super();
	    criteria = new ArrayList<Criterion>();
	}

	public boolean isValid() {
	    return criteria.size() > 0;
	}

	public List<Criterion> getAllCriteria() {
	    return criteria;
	}

	public List<Criterion> getCriteria() {
	    return criteria;
	}

	protected void addCriterion(String condition, String property) {
	    if (condition == null) {
		throw new RuntimeException("condition for " + property + " cannot be null");
	    }

	    criteria.add(new Criterion(condition));
	}

	protected void addCriterion(String condition, Object value, String property) {
	    if (condition == null) {
		throw new RuntimeException("condition for " + property + " cannot be null");
	    }
	    if (value == null) {
		throw new RuntimeException("Value for " + property + " cannot be null");
	    }
	    criteria.add(new Criterion(condition, value));
	}

	protected void addCriterion(String condition, Object value1, Object value2, String property) {
	    if (condition == null) {
		throw new RuntimeException("condition for " + property + " cannot be null");
	    }
	    if (value1 == null || value2 == null) {
		throw new RuntimeException("Between values for " + property + " cannot be null");
	    }
	    criteria.add(new Criterion(condition, value1, value2));
	}

	public Criteria equal(String field, Object value) {
	    addCriterion(field + " =", value, field);
	    return (Criteria) this;
	}

	public Criteria notEqual(String field, Object value) {
	    addCriterion(field + " <> ", value, field);
	    return (Criteria) this;
	}

	public Criteria greaterThan(String field, Object value) {
	    addCriterion(field + " > ", value, field);
	    return (Criteria) this;
	}

	public Criteria lessThan(String field, Object value) {
	    addCriterion(field + " < ", value, field);
	    return (Criteria) this;
	}

	public Criteria lessThanOrEqual(String field, Object value) {
	    addCriterion(field + " <= ", value, field);
	    return (Criteria) this;
	}

	public <T> Criteria in(String field, List<T> valueList) {
	    addCriterion(field + " in ", valueList, field);
	    return (Criteria) this;
	}

	public <T> Criteria notIn(String field, List<T> valueList) {
	    addCriterion(field + " not in ", valueList, field);
	    return (Criteria) this;
	}

	public Criteria between(String field, Object value1, Object value2) {
	    addCriterion(field + " between ", value1, value2, field);
	    return (Criteria) this;
	}

	public Criteria notBetween(String field, Object value1, Object value2) {
	    addCriterion(field + " not between ", value1, value2, field);
	    return (Criteria) this;
	}

	public Criteria isNull(String field) {
	    addCriterion(field + " is null ", field);
	    return (Criteria) this;
	}

	public Criteria isNotNull(String field) {
	    addCriterion(field + " is not null ", field);
	    return (Criteria) this;
	}

	public Criteria like(String field, Object value) {
	    addCriterion(field + " like concat(concat('%',\"" + value + "\",'%'))", field);
	    return (Criteria) this;
	}

	public Criteria notLike(String field, Object value) {
	    addCriterion(field + " not like concat(concat('%',\"" + value + "\",'%'))", field);
	    // addCriterion(field + " not like ", value, field);
	    return (Criteria) this;
	}
    }

    public static class Criteria extends GeneratedCriteria {
	public Criteria() {
	    super();
	}

	@Override
	public String toString() {
	    return "Criteria [criteria=" + criteria + ", isValid()=" + isValid() + ", getAllCriteria()="
		    + getAllCriteria() + ", getCriteria()=" + getCriteria() + ", getClass()=" + getClass()
		    + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
	}

    }

    public static class Criterion {
	private String condition;

	private Object value;

	private Object secondValue;

	private boolean noValue;

	private boolean singleValue;

	private boolean betweenValue;

	private boolean listValue;

	private String typeHandler;

	public String getCondition() {
	    return condition;
	}

	public Object getValue() {
	    return value;
	}

	public Object getSecondValue() {
	    return secondValue;
	}

	public boolean isNoValue() {
	    return noValue;
	}

	public boolean isSingleValue() {
	    return singleValue;
	}

	public boolean isBetweenValue() {
	    return betweenValue;
	}

	public boolean isListValue() {
	    return listValue;
	}

	public String getTypeHandler() {
	    return typeHandler;
	}

	protected Criterion(String condition) {
	    super();
	    this.condition = condition;
	    this.typeHandler = null;
	    this.noValue = true;
	}

	protected Criterion(String condition, Object value, String typeHandler) {
	    super();
	    this.condition = condition;
	    this.value = value;
	    this.typeHandler = typeHandler;
	    if (value instanceof List<?>) {
		this.listValue = true;
	    } else {
		this.singleValue = true;
	    }
	}

	protected Criterion(String condition, Object value) {
	    this(condition, value, null);
	}

	protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
	    super();
	    this.condition = condition;
	    this.value = value;
	    this.secondValue = secondValue;
	    this.typeHandler = typeHandler;
	    this.betweenValue = true;
	}

	protected Criterion(String condition, Object value, Object secondValue) {
	    this(condition, value, secondValue, null);
	}

	@Override
	public String toString() {
	    return "Criterion [condition=" + condition + ", value=" + value + ", secondValue=" + secondValue
		    + ", noValue=" + noValue + ", singleValue=" + singleValue + ", betweenValue=" + betweenValue
		    + ", listValue=" + listValue + ", typeHandler=" + typeHandler + "]";
	}

    }

}





<!-- <trim prefix="(" suffix=")" prefixOverrides="OR"> -->
	<sql id="Example_Where_Clause">
		<if test="criteriaList != null">
		<foreach collection="criteriaList" item="criteria" separator="AND">
				<trim prefix="(" suffix=")">
					<foreach collection="criteria.criteria" item="criterion"
						separator="OR">
						<choose>
							<when test="criterion.noValue">
								${criterion.condition}
							</when>
							<when test="criterion.singleValue">
								${criterion.condition} #{criterion.value}
							</when>
							<when test="criterion.betweenValue">
								${criterion.condition} #{criterion.value} AND
								#{criterion.secondValue}
							</when>
							<when test="criterion.listValue">
								${criterion.condition}
								<foreach collection="criterion.value" item="listItem"
									open="(" close=")" separator=",">
									#{listItem}
								</foreach>
							</when>
						</choose>
					</foreach>
				</trim>
		</foreach>
		</if>
	</sql>
	
	<sql id="orderBy_case">
		ORDER BY
		<if test="orderByList !=null">
			<foreach collection="orderByList" item="orderBy" separator=",">
				${orderBy.condition} ${orderBy.direction}	
			</foreach>
		</if>
	</sql>

	
	
	
	<select id="findByTest" parameterType="com.oceano.modity.entity.CriteriaExample"
		resultMap="modityPriceList">
		select * from t_modity_price
		<where>
			<include refid="Example_Where_Clause" />
		</where>
		<include refid="orderBy_case" />
	</select>



@RequestMapping("/sqlTest")
	@ResponseBody
	public String sqlTest() {
		
		
		CriteriaExample criteriaExample = new CriteriaExample();
		
		criteriaExample.addOrderByCase("colour_id", OrderByCase.ASC);
		criteriaExample.addOrderByCase("colour_code", OrderByCase.DESC);
		
		criteriaExample.setDistinct(true);
		
		
		Criteria criteria1 =new Criteria();
		criteria1.equal("colour_id", "Yellow");

		criteriaExample.addCriteria(criteria1);
	
		
//		Criteria criteria2 =new Criteria();
//		criteria2.notEqual("colour_code","test");
//		
//		criteriaExample.addCriteria(criteria2);
		
		Criteria criteria2 =new Criteria();
		criteria2.between("colour_id", 4, 5);
		
		criteriaExample.addCriteria(criteria2);
//		
		Criteria criteria3 =new Criteria();
		criteria3.like("colour_id", "yellow");
		criteria3.like("colour_code", "黄色");
		
		criteriaExample.addCriteria(criteria3);
		
		Criteria criteria4 =new Criteria();
		List<String> list = new ArrayList<>();
		list.add("123");
		list.add("456");
		
		criteria4.in("colour_id", list);
		criteria4.like("colour_code", "黄色");
		
		criteriaExample.addCriteria(criteria4);
		
//		Criteria criteria4 =new Criteria();
//		List<String> list = new ArrayList<>();
//		list.add("123");
//		list.add("456");
//		
//		criteria3.in("colour_id", list);
//		criteria3.like("colour_code", "黄色");
//		
//		criteriaExample.addCriteria(criteria4);
		
	
		
		List<T_ModityPrice> modityPriceList = modityPriceService.findByTest(criteriaExample);
		
		
		

		return "OK";

	}










分享到:
评论

相关推荐

    浅谈MyBatis通用Mapper实现原理

    浅谈MyBatis通用Mapper实现原理 MyBatis通用Mapper是MyBatis框架中的一种通用Mapper实现方式,主要提供了一些通用的方法,这些方法是以接口的形式提供的。通用Mapper的实现原理主要是通过Java反射机制和MyBatis框架...

    MyBatis 通用 Mapper 实现原理及相关内容 - GitChat

    MyBatis 通用 Mapper 实现原理及相关内容 抓下来打包成了HTML文件, 方便离线观看

    MyBatis动态条件通用查询

    总的来说,MyBatis动态条件通用查询是一种高效且灵活的数据库查询方式,它结合了Map的数据结构和MyBatis的动态SQL特性,使得在Java应用程序中实现复杂、可变的查询变得简单易行。通过合理的设计和实现,我们可以极大...

    Mybatis通用分页插件

    通过以上介绍,我们可以看到Mybatis通用分页插件在Java开发中扮演着重要的角色,它降低了开发难度,提高了代码的可读性和可维护性,同时提升了查询效率。在实际开发中,合理利用此类工具,能够有效地提升项目的开发...

    mybatis拦截器实现通用权限字段添加的方法

    MyBatis拦截器实现通用权限字段添加的方法 MyBatis拦截器是一种非常实用的技术,可以用来实现各种复杂的数据库操作。本文将详细介绍如何使用MyBatis拦截器来实现通用权限字段添加,达到灵活、可靠、可维护的数据库...

    Mybatis通用DAO设计封装(mybatis)

    Mybatis通用DAO设计封装主要涉及的是在Java开发中如何高效地使用Mybatis框架进行数据库操作。Mybatis是一款轻量级的持久层框架,它避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。通过提供自定义SQL、存储...

    06实现mybatis分页插件demo

    06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo...

    Mybatis通用增删改查

    在实际开发中,为了提高代码的复用性和减少重复工作,开发者通常会实现一套通用的增删改查模板,这就是“Mybatis通用增删改查”的概念。 1. **基础 DaoImpl 概念** Dao(Data Access Object)接口是Java中用于...

    maven、spring、spring mvc、mybatis 整合实现ssm通用增删改查基础开发框架

    maven、spring、spring mvc、mybatis 整合实现ssm通用增删改查基础开发框架.maven、spring、spring mvc、mybatis 整合实现ssm通用增删改查基础开发框架.maven、spring、spring mvc、mybatis 整合实现ssm通用增删改查...

    mybatis 通用DAO 简单实现

    通用DAO的概念就是基于MyBatis,创建一个可复用的DAO接口和实现类,来处理基本的CRUD(Create、Read、Update、Delete)操作。 首先,我们来看一下通用DAO的简单实现。通常,我们会定义一个基类,如`BaseMapper&lt;T&gt;`...

    springboot+mybatis通用注解

    总的来说,"springboot+mybatis通用注解"项目利用SpringBoot的自动化配置和MyBatis的注解功能,实现了数据库操作的便捷化和代码的复用。通过继承基类,开发者可以快速创建具备完整CRUD功能的服务,显著提高了开发...

    SpringBoot集成MyBatis-Plus实现国产数据库适配.docx

    SpringBoot 集成 MyBatis-Plus 实现国产数据库适配 MyBatis-Plus 是一款在 MyBatis 的基础上进行扩展的开源工具包,只做增强不做改变,引入 MyBatis-Plus 不会对您现有的 Mybatis 构架产生任何影响。MyBatis-Plus ...

    spring-boot+tk.mybatis通用mapper

    本教程将深入探讨如何在Spring Boot项目中集成tk.mybatis通用Mapper,实现高效的数据访问。 首先,我们需要理解Spring Boot的核心特性:自动配置。Spring Boot通过扫描类路径并根据存在的依赖来自动配置相应的Bean...

    Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x

    在MyBatis中,通过动态SQL实现,我们可以创建一个基础的Dao接口,包含这些通用方法,并在Mapper XML文件中编写对应的SQL语句。 接着,我们引入通用Mapper的概念。通用Mapper是MyBatis的一个扩展,由田梦阳...

    MyBatis通用Mapper实现原理及相关内容

    MyBatis通用Mapper实现原理及相关内容 MyBatis通用Mapper是MyBatis框架中的一种重要组件,它提供了一些通用的方法来简化数据库操作。在这里,我们将深入探讨MyBatis通用Mapper的实现原理及相关内容。 通用Mapper的...

    mybatis通用mapper

    MyBatis通用Mapper是一款在MyBatis框架基础上封装的工具,旨在简化开发过程中对数据库 CRUD(创建、读取、更新、删除)操作的繁琐过程。它通过提供预定义的SQL映射方法,使得开发者可以无需编写大量的XML配置文件,...

    基于SpringBoot+FreeMarker+MyBatis+ExtJs实现的一个通用后台管理系统,界面美观,适合快速迭代开发

    基于SpringBoot+FreeMarker+MyBatis+ExtJs实现的一个通用后台管理系统,界面美观,适合快速迭代开发 项目说明 技术栈: SpringBoot MyBatis Redis MySQL FreeMarker ExtJs 基于SpringBoot+FreeMarker+MyBatis+...

    mybatis 通用mapper

    MyBatis通用Mapper是一个基于MyBatis框架的扩展工具,它极大地简化了针对单一表的操作,使得开发人员无需编写繁琐的XML映射文件和接口定义就能实现数据层的增删改查等基本功能。其优势在于简化了开发流程,降低了...

    MyBatis通用插件自动生成代码器

    MyBatis通用插件自动生成代码器是一种高效实用的开发工具,主要针对MyBatis框架进行设计,旨在简化开发过程中的代码编写工作。通过这款插件,开发者可以快速地生成与数据库表对应的实体类(Entity)、接口(Mapper)...

Global site tag (gtag.js) - Google Analytics