当前位置: 首页 > news >正文

金湖县建设局网站清远新闻最新

金湖县建设局网站,清远新闻最新,传媒公司主要做什么业务,合肥建设网站制作哪个好注解作用SelectProvider动态查询SQLInsertProvider动态新增SQLUpdateProvider动态更新SQLDeleteProvider动态删除SQL Select 与 SelectProvider 只是在定义注解的方式上有所不同, 一个是静态SQL, 一个是动态SQL 。 SelectProvider 是 MyBatis 中的一个注解,用于指定…
注解作用
@SelectProvider动态查询SQL
@InsertProvider动态新增SQL
@UpdateProvider动态更新SQL
@DeleteProvider动态删除SQL

@Select 与 @SelectProvider 只是在定义注解的方式上有所不同, 一个是静态SQL, 一个是动态SQL 。

@SelectProvider 是 MyBatis 中的一个注解,用于指定一个类或者类的某个方法提供 SQL 查询语句。该注解常用于动态 SQL 的场景,例如根据不同的参数生成不同的查询语句。 使用 @SelectProvider 注解的方式可以让 MyBatis 在运行时根据注解指定的类或方法来生成对应的 SQL 查询语句,从而实现动态 SQL 功能。注解的语法如下:

@SelectProvider(type = XxxProvider.class, method = "xxxMethod")

其中,type 属性表示提供 SQL 查询语句的类,method 属性表示类中提供查询语句的方法。在查询时,MyBatis 会调用指定类中的指定方法来生成 SQL 查询语句,然后执行该查询语句并返回结果。 需要注意的是,提供 SQL 查询语句的类需要实现 org.apache.ibatis.builder.annotation.ProviderMethodResolver 接口,该接口中定义了查找提供 SQL 查询的方法的逻辑。同时,提供查询语句的方法需要返回一个字符串类型的 SQL 查询语句。 使用 @SelectProvider 注解可以让 MyBatis 的查询语句更加灵活,适用于各种复杂的查询场景。

1 @SelectProvider 使用

1 TestMapper.java

public interface TestMapper {/*** 根据性别获取老师信息** @param sex* @return* @date 2022年11月26日12点09分*/@SelectProvider(type = Test.class, method = "list")List<Teacher> list(Integer sex);}

2 Test.java

public class Test {/*** 根据性别获取老师信息** @param sex* @return* @date 2022年11月26日12点09分*/public String list(Integer sex) {return new SQL() {{SELECT("*");FROM("teacher");if (null != sex) {WHERE(" 1 = 1 sex = " + sex);} else {WHERE(" 1 = 1 ");}ORDER_BY("create_time desc");}}.toString();}}

2 模拟 Mybatis Plus

可以使用 SelectProvider 写一个适用于所有表的查询的方法。

1 BasicWrapper.java

import java.util.LinkedList;
import java.util.List;/*** @author Administrator*/
public class BasicWrapper<T> {public String last;public Class<T> bean;public String table;public String[] field;public List<String> condition = new LinkedList<>();public String orderBy;}

2 QueryWrapper.java


