您的位置:首页 > 编程语言 > Java开发

Mybatis逆向工程generator配置使用

2018-03-19 16:12 381 查看
本文讲述如何在Maven工程下使用Mybatis逆向工程(点击进入generator官方文档)

首先我们要在pom文件中引用依赖包

<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>


在工程下新建一个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="DB2Tables" targetRuntime="MyBatis3">
<!-- 去除实体类自动生成的注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- jdbc连接数据库 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/medicalplatform" userId="root"
password="password">
</jdbcConnection>

<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>

<!-- 指定实体类存放目录 -->
<javaModelGenerator targetPackage="com.doc.bean"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 指定映射文件生成位置 -->
<sqlMapGenerator targetPackage="mapp" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 指定接口生成位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.doc.mapp" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- tableName表示数据库的表名,domainObjectNamege表示要创建的实体类名 -->
<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 ta
b640
bleName="patientprescription" domainObjectName="Patientprescription"></table>
<table tableName="preregistration" domainObjectName="Preregistration"></table>
<table tableName="report" domainObjectName="Report"></table>

</context>
</generatorConfiguration>


最后在新建java文件 执行后自动生成所需文件。之后刷新就可以看到自动生成的文件

package com.test.run;

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 Sqlcore {

public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//file("value")value:MyBatis generator配置文件的配置路径
File configFile = new File("core.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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息