Mybatis使用Mybatis-generator插件

首先在POM.xml文件添加架包,我这里用的是SpringBoot,所以用的也是SpringBoot架包,最少要mybatis,generator,mysql

    <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2 </version>
</dependency> <dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<!--最好添加一个线程池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.3</version>
</dependency>

导入插件依赖包后,在plugins内配置

 <!--配置mybatis自动生成代码generator-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>mybatis generator</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<!--允许移动生成文件-->
<verbose>true</verbose>
<!--允许自动覆盖文件-->
<overwrite>true</overwrite>
<configurationFile>
src/main/resources/mybatis-generator.xml
</configurationFile>
</configuration>
</plugin>

然后在resources包下创建mybatis-generator.xml配置文件(注意名字一定一模一样,我就是把-写成了_弄了大半天)

首先在JdbcConnection的标签内配置类,url,userId,pwd等,javaModelGenerator中配置java Bean对应的路径,

sqlMapgenerator的映射路径,mybatis的mapping映射;javaClientGenerator里填写dao层的路径,最后配置数据库的表table,最好加上enableCountByExample="false" ,enableUpdateByExample="false"enableDeleteByExample="false" enableSelectByExample="false"selectByExampleQueryId="false"。

<?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="DB2Tables" targetRuntime="MyBatis3">
<!--数据库连接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/mssc"
userId="root"
password="111111">
</jdbcConnection> <javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--生成Model(DataObject类)存放的位置-->
<javaModelGenerator targetPackage="com.dspro.dataobject" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--生成Dao类文件存放-->
<!--客户端代码,生成易于使用的针对Model对象和XML配置文件的代码-->
<!--type="ANNOTATEDMAPPER" 生成javaModel和基于注解的Mapper对象-->
<!--type="MIXEDMAPPER",生成基于注解的javaModel和相应的Mapper对象-->
<!--type="XMLMAPPER",生成SQLMAP XML文件和独立的Mapper接口-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.dspro.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator> <!--<table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >-->
<!--<property name="useActualColumnNames" value="true"/>-->
<!--<generatedKey column="ID" sqlStatement="DB2" identity="true" />-->
<!--<columnOverride column="DATE_FIELD" property="startDate" />-->
<!--<ignoreColumn column="FRED" />-->
<!--<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />-->
<!--</table>-->
<table tableName="user_info" domainObjectName="UserDO"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="user_password" domainObjectName="UserPasswordDo"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table> </context>
</generatorConfiguration>
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"
这几个配置了之后就不会产生example类,不然新版本会产生一个example文件。

上面是我自己的配置,需要官方配置xml文件的官方配置

然后(IDEA)run-->edit configurations,点击"+"添加maven,在Command line处填写

mybatis-generator:generate
命令,
点击OK,然后run。 错误:
   "POM for xxx is missing, no dependency information available"
The POM for org.eclipse.m2e:lifecycle-mapping:jar:1.0. is missing, no dependency information available

在POM里添加:

        <plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<versionRange>[1.2,)</versionRange>
<goals>
<goal>enforce</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>

最新文章

  1. for循环每次取出一个字符(不是字节)
  2. DOM2级提供的对DOM结构执行深度优先遍历 笔记
  3. TCP/IP协议中网关和子网掩码概念
  4. shh简化
  5. crc循环冗余校验
  6. Irrlicht 鬼火
  7. http://www.oreilly.com/catalog/errataunconfirmed.csp?isbn=9780596529321
  8. zabbix 乱码的问题
  9. iptables的详细介绍及配置方法*
  10. iOS开发之 Xcode6 添加xib文件,去掉storyboard的hello world应用
  11. linux-``反引号
  12. SSHD配置
  13. github + SourceTree管理自己的库并上传到cocoapods及各种坑的解决办法
  14. keepalived脑裂问题查找
  15. sql审核工具
  16. ggplot2 梯度作图
  17. Windows 下使用virtualenv 第一次使用flask
  18. nginx 本地映射
  19. linux raid10管理维护
  20. VB 中窗口发现冲突名称,将使用名称...怎么解决?

热门文章

  1. mysql操作篇续
  2. [Flink]测试用的fake温度传感器
  3. [PHP] vscode配置SFTP扩展同步文件
  4. node.js中pm2启动应用出错
  5. golang数据结构和算法之QueueLinkedList链表队列
  6. Badboy安装和简单介绍
  7. 【CSP-SJX 2019】T4 散步
  8. day_0
  9. 第04组 Alpha事后诸葛亮
  10. QDialog 设置成圆角