1. 程式人生 > >Eclipse中使用Mybatis自動生成工具

Eclipse中使用Mybatis自動生成工具

  1. 外掛的安裝
    Help–>Eclipse Marketpalce,搜尋mybatis,
    選擇如下圖的外掛:這裡寫圖片描述
    一路安裝,重啟idea
  2. 配置檔案的生成
    new–>file–>other,搜尋mybatis,選擇如下圖檔案:這裡寫圖片描述
  3. 執行檔案,自動生成相應的檔案和實體類
    配置檔案,我的配置檔案如下
<?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> <context id="testTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自動生成的註釋 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--資料庫連線的資訊:驅動類、連線地址、使用者名稱、密碼 -->
<!-- <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/xshop?useUnicode=true&characterEncoding=utf8" userId="root" password="root"> </jdbcConnection>--> <jdbcConnection driverClass
="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/xshop" userId="root" password="root">
</jdbcConnection> <!-- 預設false,把JDBC DECIMAL 和 NUMERIC 型別解析為 Integer,為 true時把JDBC DECIMAL 和 NUMERIC 型別解析為java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成PO類的位置 --> <javaModelGenerator targetPackage="com.linchong.pojo" targetProject="testMybatis/src"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> <!-- 從資料庫返回的值被清理前後的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:mapper對映檔案生成的位置 --> <sqlMapGenerator targetPackage="cn.linchong.mapper" targetProject="testMybatis/src"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:mapper介面生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.linchong.mapper" targetProject="testMybatis/src"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定資料庫表 --> <!--<table tableName="items"></table> --> <table tableName="tb_shop"></table> </context> </generatorConfiguration>

執行配置檔案生成對應的實體類和mapper檔案:點選配置檔案,右鍵run as->mybatis…如下圖所示:這裡寫圖片描述
期間出現的錯誤
1.MySql8 Client does not support authentication protocol requested by server; consider upgrading MySQL
解決方法:
USE mysql;
ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘password’;
FLUSH PRIVILEGES;