mybatis逆向工程之一鍵生成數據庫表對應的實體對象和mapper.xml映射文件


Mybatis簡述一下其優缺點

  • mybatis是一種持久層框架,也屬於ORM映射。前身是ibatis。
  • 相比於hibernatehibernate為全自動化,配置文件書寫之后不需要書寫sql語句,但是欠缺靈活,很多時候需要優化;
  • mybatis為半自動化,需要自己書寫sql語句,需要自己定義映射。增加了程序員的一些操作,但是帶來了設計上的靈活,並且也是支持hibernate的一些特性,如延遲加載,緩存和映射等;對數據庫的兼容性比hibernate差。移植性不好,但是可編寫靈活和高性能的sql語句。

1.sql語句與代碼分離,存放於xml配置文件中:
優點:便於維護管理,不用在java代碼中找這些語句;

缺點: JDBC方式可以用用打斷點的方式調試,但是Mybatis不能,需要通過log4j日志輸出日志信息幫助調試,然后在配置文件中修改。

2.用邏輯標簽控制動態SQL的拼接:
優點:用標簽代替編寫邏輯代碼;

缺點:拼接復雜SQL語句時,沒有代碼靈活,拼寫比較復雜。不要使用變通的手段來應對這種復雜的語句。

3.查詢的結果集與java對象自動映射:
優點:保證名稱相同,配置好映射關系即可自動映射或者,不配置映射關系,通過配置列名=字段名也可完成自動映射。

缺點:對開發人員所寫的SQL依賴很強。

4.編寫原聲SQL:
優點:接近JDBC,比較靈活。

缺點:對SQL語句依賴程度很高;並且屬於半自動,數據庫移植比較麻煩,比如mysql數據庫編程Oracle數據庫,部分的sql語句需要調整。

————————————————————————————————————————————————————————

至此結束,介紹就到這里了,小編今日給大家介紹如何使用 Mybatis的逆向工程 一鍵生成 由數據庫表——>實體,mapper.xml,mapper.java 一站式生成策略

從 pom 文件開始

添加 mybatis-generator-maven-plugin 依賴插件 以及相關配置文件插件 resource (用於掃描配置文件)

<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>

 在resource 目錄下(也可以自定義目錄,配置相關路徑需要更改)創建 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>

<!--導入屬性配置 在同級的目錄下創建 generator.properties-->
<properties resource="generator.properties"></properties>

<!--指定特定數據庫的jdbc驅動jar包的位置(絕對路徑)-->
<classPathEntry location="${jdbc.driverLocation}"/>

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

<!-- optional,旨在創建class時,對注釋進行控制 -->
<commentGenerator>
<!--是否去掉自動生成的注釋 true:是-->
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>

<!--jdbc的數據庫連接:驅動類、鏈接地址、用戶名、密碼-->
<jdbcConnection
driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.connectionURL}"
userId="${jdbc.userId}"
password="${jdbc.password}">
</jdbcConnection>

<!-- 非必需,類型處理器,在數據庫類型和java類型之間的轉換控制-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>

<!-- Model模型生成器,用來生成含有主鍵key的類,記錄類 以及查詢Example類
targetPackage 指定生成的model生成所在的包名
targetProject 指定在該項目下所在的路徑
-->
<javaModelGenerator targetPackage="com.technologies.bear.entity"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>


<!--Mapper映射文件生成所在的目錄 為每一個數據庫的表生成對應的SqlMap文件 -->
<sqlMapGenerator targetPackage="mappers"
targetProject="src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>

<!-- 客戶端代碼,生成易於使用的針對Model對象和XML配置文件 的代碼
type="ANNOTATEDMAPPER",生成Java Model 和基於注解的Mapper對象
type="MIXEDMAPPER",生成基於注解的Java Model 和相應的Mapper對象
type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.technologies.bear.dao"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>

<!-- 數據表進行生成操作 tableName:表名; domainObjectName:對應的DO -->
<table tableName="bear_user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="bear_role" domainObjectName="Role"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>

 

配置文件中 有一個 properties 標簽標注配置文件 generator.properties 在 generatorConfig.xml同級目錄下創建

jdbc.driverLocation=D:\\Downloads\\mysql-connector-java-8.0.11\\mysql-connector-java-8.0.11\\mysql-connector-java-8.0.11.jar
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://****:3306/db?useUnicode=true&characterEncoding=utf-8
jdbc.userId=***
jdbc.password=***

 在 generator.properties 配置文件中有段 jdbc.driverLocation=url.jar 的配置,這個jar包自行下載,把電腦里的路徑拷貝過來就可以

雙擊運行插件 :

運行日志:

[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< com.technologies:bear >------------------------
[INFO] Building bear 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.2:generate (default-cli) @ bear ---
[INFO] Connecting to the Database
Wed Apr 08 10:46:38 CST 2020 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 bear_user
log4j:WARN No appenders could be found for logger (org.mybatis.generator.internal.db.DatabaseIntrospector).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[INFO] Introspecting table bear_role
[INFO] Introspecting table bear_permission
[INFO] Introspecting table bear_user_role
[INFO] Introspecting table bear_role_permission
[INFO] Generating Record class for table bear_user
[INFO] Generating Mapper Interface for table bear_user
[INFO] Generating SQL Map for table bear_user
[INFO] Generating Record class for table bear_role
[INFO] Generating Mapper Interface for table bear_role
[INFO] Generating SQL Map for table bear_role
[INFO] Generating Record class for table bear_permission
[INFO] Generating Mapper Interface for table bear_permission
[INFO] Generating SQL Map for table bear_permission
[INFO] Generating Record class for table bear_user_role
[INFO] Generating Mapper Interface for table bear_user_role
[INFO] Generating SQL Map for table bear_user_role
[INFO] Generating Record class for table bear_role_permission
[INFO] Generating Mapper Interface for table bear_role_permission
[INFO] Generating SQL Map for table bear_role_permission
[INFO] Saving file UserMapper.xml
[INFO] Saving file RoleMapper.xml
[INFO] Saving file PermissionMapper.xml
[INFO] Saving file UserRoleMapper.xml
[INFO] Saving file RolePermissionMapper.xml
[INFO] Saving file User.java
[INFO] Saving file UserMapper.java
[INFO] Saving file Role.java
[INFO] Saving file RoleMapper.java
[INFO] Saving file Permission.java
[INFO] Saving file PermissionMapper.java
[INFO] Saving file UserRole.java
[INFO] Saving file UserRoleMapper.java
[INFO] Saving file RolePermission.java
[INFO] Saving file RolePermissionMapper.java
[WARNING] Cannot obtain primary key information from the database, generated objects may be incomplete
[WARNING] Cannot obtain primary key information from the database, generated objects may be incomplete
[WARNING] Cannot obtain primary key information from the database, generated objects may be incomplete
[WARNING] Cannot obtain primary key information from the database, generated objects may be incomplete
[WARNING] Cannot obtain primary key information from the database, generated objects may be incomplete
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.995 s
[INFO] Finished at: 2020-04-08T10:46:39+08:00
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

 我生成的持久層的 mapper路徑: com.technologies.bear.dao   在xml的配置文件中配了

在resource目錄下會多一個mapper文件夾,存放的是mapper.xml映射文件

恭喜 SUCCESS!


免責聲明!

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



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