mybatis中的javatype和oftype
时间: 2023-04-26 07:04:51 浏览: 64
Mybatis中的javatype和oftype是用于指定Java类型和数据库类型之间的映射关系的。
javatype是指Java中的数据类型,oftype是指数据库中的数据类型。在Mybatis中,我们可以通过配置文件来指定它们之间的映射关系,以便在进行数据库操作时能够正确地将数据类型转换为对应的Java类型或数据库类型。
例如,如果我们要将Java中的Date类型映射到数据库中的timestamp类型,可以在配置文件中这样写:
<resultMap id="userMap" type="com.example.User">
<result column="create_time" property="createTime" javaType="java.util.Date" jdbcType="TIMESTAMP"/>
</resultMap>
这里的javaType指定了Java中的数据类型,jdbcType指定了数据库中的数据类型。这样,在进行数据库操作时,Mybatis就会自动将Java中的Date类型转换为数据库中的timestamp类型,或者将数据库中的timestamp类型转换为Java中的Date类型。
相关问题
Mybatis 统计 数据库中time 类型的和
在 MyBatis 中,可以使用自定义的 TypeHandler 来将数据库中的 time型转换为 Java 中的 Duration 类型,然后进行求和操作。具体实现步骤如下:
1. 创建一个实现了 TypeHandler 接口的类,用于将数据库中的 time 类型转换为 Java 中的 Duration 类型。
```java
public class DurationTypeHandler extends BaseTypeHandler<Duration> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Duration parameter, JdbcType jdbcType) throws SQLException {
ps.setTime(i, Time.valueOf(LocalTime.MIDNIGHT.plus(parameter)));
}
@Override
public Duration getNullableResult(ResultSet rs, String columnName) throws SQLException {
Time time = rs.getTime(columnName);
return time != null ? Duration.between(LocalTime.MIDNIGHT, time.toLocalTime()) : null;
}
@Override
public Duration getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Time time = rs.getTime(columnIndex);
return time != null ? Duration.between(LocalTime.MIDNIGHT, time.toLocalTime()) : null;
}
@Override
public Duration getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Time time = cs.getTime(columnIndex);
return time != null ? Duration.between(LocalTime.MIDNIGHT, time.toLocalTime()) : null;
}
}
```
2. 在 Mapper.xml 文件中配置 TypeHandler,指定时间类型的列使用该 TypeHandler 进行转换。
```xml
<typeHandlers>
<typeHandler javaType="java.time.Duration" jdbcType="TIME" handler="com.example.DurationTypeHandler"/>
</typeHandlers>
```
3. 在 Mapper.xml 文件中编写 SQL 语句,使用 #{} 占位符来引用方法参数。
例如,如果 Mapper 接口中的方法参数为一个实体类对象,可以使用如下 SQL 语句:
```xml
<select id="sumTimeColumn" parameterType="com.example.entity.Entity" resultType="java.time.Duration">
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(column_name))) FROM table_name WHERE condition=#{timeProperty, typeHandler=com.example.DurationTypeHandler};
</select>
```
其中,timeProperty 为实体类中 time 类型属性的名称。
4. 在代码中调用 Mapper 接口中的方法,传入参数,即可得到查询结果。
例如,如果要统计某个表中 time 类型的列的总和,并且该列的值为某个实体类对象的属性值,可以使用如下代码:
```java
Entity entity = new Entity();
entity.setTimeProperty(Duration.ofHours(1).plusMinutes(30));
Duration result = mapper.sumTimeColumn(entity);
```
其中,mapper 是 Mapper 接口的实例,sumTimeColumn 是 Mapper 接口中定义的方法名。最终得到的 result 变量的类型为 java.time.Duration。需要注意的是,如果查询结果为 null,则 result 变量也会为 null。
mybatis中collection中再写一个collection
MyBatis中的collection标签可以用于处理嵌套的集合类型,但是不支持在collection标签中再写一个collection标签。如果需要处理嵌套的集合类型,则需要使用嵌套的resultMap或association标签来处理。
举个例子,如果有一个Order对象,其中包含多个OrderItem对象,而每个OrderItem对象又包含多个Product对象,可以按以下方式编写映射文件:
```
<resultMap id="orderResultMap" type="Order">
<id property="id" column="order_id"/>
<result property="orderNo" column="order_no"/>
<collection property="orderItems" ofType="OrderItem">
<id property="id" column="item_id"/>
<result property="quantity" column="quantity"/>
<result property="price" column="price"/>
<association property="product" javaType="Product">
<id property="id" column="product_id"/>
<result property="name" column="product_name"/>
<result property="description" column="product_desc"/>
</association>
</collection>
</resultMap>
```
可以看到,在处理嵌套集合类型时,使用了association标签来处理嵌套的Product对象。