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

本文介绍如何使用 MyBatis Generator 自动生成 POJO、DAO 和 Mapper 文件,提供了一个具体的配置示例,包括配置文件详解及 Java 类实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
    }

}

以上代码是测试可用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值