Java MyBatis-如何在保存到DB表之前转换数据

Java MyBatis-如何在保存到DB表之前转换数据,java,mybatis,Java,Mybatis,我正在使用MyBatis将数据保存到简单Java应用程序中的SQLite数据库。是否可以将MyBatis配置为在Java类和DB表之间的通信中执行数据转换 例如,我的java类中有java.util.Date对象,我想将其另存为number Unix time。另一个例子:在Java类中,我有一个大的十进制数,我想将它存储为文本,我不想失去任何精度 在Java对象中,我使用适当的getter和setter来转换这些数据,但我看不到在MyBatis.xml文件中指定它们的任何可能性 总而言之,我的

我正在使用MyBatis将数据保存到简单Java应用程序中的SQLite数据库。是否可以将MyBatis配置为在Java类和DB表之间的通信中执行数据转换

例如,我的java类中有java.util.Date对象,我想将其另存为number Unix time。另一个例子:在Java类中,我有一个大的十进制数,我想将它存储为文本,我不想失去任何精度

在Java对象中,我使用适当的getter和setter来转换这些数据,但我看不到在MyBatis.xml文件中指定它们的任何可能性

总而言之,我的问题是: 1.我可以在MyBatis结果图中使用Java getter和setter吗?如果是,比如何?
2.如何通过MyBatis在数据库中正确存储复杂数据,如日期和大小数?

我很确定MyBatis的“绑定”元素可以为您省去麻烦

<select id="selectBlogsLike" resultType="Blog">
  <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
  SELECT * FROM BLOG
  WHERE title LIKE #{pattern}
</select>
在本例中,它使用getter并连接%。在您的情况下,您可以在那里进行简单的转换


我很确定mybatis的“绑定”元素可以帮你省去麻烦

<select id="selectBlogsLike" resultType="Blog">
  <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
  SELECT * FROM BLOG
  WHERE title LIKE #{pattern}
</select>
在本例中,它使用getter并连接%。在您的情况下,您可以在那里进行简单的转换


我已经设法通过上课来参加工作了。下面是将BigDecimal映射到文本db列的示例代码:

public class BigDecimalTypeHandler implements TypeHandler<BigDecimal> {
    public BigDecimal getResult(ResultSet rs, int columnIndex) throws SQLException{
        String str_val = rs.getString(columnIndex);
        BigDecimal res;

        if( str_val == null || str_val.equals("") )
            res = new BigDecimal(0);
        else
            res = new BigDecimal(str_val);

        return res;
    }
    public BigDecimal getResult(ResultSet rs, String columnName) throws SQLException{
        String str_val = rs.getString(columnName);
        BigDecimal res;

        if( str_val == null || str_val.equals("") )
            res = new BigDecimal(0);
        else
            res = new BigDecimal(str_val);

        return res;
    }
    public BigDecimal getResult(CallableStatement cs, int columnIndex) throws SQLException{
        String str_val = cs.getString(columnIndex);
        BigDecimal res;

        if( str_val == null || str_val.equals("") )
            res = new BigDecimal(0);
        else
            res = new BigDecimal(str_val);

        return res;
    }
    public void setParameter(PreparedStatement ps, int i, BigDecimal parameter, JdbcType jdbcType) throws SQLException{
        if( parameter != null )
            ps.setString(i, parameter.toString());
        else
            ps.setString(i, "0");
    }
}
在MyBatis配置文件中:

<configuration>
    (...)
    <typeHandlers>
        <typeHandler javaType="BigDecimal" jdbcType="VARCHAR" handler="example.com.BigDecimalTypeHandler"/>
    </typeHandlers>
    (...)
</configuration>

我已经设法通过上课来参加工作了。下面是将BigDecimal映射到文本db列的示例代码:

public class BigDecimalTypeHandler implements TypeHandler<BigDecimal> {
    public BigDecimal getResult(ResultSet rs, int columnIndex) throws SQLException{
        String str_val = rs.getString(columnIndex);
        BigDecimal res;

        if( str_val == null || str_val.equals("") )
            res = new BigDecimal(0);
        else
            res = new BigDecimal(str_val);

        return res;
    }
    public BigDecimal getResult(ResultSet rs, String columnName) throws SQLException{
        String str_val = rs.getString(columnName);
        BigDecimal res;

        if( str_val == null || str_val.equals("") )
            res = new BigDecimal(0);
        else
            res = new BigDecimal(str_val);

        return res;
    }
    public BigDecimal getResult(CallableStatement cs, int columnIndex) throws SQLException{
        String str_val = cs.getString(columnIndex);
        BigDecimal res;

        if( str_val == null || str_val.equals("") )
            res = new BigDecimal(0);
        else
            res = new BigDecimal(str_val);

        return res;
    }
    public void setParameter(PreparedStatement ps, int i, BigDecimal parameter, JdbcType jdbcType) throws SQLException{
        if( parameter != null )
            ps.setString(i, parameter.toString());
        else
            ps.setString(i, "0");
    }
}
在MyBatis配置文件中:

<configuration>
    (...)
    <typeHandlers>
        <typeHandler javaType="BigDecimal" jdbcType="VARCHAR" handler="example.com.BigDecimalTypeHandler"/>
    </typeHandlers>
    (...)
</configuration>

最好举一些例子,这样我们才能更好地帮助你。最好举一些例子,这样我们才能更好地帮助你。首先,谢谢你的回答。这似乎是一种处理WHERE查询的好方法,但我不确定它是否适用于将表行映射到Java类。我在MyBatis中发现了TypeHandler类,我认为这是实现我的目标的正确方法。我会尝试一下,如果我成功了,我会把它作为答案贴在这里。首先,谢谢你的回答。这似乎是一种处理WHERE查询的好方法,但我不确定它是否适用于将表行映射到Java类。我在MyBatis中发现了TypeHandler类,我认为这是实现我的目标的正确方法。我会尝试一下,如果我成功了,我会把它作为答案贴在这里。