用Maven外掛程式產生Mybatis代碼的實現方法_java

來源:互聯網
上載者:User

現在代碼管理基本上是採用Maven管理,Maven的好處此處不多說,大家用百度搜尋會有很多介紹,本文介紹一下用Maven工具如何產生Mybatis的代碼及映射的檔案。

一、配置Maven pom.xml 檔案

在pom.xml增加以下外掛程式:

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

配置好Maven外掛程式,下面需要配置外掛程式需要設定檔

二、外掛程式設定檔

Maven的項目設定檔存放路徑如下圖:

外掛程式預設會讀到src/main/resources目錄下的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="D:\repo\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar" /> <context id="context1" targetRuntime="MyBatis3">  <jdbcConnection driverClass="com.mysql.jdbc.Driver"   connectionURL="jdbc:mysql://xxxxxxx:8406/CL_DEMO?useUnicode=true&characterEncoding=UTF-8"   userId="root" password="password" />  <javaModelGenerator targetPackage="xxx.account.model"   targetProject="D:\workspace\項目名\src\main\java" />  <sqlMapGenerator targetPackage="xxxx.account.persistence"   targetProject="D:\workspace\項目名\包名\src\main\resources" />  <javaClientGenerator targetPackage="xxxx.account.persistence   targetProject="D:\\workspace\項目名\src\main\java" type="XMLMAPPER" />  <table schema="CL_DEMO" tableName="tb_user" />  <table schema="CL_DEMO" tableName="tb_role" />  <table schema="CL_DEMO" tableName="tb_permission" />  <table schema="CL_DEMO" tableName="tb_role_user" />  <table schema="CL_DEMO" tableName="tb_permission_role" /> </context></generatorConfiguration>

在本例中,使用的是MySQL資料庫,需要指定MySQL資料庫的JDBC驅動。

1、指定串連資料庫的URL;

2、指定產生資料模型對象的包名,如com.xxx.xxx.model, targetProject 指定是項目及存放model的目錄。

3、sqlMapGenerator  需要設定包名,和存放對應檔的路徑。如果用maven 管理,一般xml檔案放在src/main/resources目錄下。

4、javaClientGenerator 需要設定包名及路徑。

6、接下來需要配置你需要產生的表名。

三、產生代碼

配置搞掂後,現在就可以產生代碼了

如果是在eclipse 中,選擇pom.xml檔案,擊右鍵先擇Run AS——>Maven Build… ——>在Goals框中輸入:mybatis-generator:generate

如果在命令列輸入Maven命令即可,注意:一定是當前項目目錄下運行該命令:

mvn mybatis-generator:generate

代碼產生完,大功告別成。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.