mybatis generator代码生成主要分为三部分的配置:
第一步:去maven中央仓库导入mybatis generator的依赖关系;
第二步:去mybatis generator文档找到generatorConfig.xml配置文件的配置代码;
第三步:去mybatis generator文档找到java程序部分的代码;
第一步代码:
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
第二步代码:
<?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="DB2Tables" targetRuntime="MyBatis3">
<!-- 这里配置的是数据库的连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/medicalplatform"
userId="root"
password="123456">
</jdbcConnection>
<!-- 这里不用管 -->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 这里配置的是你的实体类的位置,我这里配置在src\main\java项目文件夹下的com.zhiyuan.bean包之下 -->
<javaModelGenerator targetPackage="com.zhiyuan.bean" targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 这里配置的是映像文件的位置,我这里配置在src\main\resources下的config文件夹下 -->
<sqlMapGenerator targetPackage="config" targetProject="src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 这里配置的是接口的位置,我配置在src\main\java下的com.zhiyuan.dao包下 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.zhiyuan.dao" targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- table标签中tableName是数据库的表名,domainObjectName的配置是你要生成的实体类文件的名字,所以我用的大写 -->
<table tableName="charge" domainObjectName="Charge" ></table>
<table tableName="cure" domainObjectName="Cure" ></table>
<table tableName="department" domainObjectName="Department" ></table>
<table tableName="doctor" domainObjectName="Doctor" ></table>
<table tableName="drug" domainObjectName="Drug" ></table>
<table tableName="examine" domainObjectName="Examine" ></table>
<table tableName="followup" domainObjectName="Followup" ></table>
<table tableName="medicalcase" domainObjectName="Medicalcase" ></table>
<table tableName="patientcure" domainObjectName="Patientcure" ></table>
<table tableName="patientexamine" domainObjectName="Patientexamine" ></table>
<table tableName="patientprescription" domainObjectName="Patientprescription" ></table>
<table tableName="preregistration" domainObjectName="Preregistration" ></table>
<table tableName="report" domainObjectName="Report" ></table>
</context>
</generatorConfiguration>
第三步代码:
package medicalplatform;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
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;
public class Generators {
public static void main(String[] args) throws Exception, Exception{
// TODO Auto-generated method stub
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//这里的generatorConfig.xml是第二步配置的xml文件名
File configFile = new File("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);
}
}
完成这三步,然后运行java文件就能自动生成接口、实体类、映像文件。