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

2024-09-02 14:48

本文主要是介绍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);
    }

}

以上代码是测试可用。

这篇关于mybatis-generator生成相对应的po、dao以及mapper的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1130258

相关文章

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

MyBatis中$与#的区别解析

《MyBatis中$与#的区别解析》文章浏览阅读314次,点赞4次,收藏6次。MyBatis使用#{}作为参数占位符时,会创建预处理语句(PreparedStatement),并将参数值作为预处理语句... 目录一、介绍二、sql注入风险实例一、介绍#(井号):MyBATis使用#{}作为参数占位符时,会

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

mapstruct中的@Mapper注解的基本用法

《mapstruct中的@Mapper注解的基本用法》在MapStruct中,@Mapper注解是核心注解之一,用于标记一个接口或抽象类为MapStruct的映射器(Mapper),本文给大家介绍ma... 目录1. 基本用法2. 常用属性3. 高级用法4. 注意事项5. 总结6. 编译异常处理在MapSt

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

Mybatis的分页实现方式

《Mybatis的分页实现方式》MyBatis的分页实现方式主要有以下几种,每种方式适用于不同的场景,且在性能、灵活性和代码侵入性上有所差异,对Mybatis的分页实现方式感兴趣的朋友一起看看吧... 目录​1. 原生 SQL 分页(物理分页)​​2. RowBounds 分页(逻辑分页)​​3. Page

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Mybatis Plus Join使用方法示例详解

《MybatisPlusJoin使用方法示例详解》:本文主要介绍MybatisPlusJoin使用方法示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录1、pom文件2、yaml配置文件3、分页插件4、示例代码:5、测试代码6、和PageHelper结合6

MyBatis设计SQL返回布尔值(Boolean)的常见方法

《MyBatis设计SQL返回布尔值(Boolean)的常见方法》这篇文章主要为大家详细介绍了MyBatis设计SQL返回布尔值(Boolean)的几种常见方法,文中的示例代码讲解详细,感兴趣的小伙伴... 目录方案一:使用COUNT查询存在性(推荐)方案二:条件表达式直接返回布尔方案三:存在性检查(EXI