ProtoBuf与Mybatis的不解之缘

看过我ProtoBuf在java中的实际应用这篇文章的,在最后应该都有看到proto类与pojo类的一个转换案例。为什么写这个案例?因为有时候我们前端展示和数据传输的数据是一样的,处于这种原因我写了一个案例。
如果遇到前端不需要展示这些数据呢?这里用上述方法反而使效率下降了。所以有没有从数据查找到数据直接封装到proto类中的方法呢?当然是有的,下面我们对其进行一个简单介绍。
此处我以MyBatis与ProtoBuf集成为例,我还是沿用初识ProtoBuf生成的proto类。
对于数据库的操作(增删改查)的操作。

增删改操作

<!-- 添加数据 -->
<insert id="saveForProto" parameterType="com.mrzhang.study.protobufdemo.protos.PersonProto$Person" keyProperty="id_" useGeneratedKeys="true">
  insert into prople
  <trim prefix="(" suffix=")" suffixOverrides=",">
    <if test="id != null">
        id,
    </if>
    <if test="name != null">
        name,
    </if>
    <if test="age != null">
        age,
    </if>
    <if test="birthday != null">
        birthday,
    </if>
  </trim>
  <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
          #{id},
      </if>
      <if test="name != null">
          #{name},
      </if>
      <if test="age != null">
          #{age},
      </if>
      <if test="birthday != null">
          #{birthday},
      </if>
  </trim>
</insert>

说明:

  • keyProperty="id_":这个配置可能有些人看不懂,为什么不是keyProperty="id",这是因为在proto类中并没有setId()方法。其次proto类中转换为我们在.proto文件定义的字段名称加下划线的形式存在与proto类中,此处大家可以查看初识ProtoBuf最后摘抄proto类的部分代码,当然也可以去看proto类。
  • birthday直接赋值给数据库会不会存在问题,我在此处尝试过使用java String类型,格式为"yyyy-MM-dd HH:mm:ss"或者以毫秒值存储对应MySQL数据库DATETIME是没有问题的。
  • parameterType="com.mrzhang.study.protobufdemo.protos.PersonProto$Person":由于Person实际是proto类的内部类可以通过这种方式书写,也可以通过Mybatis中命名别名的方式进行设置。

对于数据库的增、删、改都是类似的操作,此处我就不再重复了。


查询操作
我想这里也是大家比较关注的地方,下面我先展示一个案例。

<select id="selectAll" resultMap="personProtoMap">
    select id, name, age, birthday from person
</select>


<resultMap id="personProtoMap" type="com.mrzhang.study.protobufdemo.protos.PersonProto$Prople">
    <id column="id" property="id_" />
    <id column="name" property="name_" />
    <id column="age" property="age_" />
    <id column="birthday" property="birthday_" typeHandler="com.mrzhang.study.protobufproject.utils.handler.DateTime2LongHandler"/>
</resultMap>

说明:

  • 就像查询中说到的一样,proto类中并没有setId(),setName()等方法,所以我们只能够通过对查询SQL字段起别名的方式(存在一定的缺陷,就像我在添加的说明中写到的,有时候我们的java类型与MySQL的数据类型不能够直接转换时就会出现问题),也可以通过Mybatis提供的resultMap来映射。
  • birthday映射问题,由于数据库存储的是DATETIME类型,与java属性的类型转换存在问题,不能通过Mybatis的内置转换器转换的时候我们就需要我们自己实现转换器了。

转换器如下:

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.*;

/**
 * 时间转换Long
 */
@MappedJdbcTypes(value = {JdbcType.TIMESTAMP})
@MappedTypes({Long.class, long.class})
public class DateTime2LangHandler extends BaseTypeHandler<Long> {


    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Long parameter, JdbcType jdbcType) throws SQLException {
        ps.setTimestamp(i, new Timestamp(i));
    }

    @Override
    public Long getNullableResult(ResultSet rs, String columnName) throws SQLException {
        Timestamp timestamp = rs.getTimestamp(columnName);

        if (timestamp != null) {
            return timestamp.getTime();
        } else {
            return null;
        }
    }

    @Override
    public Long getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        Timestamp timestamp = rs.getTimestamp(columnIndex);

        if (timestamp != null) {
            return timestamp.getTime();
        } else {
            return null;
        }
    }

    @Override
    public Long getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        Timestamp timestamp = cs.getTimestamp(columnIndex);

        if (timestamp != null) {
            return timestamp.getTime();
        } else {
            return null;
        }
    }
}

说明:
类型转换只需要继承BaseTypeHandler类实现方法即可。具体方法有何用处我在这里不细说了。
上面只是说如何查询单个数据,查询所条数据项封装在proto类中也是类似的情况。


上面我只是描写了最简单的使用,更深层因为我在实际应用中并没有遇到所以,没有调研到。希望这些可以对大家有帮助,如果哪里写的不足,可以在下方留言评论。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 彼得兔的名字,是来自于作者波特小姐童年时所饲养的一只兔子。最早的彼得兔故事原形,是来自于1893年,波特小姐写给她...
    闻听西辞阅读 2,640评论 0 6
  • 本次会议主要是随意分享和探索检视,主要内容汇总如下: 一、关于大群和小群议程的讨论。大群是更好的弱连接信息资源,通...
    kikiluo阅读 288评论 0 0
  • 早上闻到宿舍里的饭特别香,依然睡大懒觉直都饿到受不了才起床吃饭。 吃完饭12点了,回教室里拿了声乐书。在没人的楼梯...
    逆风追梦人阅读 210评论 0 0
  • 这本书回答了,一个公司怎样能打开市场,怎样经久不衰,怎样能不被竞争者打败,怎样能面对技术浪潮的变革。“整合”在这过...
    Molly_0阅读 375评论 0 0
  • 昨天我说到碎片化学习不等同于碎片化知识,因为知识学习是有脉络框架的,掌握了框架脉络,即使是碎片化时间依然可以保持高...
    我是哼哼哈嘿阅读 278评论 0 0