.1)表丶类丶接口丶mapper.xml四者密切相关,因此,当知道一个的时候 其他三个应该可以自动生成
.2)具体实现步骤
1.导入jar包
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency>
2.编写配置配置文件generator.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> <context id="MybatisGenerator" targetRuntime="mybatis3"> <commentGenerator> <!-- 是否去除自动生成的注释 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 数据库连接信息:驱动类 ,连接地址,用户名,密码--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC" userId="root" password="root"> </jdbcConnection> <!-- 默认值 --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成的实体类的存放位置 --> <javaModelGenerator targetPackage="net.bdqn.hbz.pojo" targetProject="./src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <!-- <property name="enableSubPackages" value="true"/>--> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成的Mapper映射文件(XML)的存放位置 --> <sqlMapGenerator targetPackage="net.bdqn.hbz.mapper" targetProject="./src"> <!-- <property name="enableSubPackages" value="false"/>--> </sqlMapGenerator> <!-- 生成的mapper接口(JAVA文件)的存放位置 --> <javaClientGenerator targetPackage="net.bdqn.hbz.mapper" type="XMLMAPPER" targetProject="./src"> <!-- <property name="enableSubPackages" value="false"/>--> </javaClientGenerator> <!-- 指定数据库表 --> <table tableName="student"/> <table tableName="studentcart"/> <table tableName="studentclass"/> </context> </generatorConfiguration>
3.执行类
package net.bdqn.hbz.test; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.InvalidConfigurationException; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String [] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; // 指定配置文件 File configFile = new File("E:\workJavaEE\Mybatis\work\MyGenerator\target\classes\net\bdqn\hbz\test\generator.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGeneratorgenerate(null); } }