1. 程式人生 > >mybits根據表自動生成 java類和mapper 檔案

mybits根據表自動生成 java類和mapper 檔案

mybits根據表自動生成 java類和mapper 檔案

  我這個腦子啊,每次建立新的工程都會忘記是怎麼整合mybits怎麼生成mapper檔案的,so today , I can't write this blog for myself.

  NO.1 we should create table on the database.

  eg.user

CREATE TABLE `t_users` (
  `uid` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'PRK',
  `username` varchar(32) DEFAULT NULL COMMENT '使用者名稱',
  `password` varchar(
128) DEFAULT NULL COMMENT '密碼', `email` varchar(64) DEFAULT NULL COMMENT '郵箱', `crtime` datetime DEFAULT NULL COMMENT '建立時間', PRIMARY KEY (`uid`), UNIQUE KEY `name` (`username`), UNIQUE KEY `mail` (`email`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

  NO.2 我們應該在我們的專案中新增自動生成程式碼的xml 和 pom 檔案中加入配置 前提是mybits需要引入啊

1.pom檔案中
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- mybatis generator 自動生成程式碼外掛 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile><!-- 自動生成程式碼配置檔案路徑 -->
            <overwrite>true</overwrite> 
            <verbose>true</verbose>
          </configuration>
        </plugin>
      </plugins>
</build>
2.配置檔案
<?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>
<!-- 資料庫驅動:選擇你的本地硬碟上面的資料庫驅動包-->
<classPathEntry location="F:\soft\apache-maven-3.5.2\mvnRepository\mysql\mysql-connector-java\5.1.37\mysql-connector-java-5.1.37.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自動生成的註釋 true:是 : false:否 -->
<property name="suppressAllComments" value="false"/>
<!-- 生成的Java檔案的編碼 -->
<property name="javaFileEncoding" value="UTF-8"/>
</commentGenerator>
<!--資料庫連結URL,使用者名稱、密碼 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/dev_database" userId="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.zhaiyt.mylife.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成對映檔案的包名和位置-->
<sqlMapGenerator targetPackage="com.zhaiyt.mylife.mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.zhaiyt.mylife.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是資料庫中的表名或檢視名 domainObjectName是實體類名-->
<!--<table tableName="t_sr_problem_proces" domainObjectName="TSrProblemProces" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table>-->
<table tableName="common_cfg_code" domainObjectName="CommonCfgCode" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>

NO.3 IDEA配置

Run/Debug Configurations 

Maven -  Command line   mybatis-generator:generate -e

 

NO.4 Just Run It ;