mybatis-generator 代码自动生成工具使用讲解(maven方式)

来源:互联网 发布:纱窗淘宝 编辑:程序博客网 时间:2024/05/17 01:59

Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。这里讲的是maven的方式,就不贴上jar的下载,需要下载的百度上搜一下。


POM.XML

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.frameStudy</groupId><artifactId>mybatisGeneratorDemo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>mybatisGeneratorDemo</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.2.0</version></dependency><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.2</version></dependency></dependencies><build><plugins><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.2</version><configuration> <!-- 在控制台打印执行日志 -->    <verbose>true</verbose><!-- 重复生成时会覆盖之前的文件-->  <overwrite>true</overwrite></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><configuration><testFailureIgnore>true</testFailureIgnore></configuration></plugin></plugins></build></project>

generatorConfig.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>  <!-- 配置文件路径 -->  <!--<properties resource="generatorConfig.properties"/>  --><!--数据库驱动包路径 -->  <classPathEntry location="D:\generator\mysql-connector-java-5.1.10-bin.jar"/>     <context id="MySQLTables" targetRuntime="MyBatis3">  <!-- 注释 -->  <commentGenerator >  <!--  fals是确定,true是取消-->    <property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->      <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->  </commentGenerator>      <!--数据库连接信息 -->  <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/sm?characterEncoding=utf8" userId="root"  password="zhu5201314.">  </jdbcConnection>     <!--生成的model 包路径 -->  <javaModelGenerator targetPackage="com.trip.model" targetProject="H:\javaworkspaceEE\sssss\src\main\java">  <property name="enableSubPackages" value="ture"/>  <property name="trimStrings" value="true"/>  </javaModelGenerator>     <!--生成xml mapper文件 路径 -->  <sqlMapGenerator targetPackage="com.trip.mapper" targetProject="H:\javaworkspaceEE\sssss\src\main\java">  <property name="enableSubPackages" value="ture"/>  </sqlMapGenerator>     <!-- 生成的Dao接口 的包路径 -->  <javaClientGenerator type="XMLMAPPER" targetPackage="com.trip.dao" targetProject="H:\javaworkspaceEE\sssss\src\main\java">  <property name="enableSubPackages" value="ture"/>  </javaClientGenerator>  <!--对应数据库表名 -->  <!--   enableCountByExample,enableUpdateByExample,enableDeleteByExample,enableSelectByExample等配置为false后,  就不会生成生成Examle类了, 这样看起来代码干净很多, 如果需要用到where条件语句的时候 可以自已在对应的配置文件来配置sql mapper就可以了.--><table tableName="t_user" domainObjectName="bas_dict" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="true" />  </context>  </generatorConfiguration>  

使用方式:项目右键→Run Asmaven build→输入 mybatis-generator:generate



执行的结果:这是最后执行成功后生成的类



注意:targetProject这个属性必须给全路径,在这里我就入了一个小坑。

原创粉丝点击