首页 > 编程 > Java > 正文

基于mybatis逆向工程的使用步骤详解

2019-11-26 10:59:05
字体:
来源:转载
供稿:网友

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

步骤:

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

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfigurationPUBLIC "-//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逆向工程的使用步骤详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表