mybatis generator配置,Mybatis自动生成文件配置,Mybatis自动生成实体Bean配置

==============================

蕃薯耀 2018年3月14日

http://www.cnblogs.com/fanshuyao/

一、使用Maven方式引入Mybatis依赖Jar包(版本号自己改或定义)

  1. <properties>
  2. <spring.version>4.3.13.RELEASE</spring.version>
  3. <mybatis.version>3.4.6</mybatis.version>
  4. <mybatis.spring.version>1.3.1</mybatis.spring.version>
  5. <mybatis.generator.version>1.3.6</mybatis.generator.version>
  6. <junit.version>4.12</junit.version>
  7. </properties>

 

  1. <!-- mybatis-->
  2. <dependency>
  3. <groupId>org.mybatis</groupId>
  4. <artifactId>mybatis</artifactId>
  5. <version>${mybatis.version}</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.mybatis</groupId>
  9. <artifactId>mybatis-spring</artifactId>
  10. <version>${mybatis.spring.version}</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.mybatis.generator</groupId>
  14. <artifactId>mybatis-generator-core</artifactId>
  15. <version>${mybatis.generator.version}</version>
  16. <scope>provided</scope>
  17. </dependency>

二、加入文件生成xml配置文件

直接在项目文件夹下建立xml配置文件:mybatis-generator.xml,内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <!-- 配置文件信息见:http://www.mybatis.org/generator/configreference/xmlconfig.html -->
  6. <generatorConfiguration>
  7. <!-- 配置数据库信息 -->
  8. <context id="mysql" targetRuntime="MyBatis3">
  9. <commentGenerator>
  10. <!-- 设置不生成注释  suppressAllComments :When the property is true, no comments will be added to any generated element.-->
  11. <property name="suppressAllComments" value="true" />
  12. </commentGenerator>
  13. <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  14. connectionURL="jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"
  15. userId="root"
  16. password="root">
  17. </jdbcConnection>
  18. <javaTypeResolver>
  19. <property name="forceBigDecimals" value="false" />
  20. </javaTypeResolver>
  21. <!-- 指定JavaBean生成的位置,.\src表示直接在项目的src目录下生成 -->
  22. <javaModelGenerator targetPackage="com.lqy.ssm.bean" targetProject=".\src\main\java">
  23. <property name="enableSubPackages" value="true" />
  24. <property name="trimStrings" value="true" />
  25. </javaModelGenerator>
  26. <!-- 指定sql映射文件生成的位置 -->
  27. <sqlMapGenerator targetPackage="mapper"  targetProject=".\src\main\resources">
  28. <property name="enableSubPackages" value="true" />
  29. </sqlMapGenerator>
  30. <!-- 指定dao接口生成的位置 -->
  31. <javaClientGenerator type="XMLMAPPER" targetPackage="com.lqy.ssm.dao"  targetProject=".\src\main\java">
  32. <property name="enableSubPackages" value="true" />
  33. </javaClientGenerator>
  34. <!-- 指定每个table表生成的策略 -->
  35. <!-- domainObjectName可省略 -->
  36. <table tableName="user" domainObjectName="User"
  37. enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false"></table>
  38. <table tableName="user_ext"
  39. enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false"></table>
  40. </context>
  41. </generatorConfiguration>

把数据库连接信息修改为自己的数据库连接。

三、创建生成文件的代码类:MybatisGenerator.java,内容如下:

官网代码见:http://www.mybatis.org/generator/running/runningWithJava.html

  1. import java.io.File;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.mybatis.generator.api.MyBatisGenerator;
  5. import org.mybatis.generator.config.Configuration;
  6. import org.mybatis.generator.config.xml.ConfigurationParser;
  7. import org.mybatis.generator.internal.DefaultShellCallback;
  8. public class MybatisGenerator {
  9. public static void main(String[] args) throws Exception {
  10. List<String> warnings = new ArrayList<String>();
  11. boolean overwrite = true;
  12. File configFile = new File("mybatis-generator.xml");
  13. ConfigurationParser cp = new ConfigurationParser(warnings);
  14. Configuration config = cp.parseConfiguration(configFile);
  15. DefaultShellCallback callback = new DefaultShellCallback(overwrite);
  16. MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
  17. myBatisGenerator.generate(null);
  18. }
  19. }

其中File configFile = new File("mybatis-generator.xml");就是读取自己的配置文件,如果命名不一致,需要改为一致。

