【Mybatis】mybatis使用示例
BusinessAnalysisMapper.java
import com.chinamobile.epic.dao.model.PerformanceMetricAnalysis;
import com.chinamobile.epic.dao.model.PerformanceMetricAnalysisModel;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.Date;
import java.util.List;
@Mapper
public interface BusinessAnalysisMapper {
// 参考: BusinessStatisticMapper
int deleteByPrimaryKey(PerformanceMetricAnalysisModel key);
int insert(PerformanceMetricAnalysisModel record);
/**
* 批量插入数据(根据“表名后缀”插入)
*
* @param record
* @param tableNameSuffix 表名后缀
* @return
*/
int insertBatch(@Param("list") List<PerformanceMetricAnalysisModel> record, @Param("tableNameSuffix") String tableNameSuffix);
int insertSelective(PerformanceMetricAnalysisModel record);
PerformanceMetricAnalysis selectByPrimaryKey(PerformanceMetricAnalysisModel key);
int updateByPrimaryKeySelective(PerformanceMetricAnalysisModel record);
int updateByPrimaryKey(PerformanceMetricAnalysisModel record);
/**
* 删除 startTime 之前的数据
*
* @param startTime
* @param tableNameSuffix
* @return
*/
int deleteBeforeTime(@Param("createAt") Date startTime, @Param("tableNameSuffix") String tableNameSuffix);
/**
* 聚合查询
*
* @param tableName
* @param funcToUse 对value求值时,使用的函数,如:avg,sum
* @param startTime 查询的开始时间
* @param endTime 查询的结束时间
* @return
*/
List<PerformanceMetricAnalysis> groupByDimensionIDAndIndicatorID(@Param("srcTableName") String tableName, @Param("func") String
funcToUse, @Param("startTime") Date startTime, @Param("endTime") Date endTime);
/**
* 聚合查询:指标项为 {@code MetricDataType.Counter} 类型
*
* @param tableName
* @param startTime
* @param endTime
* @param indicatorKeysOfCounter
* @return
*/
List<PerformanceMetricAnalysis> groupCounterDataByDimensionIndicator(@Param("srcTableName") String tableName, @Param("startTime")
Date startTime, @Param("endTime") Date endTime, @Param("list") List<String> indicatorKeysOfCounter);
/**
* 聚合查询:指标项为 {@code MetricDataType.Gauge} 类型
*
* @param tableName
* @param startTime
* @param endTime
* @param indicatorKeysOfGauge
* @return
*/
List<PerformanceMetricAnalysis> groupGaugeDataByDimensionIndicator(@Param("srcTableName") String tableName, @Param("startTime")
Date startTime, @Param("endTime") Date endTime, @Param("list") List<String> indicatorKeysOfGauge);
}
BusinessAnalysisMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.chinamobile.epic.dao.mapper.BusinessAnalysisMapper">
<resultMap id="BaseResultMap" type="com.chinamobile.epic.dao.model.PerformanceMetricAnalysis">
<id column="dimension_id" property="dimensionId" jdbcType="CHAR"/>
<id column="indicator_key" property="indicatorKey" jdbcType="VARCHAR"/>
<id column="create_at" property="createAt" jdbcType="TIMESTAMP"/>
<result column="pool_id" property="poolId" jdbcType="CHAR"/>
<result column="value" property="value" jdbcType="DOUBLE"/>
<result column="resource_type" property="resourceType" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
pool_id, dimension_id, indicator_key, value, resource_type, create_at
</sql>
<sql id="Base_Column_List_sumValue">
pool_id, dimension_id, indicator_key, sum(value) as value, resource_type, create_at
</sql>
<sql id="Base_Column_List_avgValue">
pool_id, dimension_id, indicator_key, avg(value) as value, resource_type, create_at
</sql>
<sql id="Base_Column_List_maxValue">
pool_id, dimension_id, indicator_key, max(value) as value, resource_type, create_at
</sql>
<sql id="Aggregation_Group_By">
dimension_id, indicator_key
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="com.chinamobile.epic.dao.model.PerformanceMetricAnalysisModel">
select
<include refid="Base_Column_List"/>
from performance_metirc_business_${tableNameSuffix}
where dimension_id = #{dimensionId,jdbcType=CHAR}
and indicator_key = #{indicatorKey,jdbcType=VARCHAR}
and create_at = #{createAt,jdbcType=TIMESTAMP}
</select>
<select id="groupByDimensionIDAndIndicatorID" resultMap="BaseResultMap">
select
<if test='func=="avg"'>
<include refid="Base_Column_List_avgValue"/>
</if>
<if test='func=="sum"'>
<include refid="Base_Column_List_sumValue"/>
</if>
from ${srcTableName}
where create_at >= #{startTime,jdbcType=TIMESTAMP}
and create_at <= #{endTime,jdbcType=TIMESTAMP}
GROUP BY
<include refid="Aggregation_Group_By"/>
</select>
<!-- 这种方式可能不准确,应该使用求last的方式: 见下面-->
<!--<select id="groupCounterDataByDimensionIndicator" resultMap="BaseResultMap">-->
<!--select-->
<!--<include refid="Base_Column_List_maxValue"/>-->
<!--from ${srcTableName}-->
<!--where create_at >= #{startTime,jdbcType=TIMESTAMP}-->
<!--and create_at <= #{endTime,jdbcType=TIMESTAMP}-->
<!--<choose>-->
<!--<when test="list != null and list.size()>0">-->
<!--and indicator_key in-->
<!--<foreach collection="list" item="item" index="index" open="(" close=")" separator=",">-->
<!--#{item,jdbcType=CHAR}-->
<!--</foreach>-->
<!--</when>-->
<!--<otherwise>-->
<!--and indicator_key in('Unknown')-->
<!--</otherwise>-->
<!--</choose>-->
<!--GROUP BY-->
<!--<include refid="Aggregation_Group_By"/>-->
<!--</select>-->
<select id="groupCounterDataByDimensionIndicator" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from
(
SELECT * FROM ${srcTableName}
WHERE create_at >= #{startTime,jdbcType=TIMESTAMP}
and create_at <= #{endTime,jdbcType=TIMESTAMP}
<choose>
<when test="list != null and list.size()>0">
and indicator_key in
<foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
#{item,jdbcType=CHAR}
</foreach>
</when>
<otherwise>
and indicator_key in('Unknown')
</otherwise>
</choose>
ORDER BY create_at DESC
) AS t
GROUP BY
<include refid="Aggregation_Group_By"/>
</select>
<select id="groupGaugeDataByDimensionIndicator" resultMap="BaseResultMap">
select
<include refid="Base_Column_List_avgValue"/>
from ${srcTableName}
where create_at >= #{startTime,jdbcType=TIMESTAMP}
and create_at <= #{endTime,jdbcType=TIMESTAMP}
<choose>
<when test="list != null and list.size()>0">
and indicator_key in
<foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
#{item,jdbcType=CHAR}
</foreach>
</when>
<otherwise>
and indicator_key in('Unknown')
</otherwise>
</choose>
GROUP BY
<include refid="Aggregation_Group_By"/>
</select>
<delete id="deleteByPrimaryKey" parameterType="com.chinamobile.epic.dao.model.PerformanceMetricAnalysisModel">
delete from performance_metirc_business_${tableNameSuffix}
where dimension_id = #{dimensionId,jdbcType=CHAR}
and indicator_key = #{indicatorKey,jdbcType=VARCHAR}
and create_at = #{createAt,jdbcType=TIMESTAMP}
</delete>
<delete id="deleteBeforeTime">
delete from performance_metirc_business_${tableNameSuffix}
where create_at <= #{createAt,jdbcType=TIMESTAMP}
</delete>
<insert id="insert" parameterType="com.chinamobile.epic.dao.model.PerformanceMetricAnalysisModel">
insert into performance_metirc_business_${tableNameSuffix} (dimension_id, indicator_key, create_at, pool_id, value, resource_type )
values (#{dimensionId,jdbcType=CHAR}, #{indicatorKey,jdbcType=VARCHAR}, #{createAt,jdbcType=TIMESTAMP},
#{poolId,jdbcType=CHAR}, #{value,jdbcType=DOUBLE}, #{resourceType,jdbcType=VARCHAR}
)
</insert>
<insert id="insertBatch">
insert into performance_metirc_business_${tableNameSuffix} (dimension_id, indicator_key, create_at, pool_id, value, resource_type )
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.dimensionId,jdbcType=CHAR}, #{item.indicatorKey,jdbcType=VARCHAR}, #{item.createAt,jdbcType=TIMESTAMP},
#{item.poolId,jdbcType=CHAR}, #{item.value,jdbcType=DOUBLE}, #{item.resourceType,jdbcType=VARCHAR} )
</foreach>
</insert>
<insert id="insertSelective" parameterType="com.chinamobile.epic.dao.model.PerformanceMetricAnalysisModel">
insert into performance_metirc_business_${tableNameSuffix}
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="poolId != null">
pool_id,
</if>
<if test="dimensionId != null">
dimension_id,
</if>
<if test="indicatorKey != null">
indicator_key,
</if>
<if test="value != null">
value,
</if>
<if test="resourceType != null">
resource_type,
</if>
<if test="createAt != null">
create_at,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="poolId != null">
#{poolId,jdbcType=CHAR},
</if>
<if test="dimensionId != null">
#{dimensionId,jdbcType=CHAR},
</if>
<if test="indicatorKey != null">
#{indicatorKey,jdbcType=VARCHAR},
</if>
<if test="value != null">
#{value,jdbcType=DOUBLE},
</if>
<if test="resourceType != null">
#{resourceType,jdbcType=VARCHAR},
</if>
<if test="createAt != null">
#{createAt,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.chinamobile.epic.dao.model.PerformanceMetricAnalysisModel">
update performance_metirc_business_${tableNameSuffix}
<set>
<if test="poolId != null">
pool_id = #{poolId,jdbcType=CHAR},
</if>
<if test="value != null">
value = #{value,jdbcType=DOUBLE},
</if>
<if test="resourceType != null">
resource_type = #{resourceType,jdbcType=VARCHAR},
</if>
</set>
where dimension_id = #{dimensionId,jdbcType=CHAR}
and indicator_key = #{indicatorKey,jdbcType=VARCHAR}
and create_at = #{createAt,jdbcType=TIMESTAMP}
</update>
<update id="updateByPrimaryKey" parameterType="com.chinamobile.epic.dao.model.PerformanceMetricAnalysisModel">
update performance_metirc_business_${tableNameSuffix}
set pool_id = #{poolId,jdbcType=CHAR},
value = #{value,jdbcType=DOUBLE},
resource_type = #{resourceType,jdbcType=VARCHAR}
where dimension_id = #{dimensionId,jdbcType=CHAR}
and indicator_key = #{indicatorKey,jdbcType=VARCHAR}
and create_at = #{createAt,jdbcType=TIMESTAMP}
</update>
</mapper>
【Mybatis】mybatis使用示例的更多相关文章
- springmvc 项目完整示例04 整合mybatis mybatis所需要的jar包 mybatis配置文件 sql语句 mybatis应用
百度百科: MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBat ...
- 单独使用MyBatis的简单示例
单独使用MyBatis的简单示例:mybaties-config.xml:MyBatis配置文件 <?xml version="1.0" encoding="UTF ...
- (转)MyBatis & MyBatis Plus
(二期)3.mybatis与mybatis plus [课程三]mybatis ...运用.xmind0.1MB [课程三]mybatis...机制.xmind0.2MB [课程三]mybatis与j ...
- MyBatis -01- 初识 MyBatis + MyBatis 环境搭建
MyBatis -01- 初识 MyBatis + MyBatis 环境搭建 MyBatis 本是 apache 的一个开源项目 iBatis(iBATIS = "internet" ...
- 项目脚手架 - 《Spring Boot + MyBatis + MyBatis Generator》
前言 最近启动了一个新的项目发现,每当一个新项目的启动往往需要从头搭建一个"框架",其中虽然很多基础代码可以Copy,但也会浪费不少时间. 基于这个情况,我打算在GitHub上创建 ...
- mybatis 简单使用示例(单独使用):
mybatis的单独使用简单示例: 步骤1: 新建xml文件. 示例: <?xml version="1.0" encoding="UTF-8" ?> ...
- MyBatis的association示例——MyBatis学习笔记之三
前两篇博文介绍的都是单表映射,而实际上很多时候我们需要用到较复杂的映射.今天学会的association的用法,就是一例,现写出来和大家分享(为简洁起见,ant工程中各文件.目录的布局,以及其它与前面 ...
- Mybatis的简单示例
首先新建一个JavaWeb项目并导入mybatis依赖的jar包,同时Mybatis是对数据库的操作所以我们需要在数据库中新建一个表user用来演示. 新建完表之后我们还需要建立相对应的实体类User ...
- MyBatis7:MyBatis插件及示例----打印每条SQL语句及其执行时间
Plugins 摘一段来自MyBatis官方文档的文字. MyBatis允许你在某一点拦截已映射语句执行的调用.默认情况下,MyBatis允许使用插件来拦截方法调用 Executor(update.q ...
- MyBatis Generator配置示例
(一).MBG介绍 MyBatis Generator(MBG)是一个Mybatis的代码生成器,它可以用来生成可以访问(多个)表的基础对象.MBG解决了对数据库操作有最大影响的一些简单的CRUD(插 ...
随机推荐
- Unity 项目中委托Delegate用法案例
Unity中Delegate的用法场景 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar - ...
- input标签(按钮)
按钮: <input type="button" name="..." value="..." /> <input typ ...
- xdoj 1241--余神的rp机(区间dp)
xdoj 1241---余神的rp机 核
- fast ai环境配置
https://www.jianshu.com/p/2fe22a6b0ecb https://www.jianshu.com/p/b1be2af2f2ef https://blog.csdn.net/ ...
- webapi core2.1 IdentityServer4.EntityFramework Core进行配置和操作数据
https://identityserver4.readthedocs.io/en/release/quickstarts/8_entity_framework.html 此连接的实践 vscode ...
- sqlyog数据库管理软件下载
下载安装包 一路next 链接:https://pan.baidu.com/s/1tdIwtYEW11MNWk--Hqd3dw 提取码:0ffp 复制这段内容后打开百度网盘手机App,操作更方便哦 然 ...
- MySQL Lock--并发插入导致的死锁
============================================================================ 测试脚本: 表结构: CREATE TABLE ...
- 转-[WebServer] Windows操作系统下 Tomcat 服务器运行 PHP 的环境配置
原文 前言: 由于本人在开发和学习过程中需要同时部署 JavaWeb 和 PHP 项目,于是整理了网上的一些相关资料,并结合自己的实际操作,记录于此,以供参考. 一.环境(64bit): 1.操作系统 ...
- [转]JDK动态代理
代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代理类与委托类之间 ...
- ubuntu忘记登录密码解决方法
1.重启系统,长按Shift键,直到出现下面菜单.选择recovery mode(恢复模式).2.接下来会进入如下界面,选择Drop to root shell prompt ,也就是获取root权限 ...