Mybatis-无效的绑定语句(未找到)

时间:2019-03-05 10:35:15

标签: spring-boot mybatis

我使用mybatis创建了一个新项目以连接到mysql数据库。这是我的第二个使用mybatis的项目,所以我对此很熟悉,但是在调用save方法时出现以下错误:

2019-03-05 10:08:01.031错误86438 --- [nio-9905-exec-1] cqfrcResolveRestController:启动预设错误:无效的绑定语句(未找到):com.qlsdistribution.fps .resolverender.data.mybatis.mapper.ResolveJobReportMapper.saveResolveJobReport org.apache.ibatis.binding.BindingException:无效的绑定语句(未找到):com.qlsdistribution.fps.resolverender.data.mybatis.mapper.ResolveJobReportMapper.saveResolveJobReport     在org.apache.ibatis.binding.MapperMethod $ SqlCommand。(MapperMethod.java:232)〜[mybatis-3.5.0.jar:3.5.0]     在org.apache.ibatis.binding.MapperMethod。(MapperMethod.java:50)〜[mybatis-3.5.0.jar:3.5.0]     在org.apache.ibatis.binding.MapperProxy.lambda $ cachedMapperMethod $ 0(MapperProxy.java:62)〜[mybatis-3.5.0.jar:3.5.0]     在java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)〜[na:1.8.0_101]     在org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:62)〜[mybatis-3.5.0.jar:3.5.0]  .....

我的mybatis配置文件如下(在src / main / resources中):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <settings>
        <!-- changes from the defaults -->
       <setting name="lazyLoadingEnabled" value="true" />
       <!-- Mybatis logging -->
       <setting name="logImpl" value="LOG4J2"/>
    </settings>
    <typeAliases>
        <package name="com.qlsdistribution.fps.resolverender.data.mybatis.domain"/>

    </typeAliases>

   <mappers>
      <mapper resource="mapper/ResolveJobReport.xml"/>
<!--       <mapper resource="com/qlsdistribution/fps/resolverender/data/mybatis/mapper/ResolveJobReport.xml"/> -->
   </mappers>
</configuration>

如您所见,我为mapper xml文件尝试了不同的位置,但是如果我在mapper xml文件中放置了无效的语法,它会因无效的语法(SAXParseException)而失败,因此我知道正在读取mapper xml文件。 / p>

mapper xml文件如下(在src / main / resources / mapper中):

