spring boot 第六节 注解版

博客主要围绕Spring Boot展开,介绍了第五节XML版中出现的各种报错及解决办法,如mapper的xml文件与接口未放一处的解决方式,还给出相关参考文章。同时提到第六节注解版,步骤是删掉xml文件并将接口文件修改为注解类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring boot 第五节 xml版 各种报错解决

IDEA org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

参考文章:https://blog.youkuaiyun.com/qq_37746483/article/details/82877323

mapper的xml 文件与接口没有放在同一个地方。

解决方案:POM文件里面加入如下,maven clean 然后compile 就可以看到target里面,两个都有了,xml和class文件

        <resources>
        <resource>
            <directory>src/main/java/</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        </resources>

 

 

报错二:

Consider defining a bean of type 'com.example.springbootdruid.mapper.UserMapper' in your configurati

参考文章:https://cloud.tencent.com/developer/article/1386113

启动的时候,加入如下即可

@SpringBootApplication//标识该项目为springboot的应用
        @MapperScan("com.xk.mapper")

 

配置文件里面加上

mybatis.mapperLocations=classpath:mapper/*.xml 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

spring boot 第六节 注解版

步骤:就是删掉xml文件,然后接口文件修改为注解类的。

package com.xk.mapper;


import com.xk.domain.User;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserMapper {
    @Insert("insert into t_user(name) values(#{name})")
    @Options(useGeneratedKeys=true,keyColumn="id",keyProperty="id")
    //保存一个对象
    void save(User user);

    @Delete("delete from t_user where id = #{id}")
        //移除一个对象
    void remove(Long id);

    //更新对象
    @Update("update t_user set name = #{name} where id = #{id}")
    void update(User user);

    @Select("select * from t_user where id = #{id}")
        //通过Id加载一个对象
    User loadById(Long id);

    @Select("select * from t_user")
        //加载所有的对象
    List<User> loadAll();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值