博客
关于我
Builder模式简单应用与自定义Select的Mapper模板
阅读量:226 次
发布时间:2019-02-28

本文共 3669 字,大约阅读时间需要 12 分钟。

公司使用的 freemarker自定义代码生成模板,Mapper中在使用 SELECT * ,这无疑对性能造成了极大的影响,造成了很多不必要的IO读写。因此在想着改进一下代码,灵活的在使用中指定哪些字段需要查询,提升性能。

Builder模式

建造者模式(Builder Pattern)也叫做生成器模式,其定义如下:Separate the construction of a complex object from its representation so that the same construction process can create different representations.(将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。)

Builder 模式适用于需要多个构造方法,且参数不同的情况。

案例

Table

字段名称 字段类型
id Long
content String
siteName String

原代码

查询条件Dto
@Setter@Getterpublic class DemoQueryDto {    private Long id;    private List
ids; private List
idNotIn; private Long idLike; private Long idNotEquals; private Long idIsNull; private String content; private List
contents; private List
contentsNotIn; private String contentLike; private String contentNotEquals; private String contentIsNull; private String siteName; private List
siteNames; private List
siteNamesNotIn; private String siteNameLike; private String siteNameNotEquals; private String siteNameIsNull;}
Dao
/**     * 根据查询条件获取对应信息     * @param condition     * @return list     */    List
selectList(@Param("condition") DemoQueryDto condition);
Mapper
id, content, site_name
..........省略select
from A where 1=1
..........省略
and id = #{condition.id,jdbcType=BIGINT}
and id in
#{item}
and id not in
#{item}
and id like concat('%',#{condition.idLike,jdbcType=BIGINT},'%')
and id != #{condition.idNotEquals,jdbcType=BIGINT}
and id is null
..........类似省略
Service
......参数等省略DemoQueryDto  queryDto = new DemoQueryDto();queryDto.setIds(ids);List
demos = demoMapper.selectList(queryDto);

按照上述方法查询的时候,执行的Sql就是Select全部字段的Sql

Builder方法改造

@Getter@Setterpublic class DemoBuilderDto {    private DemoQueryDto  demoQueryDto ;    //需要判断是否查询的字段    private boolean iscontent;    private boolean issiteName;    public static class Builder{   		 private DemoQueryDto  demoQueryDto ;        //需要判断是否查询的字段 默认false        private boolean iscontent = false;        private boolean issiteName = false;        public Builder(){        }        public Builder DemoQueryDto  (DemoQueryDto  conditionQueryDto){            demoQueryDto = conditionQueryDto;            return this;        }        public Builder iscontent(boolean flag){            iscontent = flag;            return this;        }        public Builder issiteName(boolean flag){            issiteName = flag;            return this;        }        public DemoBuilderDto build(){            return new DemoBuilderDto (this);        }    }    private DemoBuilderDto (Builder builder){        demoQueryDto = builder.demoQueryDto ;        iscontent = builder.iscontent;        issiteName = builder.issiteName;    }}
Mapper
id
,content
,site_name
..........省略select
from A where 1=1
..........省略
and id = #{condition.demoQueryDto.id,jdbcType=BIGINT}
..........类似省略
Dao
List
selectList(@Param("condition") DemoBuilderDto condition);
Service
DemoQueryDto  queryDto = new DemoQueryDto();queryDto.setIds(ids);//现在我只需要查询contentDemoBuilderDto buildDto = new DemoBuilderDto.Builder().demoQueryDto(queryDto).iscontent(true).build();List
demos = demoMapper.selectList(buildDto );

转载地址:http://iqii.baihongyu.com/

你可能感兴趣的文章
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库中 count(*),count(1),count(列名)区别和效率问题
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>
MySQL 数据库的高可用性分析
查看>>
MySQL 数据库设计总结
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>