Spring Boot + MyBatis映射时间戳到LocalDateTime不起作用

时间:2018-01-14 14:11:14

标签: java spring-boot mybatis ibatis

  

当我尝试获取Purchase实例(通过id或来自User)时,我有   下一个错误:

org.apache.ibatis.executor.ExecutorException: No constructor found in com.lisonn.mybatis.entity.Purchase matching [java.lang.Long, java.lang.String, java.lang.Integer, java.sql.Timestamp, java.lang.Integer]
  

但是Timestamp应该自动转换为   LocalDateTime,正如我在PurchaseResultMap中设置的那样(我试过使用   对于计时字段的“结果”,我也将jdbcType设置为TIMESTAMP,没有   帮助)。

     

将时间戳转换为LocalDateTime有什么问题?我   使用Spring Boot 1.5.9,MyBatis Spring Boot Starter 1.3.1

     

谢谢你提前!以下是我的项目配置。

application.yaml:

...
mybatis:
  mapper-locations: classpath:mappers/**
  type-aliases-package: com.lisonn.mybatis.entity
  type-handlers-package: org.apache.ibatis.type.handler

PurchaseMapper.xml:

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lisonn.mybatis.mapper.PurchaseMapper">
    <resultMap id="PurchaseResultMap" type="com.lisonn.mybatis.entity.Purchase">
        <id column="id" property="id" />
        <result column="naming" property="naming" />
        <result column="price" property="price" />
        <association column="timing" property="timing" javaType="java.time.LocalDateTime"/>
        <association property="user" column="user_id" javaType="com.lisonn.mybatis.entity.User"
                     select="selectUserForPurchase"/>
    </resultMap>

    <select id="selectUserForPurchase"
            parameterType="java.lang.Integer"
            resultType="com.lisonn.mybatis.entity.User">
        SELECT *
        FROM
        mybatis.users
        WHERE
        id = #{user_id}
    </select>

    <select id="findOne"
            parameterType="java.lang.Integer"
            resultMap="PurchaseResultMap"
            resultType="com.lisonn.mybatis.entity.Purchase"
    >
        SELECT *
        FROM mybatis.purchase
        where id = #{id}
    </select>

</mapper>

UserMapper.xml:

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lisonn.mybatis.mapper.UserMapper">
    <resultMap id="UserResultMap" type="com.lisonn.mybatis.entity.User">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <result column="email" property="email" />
        <collection property="purchases" ofType="com.lisonn.mybatis.entity.Purchase"
                    column="id" select="selectUserPurchases" />
    </resultMap>

    <select id="findAllUsers" resultMap="UserResultMap">
        select id, name, email from users
    </select>

    <select id="findUserById" resultMap="UserResultMap">
        select id, name, email from users WHERE id=#{id}
    </select>

    <select id="findUsersWhereNameLike" resultMap="UserResultMap" parameterType="String">
        select id, name, email from users WHERE id=#{id}
    </select>

    <select id="selectUserPurchases"
            resultMap="com.lisonn.mybatis.mapper.PurchaseMapper.PurchaseResultMap"
            parameterType="java.lang.Integer"
            resultType="com.lisonn.mybatis.entity.Purchase">
        SELECT *
        FROM
        mybatis.purchase
        WHERE
        user_id = #{id}
    </select>

    <insert id="insertUser" parameterType="User" useGeneratedKeys="true" keyProperty="id">
        insert into users(name,email) values(#{name},#{email})
    </insert>
</mapper>

用户:

public class User
{
    private Integer id;
    private String name;
    private String email;
    private List<Purchase> purchases;
    ...
}

采购:

public class Purchase {

    private Long id;
    private String naming;
    private Integer price;
    private LocalDateTime timing;
    private Integer userId;
    private User user;
    ...
}

0 个答案:

没有答案