protobuf

ProtoBuf与Mybatis的不解之缘

2018-11-19  本文已影响0人  孔厌柔

看过我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>

说明:

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


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

<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>

说明:

转换器如下:

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类中也是类似的情况。


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

上一篇 下一篇

猜你喜欢

热点阅读