SpringBoot+Mybaits报Invalid bound statement

1.这个错误是配置mapper时出了问题,原以为是xml文件里的namespace配置错误,但是并不是

 2.后来我检查了application.properties原来是扫描UserMapper.xml的路径配置错误

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 是一个快速开发框架,MyBatis 是一款优秀的基于 Java 的持久层框架。在 Spring Boot 中使用 MyBatis 可以极大地提高开发效率和运行效率。 具体步骤如下: 1. 引入 MyBatisMyBatis-SpringBoot-Starter 依赖。 2. 配置数据源和 MyBatis。 3. 编写实体类和映射文件。 4. 编写 DAO 层接口和 SQL 语句。 5. 在 Service 层中调用 DAO 层接口。 6. 在 Controller 层中调用 Service 层方法,返回结果给前端。 示例代码: 1. 引入依赖: ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> ``` 2. 配置数据源和 MyBatis: ``` spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root mybatis.mapper-locations=classpath:mapper/*.xml ``` 3. 编写实体类和映射文件: 实体类: ``` public class User { private Long id; private String name; private Integer age; // 省略 getter 和 setter 方法 } ``` 映射文件: ``` <?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.example.demo.dao.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.entity.User"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="age" property="age" /> </resultMap> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long"> select * from user where id = #{id} </select> </mapper> ``` 4. 编写 DAO 层接口和 SQL 语句: ``` public interface UserMapper { User selectByPrimaryKey(Long id); } ``` 5. 在 Service 层中调用 DAO 层接口: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.selectByPrimaryKey(id); } } ``` 6. 在 Controller 层中调用 Service 层方法,返回结果给前端: ``` @RestController public class UserController { @Autowired private UserService userService; @GetMapping(value = "/user/{id}") public User getUserById(@PathVariable Long id) { return userService.getUserById(id); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值