使用 Mybatis 时,为了提高效率,会用到三个工具
- MyBatis-Generator
- MyBatis-Plugin
- MyBatis-PageHelper
MyBatis-Generator
用来生成 pojo、dao 和 mapper 文件的工具
官方文档
http://www.mybatis.org/generator/quickstart.html
配置
- 配置数据连接信息
1 | <!-- 数据库驱动、URL、用户名、密码 --> |
- 生成模型的包名和位置
1 | <!-- 生成模型的包名和位置 --> |
- 生成的映射文件包名和位置
1 | <!-- 生成的映射文件包名和位置 --> |
- 生成DAO的包名和位置
1 | <!-- 生成DAO的包名和位置 --> |
- 表与JavaBean
只需要更改 tableName
和 domainObjectName
就可以
对于普通的表
1 | <table tableName="mmall_shipping" domainObjectName="Shipping" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> |
对于有些字段类型为 TEXT
的表,需要使用 columnOverride
标签重新指定一下,否则不会为该字段生成 JavaBean 的属性
1 | <table tableName="mmall_product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" > |
使用
在控制台中输入
1 | java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite |
一个完整的配置文件例子,可以在 generatorConfig.xml 看到
MyBatis-Plugin
下载一个 IDEA 的 plugin 安装即可,如下图

但是因为 Mybatis-Plugin 现在收费了,可以下载 “Free Mybatis Plugin”

打开一个 DAO 文件,就可以看到左边出现了很多箭头

点击箭头,就可以跳转到相应的 mapper 的 xml 配置

MyBatis-PageHelper
官网:https://github.com/pagehelper/Mybatis-PageHelper
现在最新版本是 5.1.7 了,配置和 4.x 时不太一样
1 | <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> |