IDEA 通过Mybatis generator简单的创建逆向工程导出dao、mapper等文件
文件结构
注意,这里我将generatorConfig.xml文件放到了src目录下,即:与main在同级目录
配置信息
pom.xml文件内容:主要功能是导入jar包--一般需要3个jar包:
数据库连接jar包 | <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.9</version> </dependency> |
Mybatis包 | <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> |
Mybatis generator core 包 | <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> |
pom.xml 内容汇总 |
<dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.9</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies> |
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> <!-- id是唯一标示,可以自行设定 --> <context id="MyGenerator" targetRuntime="MyBatis3"> <!-- 该标签可以去掉注释 --> <commentGenerator> <!-- 去掉注释 --> <property name="suppressAllComments" value="true"/> <!-- 去掉时间戳 --> <property name="suppressDate" value="true"/> </commentGenerator> <!-- 注意修改内容 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/ssm" userId="root" password="root"> </jdbcConnection> <!-- 类型的转换,点击查看 --> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetPackage: 输入包名 输出路径 targetProject: 输出项目位置 --> <!-- 注意修改内容:targetPackage、 targetProject --> <javaModelGenerator targetPackage="com.hmy.bean" targetProject="src"> <!-- enableSubPackages:是否开启子包名称 是否在包名后面加上scheme名称 --> <property name="enableSubPackages" value="false" /> <!-- 在Set中加入trim 去逗号 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- mapper配置 --> <!-- 注意修改内容:targetPackage、 targetProject --> <sqlMapGenerator targetPackage="com.hmy.mapper" targetProject="src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- java接口 --> <!-- 注意修改内容:targetPackage、 targetProject --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.hmy.mapper" targetProject="src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 数据表 要根据数据库中的表来生成 table标签的其他内容,点击查看 --> <!-- 注意修改内容:将表名修改为自己数据库中表的名字 --> <table tableName="t_user"/> <table tableName="t_salary"/> <!-- <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" > <property name="useActualColumnNames" value="true"/> <generatedKey column="ID" sqlStatement="DB2" identity="true" /> <columnOverride column="DATE_FIELD" property="startDate" /> <ignoreColumn column="FRED" /> <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> </table> --> </context> </generatorConfiguration> |
Test.java 文件内容:
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/generatorConfig.xml"); //需要注意generatorConfig.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);
}
} |
最后就是运行一下Test文件,大功告成!