基于mybatis逆向工程的示例分析
更新:HHH   时间:2023-1-7


这篇文章给大家分享的是有关基于mybatis逆向工程的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

使用mybatis生成逆向工程的详细步骤,我个人感觉这个是最简单的一个了,虽然网上有很多种的方法来生成逆向工程,可是这个方法最简单。在这里我是使用maven搭建的环境,但是在正常的环境下也是一样的。

步骤:

1、创建一个genreatorConfig.xml文件,这个文件的名字可以任意。我创建的时候是将它放在了src/main/resources下,这个文件的内容并不需要去记,只需要去网上找就可以了。我们要做的只是对配置文件当中的一些部分做修改,修改成自己的数据就可以了。

<?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>
<!--数据库驱动,最好不要有中文字符,不然会找不到-->
<classPathEntry location="D:\MysqlJdbcconnector/mysql-connector-java-5.1.41-bin.jar" />


<context id="DB2Tables" targetRuntime="MyBatis3">

<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/pet_hospital" userId="root" password="112233">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="com.qianfeng.bean" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="com.qianfeng.bean" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成DaoMapper类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.qiafeng.dao" targetProject="src/main/java">

<property name="enableSubPackages" value="true"/>

</javaClientGenerator>
<!--生成对应表及类名,需要记住的一点是逆向工程无法生成关联关系,只能生成单表操作-->
<table tableName="owners" 
domainObjectName="Owners" 
></table>
<table tableName="pets" 
domainObjectName="Pets" 
></table>
<table tableName="types" 
domainObjectName="Types" 
></table>
<table tableName="employee" 
domainObjectName="Employee" 
></table>
<table tableName="specialties" 
domainObjectName="Specialties" 
></table>
<table tableName="vets" 
domainObjectName="Vets" 
></table>
<table tableName="visits" 
domainObjectName="Visits" 
></table>
<table tableName="vet_specialties" 
domainObjectName="VetSpecialties" 
></table>

</context>
</generatorConfiguration>

2、导入相应的jar包,mybatis-generator-core这个包和mysql-connector-java这个包

3、创建一个测试类,然后运行下面代码就可以了。

public static void generator() throws Exception{
  List<String> warnings = new ArrayList<String>();
  boolean overwrite = true;
  //项目根路径不要有中文,我的有中文,所以使用绝对路径
  File configFile = new File("src/main/resources/genreatorConfig.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);
 }
 public static void main(String[] args) {
  try {
   generator();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

感谢各位的阅读!关于“基于mybatis逆向工程的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

返回编程语言教程...