<?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.qlsdistribution.fps.resolverender.data.mybatis.ResolveJobReportMapper">

    <resultMap id="resolveJobReport" type="com.qlsdistribution.fps.resolverender.data.mybatis.domain.ResolveJobReport">
        <result property="id" column="id" />
        <result property="fpsProjectName" column="fps_project_name" />
        <result property="inputPath" column="input_path" />
        <result property="destinationPath" column="destination_path" />
        <result property="presetName" column="preset_name" />
        <result property="ipAddress" column="ip_address" />
        <result property="frameRate" column="frame_rate" />
        <result property="resolveProjectName" column="resolve_project_name" />
        <result property="width" column="width" />
        <result property="height" column="height" />
        <result property="renderFormat" column="render_format" />
        <result property="renderCodec" column="render_codec" />
        <result property="scriptPath" column="script_path" />
        <result property="cliOutput" column="cli_output" />
        <result property="jobStartedDate" column="job_started_date" />
        <result property="jobFinishedDate" column="job_finished_date" />
        <result property="createdBy" column="created_by" />
        <result property="createdDate" column="created_date" />
        <result property="modifiedBy" column="modified_by" />
        <result property="modifiedDate" column="modified_date" />
    </resultMap>

    <select id="getAllResolveJobReports" resultMap="resolveJobReport">
        SELECT id, fps_project_name, input_path, destination_path, preset_name, ip_address, frame_rate, resolve_project_name, width, height, 
        render_format, render_codec, script_path, cli_output, job_started_date, job_finished_date, created_by, created_date, modified_by, modified_date
        FROM resolve_job_report
        WHERE fps_setting_id = #{value}
        ORDER by id desc;
    </select>

    <select id="getAllResolveJobReportsById" parameterType="Long"  resultMap="resolveJobReport">
        SELECT id, fps_project_name, input_path, destination_path, preset_name, ip_address, frame_rate, resolve_project_name, width, height, 
        render_format, render_codec, script_path, cli_output, job_started_date, job_finished_date, created_by, created_date, modified_by, modified_date
        FROM resolve_job_report
        WHERE id = #{value};
    </select>

    <insert id="saveResolveJobReport" parameterType="com.qlsdistribution.fps.resolverender.data.mybatis.domain.ResolveJobReport">
        INSERT INTO resolve_job_report 
        (fps_project_name, input_path, destination_path, preset_name, ip_address, frame_rate, resolve_project_name, width, height, 
        render_format, render_codec, script_path, cli_output, job_started_date, job_finished_date, created_by)
        VALUE 
        (#{fpsProjectName},#{inputPath},#{destinationPath},#{presetName},#{ipAddress},#{frameRate},#{resolveProjectName},#{width},#{height},
        #{renderFormat},#{renderCodec}, #{scriptPath},#{cliOutput},#{jobStartedDate},#{jobFinishedDate},#{createdBy});
    </insert>

    <update id="updateResolveJobReportById" parameterType="resolveJobReport">
        UPDATE resolve_job_report
        SET
        fps_project_name = #{fpsProjectName}, 
        input_path = #{inputPath}, 
        destination_path = #{destinationPath}, 
        preset_name = #{presetName}, 
        ip_address = #{ipAddress}, 
        frame_rate = #{frameRate}, 
        resolve_project_name = #{resolveProjectName}, 
        width = #{width}, 
        height = #{height}, 
        render_format = #{renderFormat}, 
        render_codec = #{renderCodec}, 
        script_path = #{scriptPath}, 
        cli_output = #{cliOutput}, 
        job_started_date = #{jobStartedDate}, 
        job_finished_date = #{jobFinishedDate},
        modified_by = #{modifiedBy}
        where id = #{id};
    </update>

    <delete id="deleteResolveJobReporttById" parameterType="Long">
        DELETE FROM resolve_job_report
        WHERE id = #{value}
    </delete>
</mapper>

我的application.properties文件包含以下内容(在(src / main / resources)中:

spring.datasource.url=jdbc:mysql://localhost:3306/fpsresolvetool?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.schema=classpath:schema.sql

mybatis.config-location=classpath:SqlMapConfig.xml

我什至尝试在pom.xml中使用最新的mybatis版本:

    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.0</version>
    </dependency>

我的spring boot应用程序如下:

@SpringBootApplication
@EnableScheduling
@ComponentScan({"com.qlsdistribution.fps.resolverender"})
@EnableJms
@MapperScan("com.qlsdistribution.fps.resolverender.data.mybatis")
public class FPSResolveRenderApp implements WebMvcConfigurer {
    /** UTF-8 Character set name */
    private static final String UTF_8 = "UTF-8";

    /** Logger */
    private static final Logger logger = LogManager.getLogger(FPSResolveRenderApp.class);

    public static void main(String[] args) {
        new SpringApplicationBuilder(FPSResolveRenderApp.class).run(args);

    }

    /**
     * Creates and gets the FilterRegistrationBean
     * @return
     */
    @Bean
    public FilterRegistrationBean<CharacterEncodingFilter> filterRegistrationBean() {
        FilterRegistrationBean<CharacterEncodingFilter> registrationBean = new FilterRegistrationBean<CharacterEncodingFilter>();
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding(UTF_8);
        registrationBean.setFilter(characterEncodingFilter);
        return registrationBean;
    }
}

映射器界面如下:

@Mapper
public interface ResolveJobReportMapper {

    public List<ResolveJobReport> getAllResolveJobReports();

    public List<ResolveJobReport> getAllResolveJobReports(RowBounds rowBounds);

    public List<ResolveJobReport> getAllResolveJobReportsById(Long id);

    public List<ResolveJobReport> getAllResolveJobReportsById(Long id, RowBounds rowBounds);

    public void saveResolveJobReport(ResolveJobReport resolveJobReport);

    public void updateResolveJobReportById(ResolveJobReport resolveJobReport);

    public void deleteResolveJobReporttById(Long id);

}

服务类如下:

@Service("ResolveJobReportService")
public class ResolveJobReportService {

    @Autowired
    private ResolveJobReportMapper resolveJobReportMapper= null;

    public List<ResolveJobReport> getAllResolveJobReports() {
        return resolveJobReportMapper.getAllResolveJobReports();
    }

    public List<ResolveJobReport> getAllResolveJobReports(RowBounds rowBounds) {
        return resolveJobReportMapper.getAllResolveJobReports();
    }

    public List<ResolveJobReport> getAllResolveJobReportsById(Long id) {
        return resolveJobReportMapper.getAllResolveJobReportsById(id);
    }

    public List<ResolveJobReport> getAllResolveJobReportsById(Long id, RowBounds rowBounds) {
        return resolveJobReportMapper.getAllResolveJobReportsById(id);
    }

    public void saveResolveJobReport(ResolveJobReport resolveJobReport) {
        resolveJobReportMapper.saveResolveJobReport(resolveJobReport);
    }

    public void updateResolveJobReportById(ResolveJobReport resolveJobReport) {
        resolveJobReportMapper.updateResolveJobReportById(resolveJobReport);
    }

    public void deleteResolveJobReporttById(Long id) {
        resolveJobReportMapper.deleteResolveJobReporttById(id);
    }

}

谁能看到哪里出了问题。

6 个答案:

答案 0 :(得分:1)

发生问题是因为映射器xml中的namespacecom.qlsdistribution.fps.resolverender.data.mybatis.ResolveJobReportMapper,但是映射器接口的包是com.qlsdistribution.fps.resolverender.data.mybatis.mapper.ResolveJobReportMapper

要对其进行修复,请确保它们匹配。

答案 1 :(得分:1)

当我将项目从JavaFX Project更改为Maven Project时,我有相同的错误消息。

我必须将所有xml映射器文件移动到资源文件夹。之后,就可以了。

答案 2 :(得分:1)

对于那些在 spring boot 项目中使用 MyBatis 而没有 xml 的人:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>

请检查您的 Spring Boot 应用程序类,确保您的 @MapperScan 与您的 dao 包匹配。

@MapperScan("com.jjs.videoservice.dao") 

路径必须与您的 dao 包(包含您的映射器接口)相同,我忽略了“dao”并导致了问题。

希望对大家有所帮助,谢谢

答案 3 :(得分:0)

我最近遇到了这个问题,并检查了可以在互联网上找到的所有内容。花了我一整天。

最后,我将xml文件与接口java文件放在同一位置,从而解决了问题。

我认为这是因为由于某些原因而无法找到xml文件,并将其放在其中可以看到它们。

答案 4 :(得分:0)

我遇到了同样的问题

对我来说,修复程序为 java(映射器接口类)和相应的 xml 文件提供了相同的文件名

答案 5 :(得分:0)

将其放入 application.properties mybatis.mapper-locations=classpath*:**/mappers/*Mapper.xml