四、最后运行MybatisGenerator方法生成文件,然后F5刷新项目,文件就直接生成。

==============================

蕃薯耀 2018年3月14日

http://www.cnblogs.com/fanshuyao/

mybatis generator配置,Mybatis自动生成文件配置,Mybatis自动生成实体Bean配置的更多相关文章

  1. eclipse中mybatis generator插件的安装与使用,实现自动生成代码

    git地址:https://github.com/mybatis/generator 下载后解压: 选择任意一个版本的jar放到eclipse的features目录下即可 选择任意一个版本的jar放到 ...

  2. SpringBoot(十一):springboot2.0.2下配置mybatis generator环境,并自定义字段/getter/settetr注释

    Mybatis Generator是供开发者在mybatis开发时,快速构建mapper xml,mapper类,model类的一个插件工具.它相对来说对开发者是有很大的帮助的,但是它也有不足之处,比 ...

  3. 使用MyBatis Generator 1.3.7自动生成代码

    MyBatis Generator是一款mybatis自动代码生成工具,可以通过配置后自动生成文件. MyBatis Generator有几种方法可以生成代码,下面是其中一种.  一.官网下载 MyB ...

  4. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

  5. IDEA Maven Mybatis generator 自动生成代码

    IDEA Maven Mybatis generator 自动生成代码 一.安装配置maven以及在Idea中配置maven 安装过程步骤可以看上面的博文,里面介绍得很详细. 二.建数据表 DROP ...

  6. Eclipse 使用mybatis generator插件自动生成代码

    Eclipse 使用mybatis generator插件自动生成代码 标签: mybatis 2016-12-07 15:10 5247人阅读 评论(0) 收藏 举报 .embody{ paddin ...

  7. IDEA Maven Mybatis generator 自动生成代码(实例讲解)(转)

    IDEA Maven Mybatis generator 自动生成代码(实例讲解) MyBatis Generator • 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的 ...

  8. IDEA使用mybatis generator自动生成代码

    主要就三步: 1.pom 文件中引入jar包并配置 build 属性 <dependencies> <!-- 自动生产mapper Begin! --> <depende ...

  9. spring boot集成mybatis(3) - mybatis generator 配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

随机推荐

  1. MongoDB 比较适用哪些业务场景?

      在云栖社区上发起了一个 MongoDB 使用场景及运维管理问题交流探讨的技术话题,有近5000人关注了该话题讨论,这里就 MongoDB 的使用场景做个简单的总结,谈谈什么场景该用 MongoDB ...

  2. webpack4--热更新

    所谓热更新,就是在浏览器能同步刷新你的代码.webpack 热更新依赖 webpack-dev-server.具体实现步骤如下: 1.局部安装依赖 webpack-dev-server npm ins ...

  3. label文字居中

    height: height,//"20px"                "line-height": height, autosize:true, &qu ...

  4. JavaScript prototype背后的工作原理

    首先从一个函数说起 function fn1(name, age) { this.name = name; this.age = age; this.say = function() { alert( ...

  5. ubuntu安装mxnet GPU版本

    安装mxnet GPUsudo pip install mxnet-cu80==1.1.0 推荐pip安装mxnet,土豪gpu版本: pip install mxnet-cu90==1.0.0 豪华 ...

  6. Android 耳机插入过程分析

    Android 耳机插入过程分析 参考链接: https://www.jianshu.com/p/d82a8dabb3e7 初始化: 10-26 07:40:43.932 1414 1414 I Sy ...

  7. 第三百八十三节,Django+Xadmin打造上线标准的在线教育平台—第三方模块django-simple-captcha验证码

    第三百八十三节,Django+Xadmin打造上线标准的在线教育平台—第三方模块django-simple-captcha验证码 下载地址:https://github.com/mbi/django- ...

  8. 性能优化系列三:JVM优化

    一.几个基本概念 GCRoots对象都有哪些 所有正在运行的线程的栈上的引用变量.所有的全局变量.所有ClassLoader... 1.System Class.2.JNI Local3.JNI Gl ...

  9. JUnit4参数化测试实例

    在JUnit中,可以同时使用@RunWith 和 @parameter 注解来为单元测试传递参数. 注意: 在Eclipse中因为版本问题,可能无法使用@parameters(name = " ...

  10. e565. 关闭的时候隐藏窗口

    By default, when the close button on a frame is clicked, nothing happens. This example shows how to ...