自动生成Mybatis类文件

1、首先用生成一个空的Maven工程
2、增加

<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.0modelVersion>


  <groupId>com.dkdgroupId>
  <artifactId>com.dkdartifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>jarpackaging>


  <name>com.dkdname>
  <url>http://maven.apache.orgurl>


  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
  properties>


  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>3.8.1version>
      <scope>testscope>
    dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.36version>
        dependency>
  dependencies>

  <build>  
    <plugins>  
      <plugin>
          <groupId>org.mybatis.generatorgroupId>
          <artifactId>mybatis-generator-maven-pluginartifactId>
          <version>1.3.2version>
          <configuration>
              <verbose>trueverbose>
              <overwrite>trueoverwrite>
          configuration>
      plugin>
    plugins>  
  build>  
project>

3、创建一个表并写入数据:

CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);

insert into pet values('Fluffy', 'Harold', 'cat', 'f', '1993-02-04', null);
insert into pet values('Claws', 'Gwen', 'cat', 'm', '1994-03-17', null);
insert into pet values('Buffy', 'Harold', 'dog', 'f', '1989-05-13', null);
insert into pet values('Fang', 'Benny', 'dog', 'm', '1990-08-27', null);
insert into pet values('Bowser', 'Diane', 'dog', 'm', '1979-08-31', '1995-07-29');
insert into pet values('Chirpy', 'Gwen', 'bird', 'f', '1998-09-11', null);
insert into pet values('Whistler', 'Gwen', 'bird', null, '1997-12-09', null);
insert into pet values('Slim', 'Benny', 'snake', 'm', '1996-04-29', null);

4、运行 mybatis-generator ,将下面的xml文件放在resources(src\main\resources)目录下




<generatorConfiguration>
    <classPathEntry location="C:\Users\web-214\.m2\repository\mysql\mysql-connector-java\5.1.36\mysql-connector-java-5.1.36.jar" />

    <context id="DB2Tables" targetRuntime="MyBatis3">

        <commentGenerator>
            <property name="suppressDate" value="true" />
        commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost/dengkaidi" userId="root" password="123456">
        jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        javaTypeResolver>

        <javaModelGenerator targetPackage="test.model"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        javaModelGenerator>

        <sqlMapGenerator targetPackage="test.dao"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.dkd" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        javaClientGenerator>

        <table tableName="pet" domainObjectName="Pet">
        table>

    context>
generatorConfiguration>

5、执行命令即可生成
mvn mybatis-generator:generate

你可能感兴趣的:(mybatis,maven)