MyBatis - 如何在保存到数据库之前转换数据 table
MyBatis - How to transform data before saving to DB table
我在简单的 Java 应用程序中使用 MyBatis 将数据保存到 SQLite 数据库。 Java class 和DB table 通信中是否可以配置MyBatis 进行数据转换?
例如,我的 Java class 中有 java.util.Date 对象,我想将其保存为数字(Unix 时间)。另一个例子:在 Java class 中,我有一个 BigDecimal 数字,我想将其存储为文本(我不想失去任何精度)。
在 Java 对象中,我使用适当的 getter 和 setter 来转换这些数据,但我没有看到在 MyBatis .xml 文件中指定它们的任何可能性。
综上所述,我的问题:
1、我可以在MyBatis Result Map中使用Java个getters和setters吗?如果是,比如何?
2. 如何通过MyBatis在DB中正确存储复杂数据(如Dates和BigDecimals)?
我很确定 mybatis 的 'bind' 元素可以为您省去麻烦
<select id="selectBlogsLike" resultType="Blog">
<bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
SELECT * FROM BLOG
WHERE title LIKE #{pattern}
</select>
在本例中,它使用 getter 并连接 %。对于您的情况,您可以在那里进行简单的转换。
我已经通过使用 TypeHandler class 开始工作了。下面是将 BigDecimal 映射到 TEXT 数据库列的示例代码:
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>
我在简单的 Java 应用程序中使用 MyBatis 将数据保存到 SQLite 数据库。 Java class 和DB table 通信中是否可以配置MyBatis 进行数据转换?
例如,我的 Java class 中有 java.util.Date 对象,我想将其保存为数字(Unix 时间)。另一个例子:在 Java class 中,我有一个 BigDecimal 数字,我想将其存储为文本(我不想失去任何精度)。
在 Java 对象中,我使用适当的 getter 和 setter 来转换这些数据,但我没有看到在 MyBatis .xml 文件中指定它们的任何可能性。
综上所述,我的问题:
1、我可以在MyBatis Result Map中使用Java个getters和setters吗?如果是,比如何?
2. 如何通过MyBatis在DB中正确存储复杂数据(如Dates和BigDecimals)?
我很确定 mybatis 的 'bind' 元素可以为您省去麻烦
<select id="selectBlogsLike" resultType="Blog">
<bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
SELECT * FROM BLOG
WHERE title LIKE #{pattern}
</select>
在本例中,它使用 getter 并连接 %。对于您的情况,您可以在那里进行简单的转换。
我已经通过使用 TypeHandler class 开始工作了。下面是将 BigDecimal 映射到 TEXT 数据库列的示例代码:
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>