springboot + mybatis + mybatis-generator + tk.mybatis

springboot在配置mybatis-generator时遇到的坑,我用的是eclipse,以eclipse为例

1. eclipse 先安装下mybatis-generator插件

Help -> Install New Software.... -> add ->Name 可以自定义,Location 填写https://dl.bintray.com/mybatis/mybatis-generator/

然后next 到最后finish,至此mybatis-generator插件 安装完毕。

2. springboot 的pom文件中添加

  <dependencies>

<dependency>

            <groupId>org.mybatis.spring.boot</groupId>

            <artifactId>mybatis-spring-boot-starter</artifactId>

            <version>1.3.2</version>

        </dependency>

        <dependency>

            <groupId>tk.mybatis</groupId>

            <artifactId>mapper-spring-boot-starter</artifactId>

            <version>2.0.4</version>

        </dependency>

<dependency>

       <groupId>tk.mybatis</groupId>

       <artifactId>mapper</artifactId>

       <version>3.4.6</version>

   </dependency>

  </dependencies>

<plungins>

<plugin>

                <groupId>org.mybatis.generator</groupId>

                <artifactId>mybatis-generator-maven-plugin</artifactId>

                <version>1.3.5</version>

                <configuration>

                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>

                    <overwrite>true</overwrite>

                    <verbose>true</verbose>

                </configuration>

                <dependencies>

                <dependency>

            <groupId>com.oracle</groupId>

            <artifactId>ojdbc6</artifactId>

            <version>12.1.0.2.0</version>

        </dependency>

                <!-- MyBatis Generator及工具 -->

                <dependency>

<groupId>org.mybatis.generator</groupId>

<artifactId>mybatis-generator-core</artifactId>

<version>1.3.5</version>

</dependency>

                    <dependency>

                        <groupId>tk.mybatis</groupId>

                        <artifactId>mapper</artifactId>

                        <version>3.4.6</version>

                    </dependency>

                </dependencies>

            </plugin>

</plungins>

3. 配置generatorConfig.xml

配置详情自行百度。

4. 注意

在配置的过程中遇到各种奇葩的问题,搜索之后并没有提示的很明显,所以耗费了很多的时间。这些比较坑的问题见下面:

1)Cannot instantiate object of type tk.mybatis.mapper.generator.MapperPlugin

这是提示MapperPlugin 不能实例化,一定是没有maven到包,找不到类。找了一圈才发现tk.mybatis的依赖不能只在<plugin>中配置,还得在外部<dependencies>中添加依赖。

2)以上步骤都完成后在generatorConfig.xml文件上右击 run as -> 

会报如下错误:

MyBatis Generator Started...

  Buildfile: 

  WARNING: Project src does not exist

  WARNING: Project src does not exist

  WARNING: Project src does not exist

  BUILD SUCCESSFUL

MyBatis Generator Finished

src目录找不到 但是我配置就是targetProject="src/main/java",没问题啊,但就是报错。尝试了在src前加应用的名称,再执行就ok了。这就比较坑了,很难发现。另外,targetProject一定要写相对路径,不要写绝对路径。

3)生成pojo类的同时生成Example类,这是tk.mybatis的功能。目的就是动态生成sql,做简单的curd。如何生成Example类?generatorConfig.xml中 <context>标签targetRuntime="MyBatis3" 或者targetRuntime不写,因为targetRuntime默认值是MyBatis3。

如何使用Example类?(百度一下)

后面会发现会产生很多的Example类,在tk.mybatis还有另一种方式的情况下,这中方案就显得很多余了。

另一种动态sql方式?targetRuntime="MyBatis3Simple",然后直接使用tk.mybatis的Example类。例如:Example ex = new Example([pojo类名称].class);

 

over.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值