mybatis-generator外掛的使用方法
之前看過一篇人氣頗高的博主的帖子,因為本人也是初學者,之前的使用框架都是自己寫實體類,
發現mybatis-generator後想具體學習可發現大多的帖子都十分深奧(不能直觀地理解與使用,看過和沒看過沒什麼兩樣)
現自己總結一篇附中間遇到的問題。
使用工具:
Idea MySQL8 jdk8 maven3
第一步,資料庫中建立表
CREATE TABLE test_user( user_id INT(11), loginName VARCHAR(20) NOT NULL, loginPwd VARCHAR(50) NOT NULL, user_sex TINYINT(4) DEFAULT(0), user_name VARCHAR(20), user_phone CHAR(11), user_email VARCHAR(50), PRIMARY KEY(user_id) )
檢視pom檔案中是否存在MySQL相關jar包
c3p0是一個jdbc連線池,用其他什麼的都可以,MySQL的版本最好和當前配對使用的版本一致,當然不一致也沒什麼問題,但是如果出錯了你有可能在這個問題上浪費時間
Mybatis。。。。。。。。。。
新增外掛
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin>
Ok,看見了沒,其中有個xml檔案的路徑配置,自己要先建立呢個xml檔案,只要你願意且能保證系統找到隨便你放哪,檢測能不能找到按住CTRL 能點進去就行
我的目錄在這
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> <!-- 資料庫驅動:選擇你的本地硬碟上面的資料庫驅動包--> <classPathEntry location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\8.0.11\mysql-connector-java-8.0.11.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自動生成的註釋 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--資料庫連結URL,使用者名稱、密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/cbs?serverTimezone=GMT%2B8" userId="root" password="123qwe"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="com.test.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成對映檔案的包名和位置--> <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.winter.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表 tableName是資料庫中的表名或檢視名 domainObjectName是實體類名--> <table tableName="t_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
Ok 建立好之後
點選run->Edit Configurations
或者
右上交,點選下三角,Edit Configuration
點選左上角+號
選擇maven
改個名字,不改也行
下面的圓圈內填入mybatis-generator:generate -e
點選ok
右上角選擇執行。。。。。。。
原先目錄
執行不報錯就是成功
Ok至此就結束
遇到的問題:
1 本地資料庫驅動不知道在哪?
如果使用自定義maven setxml,呢麼自己去找去,
如果沒有配置,C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\8.0.11
這是我的,你沒配置大概就在就這個位置左右
其實就是自定義maven中jar包所在
2 報錯caching_sha2_password
MySQL8之後出現的驗證方式問題與之前版本不同
使用navicat root登陸或者用MySQL自己的shell都行 cmd也行
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; #修改加密規則
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; #更新一下使用者的密碼
FLUSH PRIVILEGES; #重新整理許可權
3 錯誤提示 The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized。。。。。。。。。。
MySQL時區錯誤,在generatorConfig.Xml
的url後面加上?serverTimezone=GMT%2B8
就像這樣
Ok,
就這樣就結束了,這是一個快速使用的方法,如果大神想教我後面的如何學習,請將學習部落格附在評論中 三q