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>
说明:
- 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类中也是类似的情况。
上面我只是描写了最简单的使用,更深层因为我在实际应用中并没有遇到所以,没有调研到。希望这些可以对大家有帮助,如果哪里写的不足,可以在下方留言评论。