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 ;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM