【问题标题】:java.sql.Timestamp value; nested exception is java.sql.SQLDataException in Spring-boot projectjava.sql.Timestamp 值;嵌套异常是 Spring-boot 项目中的 java.sql.SQLDataException
【发布时间】:2019-07-03 02:04:57
【问题描述】:

我正在用spring boots构建一个项目,通过控制检查互联网的状态,我正在连接数据库。

我正在尝试执行“MyBatis”,但出现错误。

这是我的目录列表:

MinitoringdataApplication.java

package com.smartcore.mn.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan(basePackages = "com.smartcore.mn.springboot")
public class MinitoringdataApplication {

    public static void main(String[] args) {
        SpringApplication.run(MinitoringdataApplication.class, args);
    }

}

ServletInitializer.java

package com.smartcore.mn.springboot;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MinitoringdataApplication.class);
    }

}

ApiController.java

package com.smartcore.mn.springboot.controller;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.smartcore.mn.springboot.model.Member;
import com.smartcore.mn.springboot.service.MemberService;

@RestController
public class ApiController {

    @Autowired
    MemberService memberService;


    @GetMapping(path = "/helloWorld")
    public String helloWorld() {
        return LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
    }

    @GetMapping(path = "/db")
    public List<Member> selectAllMember() {
         List<Member> members = memberService.getAllMember();
         return members;
    }

}

MemberMapper.interface

package com.smartcore.mn.springboot.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.smartcore.mn.springboot.model.Member;

@Mapper
public interface MemberMapper {
    Member selectMemberById(Long id);
    List<Member> selectAllMember();
    void insertMember(Member member);

}

Member.java

package com.smartcore.mn.springboot.model;

import java.util.Date;

import org.apache.ibatis.type.Alias;

import com.smartcore.mn.springboot.Exception.IdPasswordNotMatchingException;

import lombok.Data;


@Data
@Alias("member")
public class Member {

    private Long id;
    private String email;
    private String password;
    private String name;
    private Date registerDate;

    public Member(String email, String password, String name, Date registerDate) {
        this.email = email;
        this.password = password;
        this.name = name;
        this.registerDate = registerDate;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }

    public String getName() {
        return name;
    }

    public Date getRegisterDate() {
        return registerDate;
    }

    public void changePassword(String oldPassword, String newPassword) {
        if (!password.equals(oldPassword))
            throw new IdPasswordNotMatchingException();
        this.password = newPassword;
    }

}

MemberService.java

package com.smartcore.mn.springboot.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smartcore.mn.springboot.mapper.MemberMapper;
import com.smartcore.mn.springboot.model.Member;

@Service
@Transactional
public class MemberService {

    @Autowired
    MemberMapper memberMapper;

    public Member getMemberById(Long id) {
        return memberMapper.selectMemberById(id);
    }

    public List<Member> getAllMember() {
        return memberMapper.selectAllMember();
    }

    public void addMember(Member member) {
        memberMapper.insertMember(member);
    }

}

application.properties

spring.datasource.url=jdbc:mysql://localhost/mydb?serverTimezone=UTC&autoReconnection=true
spring.datasource.username=mydb
spring.datasource.password=mydb
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.type-aliases-package=com.smartcore.mn.springboot.model
logging.level.com.smartcore.mn.springboot.mapper=TRACE

MemberMapper.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.smartcore.mn.springboot.mapper.MemberMapper">

    <select id="selectMemberById" resultType="member">
         SELECT * 
         FROM MEMBER 
         WHERE ID = #{id}
    </select>

    <select id="selectAllMember" resultType="member">
          SELECT * 
          FROM MEMBER
    </select>

    <insert id="insertMember">
      INSERT INTO MEMBER (EMAIL, PASSWORD, NAME, REGDATE)
      VALUES (#{email}, #{password}, #{name}, #{registerDate})
    </insert>

</mapper>

http://localhost:8080/helloworld工作正常。

但是http://localhost:8080/db看到错误

我需要你的解决方案。提前谢谢你。

我的桌子

【问题讨论】:

  • 实体与数据表的映射不正确。
  • 出了什么问题?
  • 显示您的Member 表。我猜您的结果中有一些意想不到的列,因为您选择了所有列 (SELECT *)。当你澄清你想要的列而不是所有列时,这是一个很好的做法。
  • @Càphêđen 我更新了我的问题;
  • 您确定连接到正确的数据库吗?如我所见,连接字符串中的数据库与您的命令行不同。

标签: java spring hibernate spring-boot spring-mvc


【解决方案1】:

正如我们在 cmets 中的讨论中提到的,MyBatis 正在尝试将结果集中的 NAME 列映射到 Member 构造函数中的 registerDate 参数。

由于您没有为每个字段指定paramName,因此构造函数中arg元素的顺序容易出错。

尝试使用正确的有序参数将结果集映射到构造函数:

Member(String email, String password, String name, Date registerDate) 应该匹配 SELECT EMAIL, PASSWORD, NAME, REGDATE FROM MEMBER

Member(Long id, String email, String password, String name, Date registerDate) 应该匹配 SELECT * FROM MEMBER

【讨论】:

    猜你喜欢
    • 2020-09-01
    • 1970-01-01
    • 2019-07-14
    • 2020-10-18
    • 1970-01-01
    • 2021-05-06
    • 2017-10-20
    • 2018-05-04
    • 1970-01-01
    相关资源
    最近更新 更多