mybatis逆向工程生成java代码和xml配置

mybatis官方提供了一个逆向工程包,可以针对数据库表自动生成mybatis执行所需要的Pojo、Mapper xml文件、Mapper 接口文件。 

mybatis-generator有很多种用法:命令行、eclipse/IDEA、Maven插件,其使用原理完全一样。 
无论哪种方式,首先要准备两个组件包:mybatis-generator-core-1.X.X.jar 和MySQL-connector-Java-5.X.XX.jar (点击下载两个jar包

命令行方式

从这个入手,因为最方便。 
1、新建任意目录(D:\A-TWM\Mybatis),把两个组件拷入目录。 
这里写图片描述

2、新建配置文件,命名:config.xml 
补充:下载好的jar包里面有帮助文档,打开后里面有配置文件的模板。 
config.xml内容:

pojo 的映射路径为当前正在做的项目的路径下面(也可以为任意路径下面,只不过写在其他路径的时候,在拷贝到当前项目中需要修改每一个pojo 的 package 的名称)

<?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:\A-TWM\Mybatis\mysql-connector-java-5.1.26-bin.jar" />

    <context id="sqlGenerate" targetRuntime="MyBatis3">
        <!-- 是否去除自动生成的注释 true:是 : false:否 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!-- 数据库链接URL、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/tangwenmingdb?characterEncoding=utf8"
            userId="root" password="root">
        </jdbcConnection>

         <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer;
         为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 生成Pojo包名和位置 -->
        <javaModelGenerator targetPackage="twm.mybatisdemo.pojo"
            targetProject="D:\A-TWM\Mybatis\src">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true" />
            <!-- 清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- 生成Mapper映射XML文件位置 -->
        <sqlMapGenerator targetPackage="twm.mybatisdemo.mapper"
            targetProject="D:\A-TWM\Mybatis\src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 生成Mapper接口文件位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="twm.mybatisdemo.mapper" targetProject="D:\A-TWM\Mybatis\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 要生成哪些表(更改tableName和domainObjectName就可以) -->
        <!-- tableName:要生成的表名
        domainObjectName:生成后的实例名
        enableCountByExample:Count语句中加入where条件查询,默认为true开启
        enableUpdateByExample:Update语句中加入where条件查询,默认为true开启
        enableDeleteByExample:Delete语句中加入where条件查询,默认为true开启
        enableSelectByExample:Select多条语句中加入where条件查询,默认为true开启
        selectByExampleQueryId:Select单个对象语句中加入where条件查询,默认为true开启
        -->
        <table tableName="user" domainObjectName="User"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
        <table tableName="category" />
        <table tableName="order"/>
        <table tableName="product"/>
        <table tableName="order_detail"/>
    </context>
</generatorConfiguration>

如果table里边不配置property,默认将所有字段逆向生成为类属性。 

如果有些字段并不想生成为类属性,可以用ignoreColumn标签:

<ignoreColumn column="FRED" />//忽略字段 

还可以指定逆向生成时,字段到属性的转换对应关系

<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />//无论字段是什么类型,生成的类属性都是varchar。 

3、通过cmd打开命令窗口 
运行:java -jar mybatis-generator-core-1.3.2.jar -configfile config.xml -overwrite 
出现MyBatis Generator finished successfully.表示运行成功,将指定生成位置(这里是src)的源码拷入工作项目中即可。 
这里写图片描述

Eclipse方式

1、任意新建一个 java project、将组件和将配置文件config.xml放到对应的目录,其中 Main.java  就是要运行的主程序

2、在main函数中写代码运行

package Main;

import java.io.File;
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.internal.DefaultShellCallback;

public class main {

	public static void main(String[] args) throws Exception{
		List<String> warnings = new ArrayList<String>();  
	    boolean overwrite = true;  
	    //指向逆向工程配置文件,只需要把下面这个文件改为你自己写的配置文件即可
	    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);
	}
}

3、以application的方式运行就可以了

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
public class GeneratorSqlmap { public void generator() throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; //指定 逆向工程配置文件 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); } public static void main(String[] args) throws Exception { try { GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap(); generatorSqlmap.generator(); } catch (Exception e) { e.printStackTrace(); } } } <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成PO类的位置 --> <javaModelGenerator targetPackage="cn.itcast.ssm.po" targetProject=".\\src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:mapper映射文件生成的位置 --> <sqlMapGenerator targetPackage="cn.itcast.ssm.mapper" targetProject=".\\src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:mapper接口生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.itcast.ssm.mapper" targetProject=".\\src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定数据库表 --> <table schema="" tableName="user"></table> <table schema="" tableName="orders"></table> <table schema="" tableName="items"></table> <table schema="" tableName="orderdetail"></table>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Micrle_007

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值