在intellij idea中使用mybatis插件产生dao、model

版权声明: https://blog.csdn.net/ksksjipeng/article/details/59182145

之前一直使用mybatis编写dao、mapper、以及model层,由于是半路出家,只能这样子手工编写,于是各种问题,新手不易,后来接触到在intellij idea有个mybatis插件可以直接产生dao等这些文件,省了不少事情,研究了一番,果真好用。

1、项目的pom.xml文件中加入以下代码

 <plugins>
    <!-- Mybatis generator代码生成插件 配置 -->
    <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>${plugin.mybatis.generator}</version>
        <configuration>
            <configurationFile>${mybatis.generator.generatorConfig.xml}</configurationFile>
            <overwrite>true</overwrite>
            <verbose>true</verbose>
        </configuration>
    </plugin>

    <!-- Maven插件 配置 -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${plugin.maven-compiler}</version>
        <configuration>
            <source>${project.build.jdk}</source>
            <target>${project.build.jdk}</target>
            <encoding>${project.build.sourceEncoding}</encoding>
        </configuration>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${plugin.maven-surefire}</version>
        <configuration>
            <skipTests>${skipTests}</skipTests>
        </configuration>
    </plugin>
</plugins>

完成的pom.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 url="${mybatis.generator.generatorConfig.properties}"/>

    <!--数据库驱动包路径 -->
    <classPathEntry location="${drive.class.path}"/>

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!--关闭注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--数据库连接信息 -->
        <jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}"
                        password="${jdbc.password}">
        </jdbcConnection>

        <!--生成的model 包路径 -->
        <javaModelGenerator targetPackage="${model.package}" targetProject="${target.project}">
            <property name="enableSubPackages" value="ture"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!--生成xml mapper文件 路径 -->
        <sqlMapGenerator targetPackage="${xml.mapper.package}" targetProject="${target.project}">
            <property name="enableSubPackages" value="ture"/>
        </sqlMapGenerator>

        <!-- 生成的Dao接口 的包路径 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="${dao.package}" targetProject="${target.project}">
            <property name="enableSubPackages" value="ture"/>
        </javaClientGenerator>

        <!--用户表 -->
        <table tableName="user" domainObjectName="User"/>

        <!--权限表-->
        <table tableName="permission" domainObjectName="Permission"/>

        <!--角色表-->
        <table tableName="role" domainObjectName="Role"/>

    </context>
</generatorConfiguration>

generatorConfig.properties完整文件如下

# 数据库驱动jar 路径
//本地仓库mysql-connector-java.jar文件路径
drive.class.path=/Users/terrence/.m2/repository/mysql/mysql-connector-java/5.1.39/mysql-connector-java-5.1.39.jar
# 数据库连接参数
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/hello?useUnicode=true&characterEncoding=utf-8
jdbc.username=xxx
jdbc.password=xxx
# 包路径配置
model.package=com.xxx.model
dao.package=com.xxx.dao
xml.mapper.package=com.xxx.dao
//目标目录
target.project=src/main/java

3、接下来开始配置intellij idea,打开ide,点击Run->Edit Configurations
这里写图片描述

4、点击+号按钮,选择添加Maven,添加选择框以下内容
这里写图片描述

添加完成以后,再次点击Run->generator,此时ide会开始执行

generatorConfig.xml文件,出现以下信息说明已经成功
/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/bin/java -Dmaven.multiModuleProjectDirectory=/Users/terrence/Downloads/quick4j-master -DarchetypeCatalog=local -Dmaven.home=/Users/terrence/Documents/DevTool/apache-maven-3.3.9 -Dclassworlds.conf=/Users/terrence/Documents/DevTool/apache-maven-3.3.9/bin/m2.conf -Didea.launcher.port=7533 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Users/terrence/Documents/DevTool/apache-maven-3.3.9/boot/plexus-classworlds-2.5.2.jar:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=2016.3.1 mybatis-generator:generate -e
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building quick4j App 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.1:generate (default-cli) @ quick4j ---
[INFO] Connecting to the Database
Wed Mar 01 22:48:44 HKT 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
[INFO] Introspecting table user
[INFO] Introspecting table permission
[INFO] Introspecting table role
[INFO] Generating Example class for table user
[INFO] Generating Record class for table user
[INFO] Generating Mapper Interface for table user
[INFO] Generating SQL Map for table user
[INFO] Generating Example class for table permission
[INFO] Generating Record class for table permission
[INFO] Generating Mapper Interface for table permission
[INFO] Generating SQL Map for table permission
[INFO] Generating Example class for table role
[INFO] Generating Record class for table role
[INFO] Generating Mapper Interface for table role
[INFO] Generating SQL Map for table role
[INFO] Saving file UserMapper.xml
[INFO] Saving file PermissionMapper.xml
[INFO] Saving file RoleMapper.xml
[INFO] Saving file UserExample.java
[INFO] Saving file User.java
[INFO] Saving file UserMapper.java
[INFO] Saving file PermissionExample.java
[INFO] Saving file Permission.java
[INFO] Saving file PermissionMapper.java
[INFO] Saving file RoleExample.java
[INFO] Saving file Role.java
[INFO] Saving file RoleMapper.java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.443 s
[INFO] Finished at: 2017-03-01T22:48:44+08:00
[INFO] Final Memory: 11M/156M
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

此时分享说明配置成功。

猜你喜欢

转载自blog.csdn.net/ksksjipeng/article/details/59182145
今日推荐