色空大师 2023-11-16 03:06 采纳率: 40.9%
浏览 18
已结题

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

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

问题:配置多数据源时,查询接口报错,提示绑定错误,网上说的时生成的mapper.xml问题,我这没有mapper.xml文件,加了下图中的依赖也不行

img

附上个人代码
pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>mybatis-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.29</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>

        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-extension</artifactId>
            <version>3.5.3</version>
        </dependency>

    </dependencies>

</project>

配置文件

server:
  port: 10012
spring:
  application:
    name: dynamic-datasource-mybatis
  datasource:
    master:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&characterEncoding=utf-8
      username: root
      password: root
    school:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://localhost:3306/school?serverTimezone=Asia/Shanghai&characterEncoding=utf-8
      username: root
      password: root


启动类

package com.test.mybatis;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import springfox.documentation.oas.annotations.EnableOpenApi;

/**
 * @author清梦
 * @site www.xiaomage.com
 * @company xxx公司
 * @create 2023-11-11 21:06
 */
@SpringBootApplication(/*exclude = {DataSourceAutoConfiguration.class, MybatisPlusAutoConfiguration.class}*/)
@EnableOpenApi
public class MybatisService {
    public static void main(String[] args) {
        SpringApplication.run(MybatisService.class,args);
    }
}

实体类

package com.test.mybatis.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.io.Serializable;
import java.sql.Date;

/**
 * 学生表
 * @TableName student
 */
@Data
public class Student implements Serializable {
    /**
     * id
     */
    @TableId(type = IdType.AUTO)
    private Integer id;

    /**
     * 学生姓名
     */
    private String studentName;

    /**
     * 学号
     */
    private String studentNo;

    /**
     * 密码
     */
    private String password;

    /**
     * 性别:0-男,1-女
     */
    private Boolean sex;

    /**
     * 生日
     */
    private Date birthday;//java.sql.Date对应数据库date类型,只显示年月日

    /**
     * 身份证号
     */
    private String cardId;

    /**
     * 手机号
     */
    @TableField(exist = false)
    @JsonIgnore
    private String phone;

}

package com.test.mybatis.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

import java.io.Serializable;
import java.sql.Date;

/**
 * 用户表
 * @TableName user
 */
@Data
public class User implements Serializable {
    /**
     * id
     */
    @TableId(type = IdType.AUTO)
    private Integer id;

    /**
     * 用户名
     */
    private String username;

    /**
     * 真实姓名
     */
    private String realName;

    /**
     * 密码
     */
    private String password;

    /**
     * 性别:0-男,1-女
     */
    private Boolean sex;

    /**
     * 生日
     */
    private Date birthday;//java.sql.Date对应数据库date类型,只显示年月日

    /**
     * 身份证号
     */
    private String cardId;

    /**
     * 手机号
     */
    private String phone;

    private static final long serialVersionUID = 1L;
}

配置类

package com.test.mybatis.config;

import com.sun.tracing.ProbeName;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;

/**
 * @author清梦
 * @site www.xiaomage.com
 * @company xxx公司
 * @create 2023-11-11 21:14
 */
@Configuration
@MapperScan(basePackages = "com.test.mybatis.mapper.test",sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {

    @Bean
    @ConfigurationProperties("spring.datasource.master")
    public DataSource masterDatasource(){
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }

    @Primary
    @Bean
    public SqlSessionFactory masterSqlSessionFactory()throws Exception{
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(masterDatasource());
        return bean.getObject();
    }

    @Primary
    @Bean
    public DataSourceTransactionManager masterTransactionManager(){
        DataSourceTransactionManager manager = new DataSourceTransactionManager();
        manager.setDataSource(masterDatasource());
        return manager;
    }

    @Bean
    public TransactionTemplate masterTemplate(){
        return new TransactionTemplate(masterTransactionManager());
    }

}


package com.test.mybatis.config;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;

/**
 * @author清梦
 * @site www.xiaomage.com
 * @company xxx公司
 * @create 2023-11-11 21:14
 */
@Configuration
@MapperScan(basePackages = "com.test.mybatis.mapper.school",sqlSessionFactoryRef = "schoolSqlSessionFactory")
public class SchoolDataSourceConfig {

    @Bean
    @ConfigurationProperties("spring.datasource.school")
    public DataSource schoolDatasource(){
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }

    @Bean
    public SqlSessionFactory schoolSqlSessionFactory()throws Exception{
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(schoolDatasource());
        return bean.getObject();
    }

    @Bean
    public DataSourceTransactionManager schoolTransactionManager(){
        DataSourceTransactionManager manager = new DataSourceTransactionManager();
        manager.setDataSource(schoolDatasource());
        return manager;
    }

    @Bean
    public TransactionTemplate schoolTemplate(){
        return new TransactionTemplate(schoolTransactionManager());
    }

}


mapper

package com.test.mybatis.mapper.school;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.test.mybatis.domain.Student;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

/**
 * @author清梦
 * @site www.xiaomage.com
 * @company xxx公司
 * @create 2023-11-12 17:11
 */
@Mapper
@Repository
public interface StudentMapper extends BaseMapper<Student> {
}


package com.test.mybatis.mapper.test;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.test.mybatis.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

/**
 * @author清梦
 * @site www.xiaomage.com
 * @company xxx公司
 * @create 2023-11-12 17:11
 */
@Mapper
@Repository
public interface UserMapper extends BaseMapper<User> {
    @Select("select phone from user where card_id = #{cardId}")
    String selectPhoneByCardId(String cardId);
}


service

package com.test.mybatis.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.test.mybatis.domain.Student;
import org.springframework.stereotype.Service;

import java.util.List;

/**
*
*/
@Service
public interface StudentService extends IService<Student> {

    List<Student> getPhone();
}

package com.test.mybatis.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.test.mybatis.domain.User;

/**
 *
 */
public interface UserService extends IService<User> {

    void registerTiUser(Integer id, String username, String phone, String cardId);
}

实现类

package com.test.mybatis.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.mybatis.domain.Student;
import com.test.mybatis.mapper.school.StudentMapper;
import com.test.mybatis.mapper.test.UserMapper;
import com.test.mybatis.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
*
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper,Student> implements StudentService{

    @Autowired
    private UserMapper userMapper;
    @Override
    public List<Student> getPhone() {
        List<Student> list = list();
        list.stream().filter(student -> StringUtils.isNotBlank(student.getCardId())).forEach(student -> {
            String phone = userMapper.selectPhoneByCardId(student.getCardId());
            student.setPhone(phone);
        });
        return list;
    }

}


package com.test.mybatis.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.mybatis.domain.Student;
import com.test.mybatis.domain.User;
import com.test.mybatis.mapper.school.StudentMapper;
import com.test.mybatis.mapper.test.UserMapper;
import com.test.mybatis.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
*
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public void registerTiUser(Integer id, String username, String phone, String cardId) {
        Student student = studentMapper.selectById(id);
        if (null == student){
            throw new RuntimeException("该学生不存在");
        }
        User user = new User();
        user.setRealName(student.getStudentName());
        BeanUtils.copyProperties(student,user);
        user.setUsername(username);
        user.setPhone(phone);
        user.setCardId(cardId);
        userMapper.insert(user);
    }
}


展开全部

  • 写回答

2条回答 默认 最新

  • 色空大师 2023-11-17 02:53
    关注

    在数据源配置类中修改SqlSessionFactoryBean为MybatisSqlSessionFactoryBean即可解决

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月17日
  • 已采纳回答 11月17日
  • 创建了问题 11月16日

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部