import java.util.Arrays;import org.apache.ibatis.jdbc.SQL;import cn.hutool.core.collection.CollectionUtil;/*** @author */
public class QueryWrapper<T> extends BasicWrapper {public QueryWrapper() {}public QueryWrapper(String table, String... field) {super.bean = null;super.table = table;super.field = (null == field || field.length == 0) ? new String[]{"*"} : field;}public String list(QueryWrapper wrapper) {Query query = wrapper.build();return new SQL() {{SELECT(query.getFields());FROM(query.getTable());WHERE(" 1 = 1 " + query.getCondition());ORDER_BY(query.getOrderBy());}}.toString();}public Query build() {Query query = new Query();query.setTable(super.table);query.setFields((null == super.field || super.field.length == 0) ? "*" : String.join(", ", Arrays.asList(super.field)));String condition = CollectionUtil.isEmpty(super.condition) ? " 1 = 1 " : String.join(" ", super.condition);String last = null == super.last ? "" : super.last;query.setCondition(condition + " " + last);return query;}public QueryWrapper orderBy(String sql) {super.orderBy = sql;return this;}public QueryWrapper orderBy(boolean condition, String sql) {if (condition) {super.orderBy = sql;}return this;}public QueryWrapper apply(String sql) {super.condition.add(" and (" + sql + ") ");return this;}public QueryWrapper apply(boolean condition, String sql) {if (condition) {super.condition.add(" and (" + sql + ") ");}return this;}public QueryWrapper eq(String filed, Object value) {super.condition.add(" and " + filed + " = " + value);return this;}public QueryWrapper eq(boolean condition, String filed, Object value) {if (condition) {if (value instanceof String) {super.condition.add(" and " + filed + " = '" + value + "'");} else {super.condition.add(" and " + filed + " = " + value);}}return this;}public QueryWrapper last(String value) {super.last = " " + value;return this;}public QueryWrapper last(boolean condition, String value) {if (condition) {super.last = " " + value;}return this;}public QueryWrapper like(String filed, Object value) {super.condition.add(" and " + filed + " like %" + value + "%");return this;}public QueryWrapper likeLeft(String filed, Object value) {super.condition.add(" and " + filed + " like %" + value + "%");return this;}public QueryWrapper likeRight(String filed, Object value) {super.condition.add(" and " + filed + " like %" + value + "%");return this;}public QueryWrapper like(boolean condition, String filed, Object value) {if (condition) {super.condition.add(" and " + filed + " like %" + value + "%");}return this;}public QueryWrapper likeLeft(boolean condition, String filed, Object value) {if (condition) {super.condition.add(" and " + filed + " like %" + value + "%");}return this;}public QueryWrapper likeRight(boolean condition, String filed, Object value) {if (condition) {super.condition.add(" and " + filed + " like %" + value + "%");}return this;}}

3 Query.java

import lombok.Data;/*** @author Administrator*/
@Data
public class Query {private String table;private String fields;private String orderBy;private String condition;}

4 Test.java

import org.apache.ibatis.jdbc.SQL;public class Test {public static void main(String agrs[]) {QueryWrapper<Teacher> wrapper = new QueryWrapper<>("teacher");wrapper.eq("sex", 1);wrapper.eq(StringUtils.isNotBlank(name), "name", name);wrapper.orderBy("create_time desc");List<Teacher> list = processMapper.list(wrapper);}}

http://www.khdw.cn/news/41986.html

相关文章:

  • 网页设计图片变换特效安徽网站建设优化推广
  • 济南做网站0531soso百度seo关键词排名价格
  • 帮做软件测试的网站百度搜索引擎优化相关性评价
  • 织梦中查看演示网站怎么做一级造价工程师
  • 免费建网站那个好h5制作
  • 帝国做的网站 news.url获取不到地址啊北京百度快速排名
  • 网站建设尾款收取百度移动端关键词优化
  • wordpress博客主题acgseo和sem
  • 网站搭建工作室加盟个人如何在百度上做广告
  • 国内重大新闻2022搜索引擎关键词优化方案
  • 黄冈市建设委员会网站武汉网络推广外包公司
  • 高清的网站建设制作网站推广
  • 做网站空百度问一问
  • mac apache wordpressseo网站优化推广怎么样
  • 免费网站安全软件大全苹果版网络推广引流有哪些渠道
  • 建设网站的相关费用如何自创网站
  • 郑州专业建网站关键词排名怎么做好
  • 那个网站做的刀比较好企业网站的类型
  • 自己做网站兼职十大最靠谱培训机构
  • 网络公司网站案例营销策划的概念
  • 做雕塑设计的网站手机怎么建立网站
  • 沈阳招标中心招标公告seo公司后付费
  • 网站服务内容网站建设今天中国新闻
  • 微网站是免费的吗海外广告投放公司
  • 以前在线做预算的网站seo优化工作
  • 建站工具交流网站策划方案范文
  • 网站怎样建设才叫人性化站外推广平台有哪些
  • 网站建设分几模块seo软件服务
  • 杭州视频网站建设地推公司
  • shopex网站经常出错广告推广怎么找客户