mybatisplus activerecord之mybatisplus entity XXX Not Found TableInfoCache.错误

MybatisPlus entity XXX Not Found TableInfoCache.错误

  • 首先需要当前的实体类继承 Model 并且指定当前的Model中的泛型为当前的实体类对象 其次还需要重写其中的pkVal方法
public class Student extends Model<Student> {

    private int id;
    private String name;
    private String password;
    private Double salary;
    private int age;

    protected Serializable pkVal() {
        return id;
    }

然后最重要的来了 如果是使用的Spring集成的MyBaitsPlus 需要使用ActiveRecord 必须 必须 必须 要先使用Spring 将当前的DAO接口 继承了BaseMapper 这个接口 创建出来,不然就会报错!!!恶心死了!!!!!

代码如图

        StudentMapper sm = ac.getBean("studentMapper", StudentMapper.class);
        Student st = new Student();
        Student student = st.selectById(5);
        System.out.println(student);

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I can help you with configuring the MyBatis-Plus code generator. Here is a step-by-step guide: Step 1: Add MyBatis-Plus dependency First, make sure you have added the MyBatis-Plus dependency to your project's `pom.xml` file if you are using Maven, or `build.gradle` file if you are using Gradle. Here is an example for Maven: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>latest-version</version> </dependency> ``` Replace `latest-version` with the latest version of MyBatis-Plus. Step 2: Configure the Code Generator Next, you need to configure the code generator. MyBatis-Plus provides a built-in code generator called `MyBatisGenerator`. You can configure it in the `mybatis-plus-generator.properties` file. Create a file named `mybatis-plus-generator.properties` in the `src/main/resources` directory and add the following properties: ```properties # DataSource configuration mp.generator.jdbc.driver=com.mysql.jdbc.Driver mp.generator.jdbc.url=jdbc:mysql://localhost:3306/db_name mp.generator.jdbc.username=root mp.generator.jdbc.password=password # Package configuration mp.generator.package.parent=com.example.project mp.generator.package.entity=model mp.generator.package.mapper=mapper mp.generator.package.xml=mapper.xml # Strategy configuration mp.generator.strategy.super-entity-class=com.baomidou.mybatisplus.extension.activerecord.Model mp.generator.strategy.super-mapper-class=com.baomidou.mybatisplus.core.mapper.BaseMapper mp.generator.strategy.super-service-class=com.baomidou.mybatisplus.extension.service.IService mp.generator.strategy.super-service-impl-class=com.baomidou.mybatisplus.extension.service.impl.ServiceImpl # Template configuration mp.generator.template.mapper=templates/mapper.java.vm mp.generator.template.xml=templates/mapper.xml.vm mp.generator.template.entity=templates/entity.java.vm mp.generator.template.service=templates/service.java.vm mp.generator.template.service-impl=templates/serviceImpl.java.vm ``` Make sure to replace the values according to your project's settings and requirements. Step 3: Generate code Once the configuration is done, you can now generate code using the MyBatis-Plus code generator. You can do this by executing the `MyBatisGenerator` class in your project. Here is an example: ```java import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.GlobalConfig; import com.baomidou.mybatisplus.generator.config.DataSourceConfig; import com.baomidou.mybatisplus.generator.config.PackageConfig; import com.baomidou.mybatisplus.generator.config.StrategyConfig; public class CodeGenerator { public static void main(String[] args) { // Step 3: Generate code AutoGenerator mpg = new AutoGenerator(); // Global configuration GlobalConfig gc = new GlobalConfig(); gc.setOutputDir(System.getProperty("user.dir") + "/src/main/java"); gc.setAuthor("Your Name"); gc.setOpen(false); mpg.setGlobalConfig(gc); // DataSource configuration DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/db_name"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("password"); mpg.setDataSource(dsc); // Package configuration PackageConfig pc = new PackageConfig(); pc.setParent("com.example.project"); pc.setEntity("model"); pc.setMapper("mapper"); pc.setXml("mapper.xml"); mpg.setPackageInfo(pc); // Strategy configuration StrategyConfig strategy = new StrategyConfig(); strategy.setInclude("table_name"); // Specify the tables you want to generate code for strategy.setSuperEntityClass("com.baomidou.mybatisplus.extension.activerecord.Model"); strategy.setSuperMapperClass("com.baomidou.mybatisplus.core.mapper.BaseMapper"); strategy.setSuperServiceClass("com.baomidou.mybatisplus.extension.service.IService"); strategy.setSuperServiceImplClass("com.baomidou.mybatisplus.extension.service.impl.ServiceImpl"); mpg.setStrategy(strategy); // Execute code generation mpg.execute(); } } ``` Replace the values accordingly, especially the database connection settings and the table names you want to generate code for. That's it! You have now successfully configured the MyBatis-Plus code generator. When you run the `CodeGenerator` class, it will generate the required files based on the configuration provided.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值