介绍
众说周知,iBatis是“半自动”ORM。Hibernate可以通过对象关系模型实现对数据库的操作,但是MyBatis只能通过手写SQL来管理对象关系和数据库字段映射。
MyBatis Generator(MBG)是MyBatis的子项目之一,用于自动化创建简单的CURD组件,减少重复性编码工作。
具体介绍http://www.mybatis.org/generator/
使用方式:
MBG支持种5工作方式,这里介绍两种:
1.With Maven
2.With Java
一、这里介绍Maven插件的使用方法
引入Maven插件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
编辑generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--导入属性配置-->
<!--<properties resource="generatorConfig.properties"/>-->
<!--指定特定数据库的jdbc驱动jar包的位置-->
<classPathEntry
location="C:\Users\moun\.m2\repository\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar"/>
<context id="default" targetRuntime="MyBatis3" defaultModelType="flat">
<property name="autoDelimitKeywords" value="true"/>
<!-- optional,旨在创建class时,对注释进行控制 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--jdbc的数据库连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/blog_db?useUnicode=true&characterEncoding=utf-8&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"
userId="root"
password="root" />
<!--客户端接口生成器,生成针对Model和Mapper的带有iBatis注解的接口文件-->
<javaClientGenerator targetPackage="antnest.blog.dao.database" targetProject="src/main/java" type="XMLMAPPER" />
<!--Model模型生成器,用来生成含有主键的实体类以及用于多条件查询Example类-->
<javaModelGenerator targetPackage="antnest.blog.model.database" targetProject="src/main/java" />
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources" />
<!--数据库中表生成类的信息-->
<table tableName="user"
enableCountByExample="true" enableUpdateByExample="true"
enableDeleteByExample="true" enableSelectByExample="true" />
<table tableName="config"
enableCountByExample="true" enableUpdateByExample="true"
enableDeleteByExample="true" enableSelectByExample="true" />
<!--<table tableName="blog_db.%"
enableCountByExample="true" enableUpdateByExample="true"
enableDeleteByExample="true" enableSelectByExample="true" />-->
</context>
</generatorConfiguration>
运行插件并生成代码
maven插件运行方法:
该项目 右键>>>run as>>>maven build…>>>Goals项输入:mybatis-generator:generate
刷新该项目即可!
二、Java运行方法
引入相关jia包
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<!-- 该jar包可以省略 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
</dependencies>
编辑配置文件mbg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 指定驱动包 -->
<!-- 直接从Maven Dependencies中找到mysql-connector-java的jar包复制出location,如上图 -->
<classPathEntry location="C:\Users\MSI\.m2\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar" />
<!-- 指定数据库连接字符串 -->
<context id="DB2Tables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ov"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 指定实体类 -->
<!-- targetPackage="cn.xd.ovls.entity" targetProject=".\src\main\java"根据工程自己指定 -->
<javaModelGenerator targetPackage="cn.xd.ov.entity" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 指定sql定义文件 -->
<!-- targetPackage="sql" targetProject=".\src\main\resources"根据工程自己指定 -->
<sqlMapGenerator targetPackage="sql" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 指定Mapper映射器接口type='XMLMAPPER' XML文件定义SQL; type='ANNOTATEDMAPPER' 注解SQL-->
<!-- type类型参考[http://www.mybatis.org/generator/configreference/javaClientGenerator.html](http://www.mybatis.org/generator/configreference/javaClientGenerator.html) -->
<!-- targetPackage="cn.xd.ov.dao" targetProject=".\src\main\java"根据工程自己指定 -->
<javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="cn.xd.ov.dao" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 指定数据表 -->
一、<!-- %可以实现库里的多表生成 -->
<table tableName="%"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
二、指定单表
<table tableName="表名" domainObjectName="类名"></table>
eg:
<table tableName="USER" domainObjectName="User"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"></table>
</context>
</generatorConfiguration>
编写Java启动类RunMyBatisGenerator
编写Java启动类参考http://www.mybatis.org/generator/running/runningWithJava.html
public class RunMyBatisGenerator {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mbg.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
运行Java类生成实体类
mybatis中的mapper接口文件以及example类的实例函数以及详解
参考:
https://blog.csdn.net/paincupid/article/details/50921110
https://blog.csdn.net/biandous/article/details/65630783
https://blog.csdn.net/luluyo/article/details/81708833
https://blog.csdn.net/papima/article/details/78577748
https://blog.csdn.net/yangshangwei/article/details/80140693
原文:https://blog.csdn.net/acquaintanceship/article/details/78512783