mybatis-generator生成相对应的po、dao以及mapper

1、下载mybatis-generator相应的jar包文件,可以进入http://search.maven.org/#search找到不同版本的jar包;

2、进入http://mybatis.github.io/generator/configreference/xmlconfig.html官方网站查看官方文档,选择你相应的方式来生成po、dao、mapper文件,本来选择的是配置文件+java文件来生成一系列文件。下面给出相对应的文件代码:

mybatis-genertor.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="testTables" targetRuntime="MyBatis3">  
            <commentGenerator>  
                <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
                <property name="suppressAllComments" value="true" />  
            </commentGenerator>  
            <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->  
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
                connectionURL="jdbc:mysql://localhost:3306/sktask" userId="root"  
                password="root">  
            </jdbcConnection>  
            <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"  
                connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"   
                userId="yycg"  
                password="yycg">  
            </jdbcConnection> -->  
      
            <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和   
                NUMERIC 类型解析为java.math.BigDecimal -->  
            <javaTypeResolver>  
                <property name="forceBigDecimals" value="false" />  
            </javaTypeResolver>  
      
            <!-- targetProject:生成PO类的位置 -->  
            <javaModelGenerator targetPackage="com.sk.main.entity"  
                targetProject=".\src">  
                <!-- enableSubPackages:是否让schema作为包的后缀 -->  
                <property name="enableSubPackages" value="false" />  
                <!-- 从数据库返回的值被清理前后的空格 -->  
                <property name="trimStrings" value="true" />  
            </javaModelGenerator>  
            <!-- targetProject:mapper映射文件生成的位置 -->  
            <sqlMapGenerator targetPackage="com.sk.main.entity.mapper"   
                targetProject=".\src">  
                <!-- enableSubPackages:是否让schema作为包的后缀 -->  
                <property name="enableSubPackages" value="false" />  
            </sqlMapGenerator>  
            <!-- targetPackage:mapper接口生成的位置 -->  
            <javaClientGenerator type="XMLMAPPER"  
                targetPackage="com.sk.main.dao"   
                targetProject=".\src">  
                <!-- enableSubPackages:是否让schema作为包的后缀 -->  
                <property name="enableSubPackages" value="false" />  
            </javaClientGenerator>  
            <!-- 指定数据库表 -->  
            <table tableName="t_sys_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>  
            <table tableName="t_sys_role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
             <table tableName="t_b_permission" domainObjectName="Permission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
            <!-- 有些表的字段需要指定java类型  
             <table schema="" tableName="">  
                <columnOverride column="" javaType="" />  
            </table> -->  
        </context>  
    </generatorConfiguration> 

MybatisGenerator.java

package com.sk.util.mybatis.generator;

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.Context;
import org.mybatis.generator.config.ModelType;
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;
/**
 * <p>使用mybatisGenerator生成器生成pojo,dao已经mapper,可以修改mybatis-generator.xml中的属性来修改生成的数据</p>
 * @author 林添
 * @since 2015-07-29
 * @version 1.0
 */
public class MybatisGenerator {

    public static void main(String[] args)  {
          try {
              MybatisGenerator my=new MybatisGenerator();
            my.generator();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (XMLParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public  void generator() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException{
        List<String> warnings = new ArrayList<String>();
           boolean overwrite = true;
           System.out.println(this.getClass().getResource("/").getPath());
           File configFile = new File(this.getClass().getResource("/").getPath()+"mybatis-generator.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);
    }

}

以上代码是测试可用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis-generator是一个自动生成MyBatis代码的工具,可以快速生成MapperDao、Entity等代码文件,可大大提高开发效率。如果需要多个项目使用相同的生成器配置,我们可以将该生成器打包成jar包,以供其他项目引用。 首先,在生成器的pom.xml文件中添加以下代码,将生成器打包成jar包: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.1</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>org.mybatis.generator.api.ShellRunner</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> ``` 需要注意的是,这里需要指定MainClass为org.mybatis.generator.api.ShellRunner。 然后,执行以下Maven命令打包即可: ```bash mvn clean package ``` 生成的jar包位于target文件夹下。 接下来,我们可以将生成的jar包引入到其他项目中,在其他项目的pom.xml文件中添加以下依赖: ```xml <dependencies> <dependency> <groupId>com.company</groupId> <artifactId>mybatis-generator</artifactId> <version>1.0-SNAPSHOT</version> <scope>provided</scope> </dependency> </dependencies> ``` 其中,groupId和artifactId需要根据实际项目名称进行修改,version则要对生成的jar包版本号。 最后,在其他项目中,可以通过在命令行中执行以下命令运行自定义生成器: ```bash java -jar mybatis-generator.jar -configfile generatorConfig.xml -overwrite ``` 其中,generatorConfig.xml为自定义的生成器配置文件,-overwrite表示覆盖已存在的文件。 通过以上步骤,我们就可以自定义Mybatis-generator生成器并将其打包成jar包,以供其他项目引用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值