1. 程式人生 > >自動生成bean層dao層以及xml層工具

自動生成bean層dao層以及xml層工具

首先需要一個配置檔案configuration.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:資料庫的JDBC驅動的jar包地址-->
	<classPathEntry location="E:\xml\jar\mysql-connector-java-5.1.21.jar" />
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<!-- 是否去除自動生成的註釋 true:是 : false:否 -->
			<property name="suppressAllComments" value="false" />
			<!--資料庫連線的資訊:驅動類、連線地址、使用者名稱、密碼 -->
		</commentGenerator>
		<!-- 控制資料庫配置 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf8"
			userId="root" password="password" />
		<!--
			預設false,把JDBC DECIMAL 和 NUMERIC 型別解析為 Integer true,把JDBC DECIMAL 和
			NUMERIC 型別解析為java.math.BigDecimal
		-->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		<!-- targetProject:自動生成實體類程式碼的位置 -->
		<javaModelGenerator targetPackage="net.okdi.api.entity"
			targetProject="E:\xml\main">
			<!-- enableSubPackages:是否讓schema作為包的字尾 -->
			<property name="enableSubPackages" value="false" />
			<!-- 從資料庫返回的值被清理前後的空格  -->
			<property name="trimStrings" value="false" />
		</javaModelGenerator>
		<!--
			<sqlMapGenerator> =>
			配置生成相應的實體Mapper.xml,對於Mapper3.X我們需要把type="XMLMAPPER"
		-->
		<sqlMapGenerator targetPackage="net.okdi.api.dao"
			targetProject="E:\xml\main">
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		<!--<javaClientGenerator> => 配置生成相應的介面類,對應與Mapper.xml中的一系列CRUD方法SQL語句-->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="net.okdi.api.dao" targetProject="E:\xml\main">
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>
		<!-- tableName:用於自動生成程式碼的資料庫表;domainObjectName:對應於資料庫表的javaBean類名 -->


		<table schema="test" tableName="sms_log"
			domainObjectName="SmsLog" enableCountByExample="false"
			enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false" />
			
		<table schema="test" tableName="sms_lxhl_key"
			domainObjectName="SmsLxhlKey" enableCountByExample="false"
			enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false" />
	</context>
</generatorConfiguration> 
然後就是一個工具類GenerateRun.java
package com.amssy.util;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

public class GenerateRun {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		generateMbgConfiguration();
	}

	private static void generateMbgConfiguration() {
		/*
		 * Mybatis自帶Generator工具生成相應東西
		 */
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		// 配置檔案的位�?可以為專案路徑也可以為磁碟的絕對路徑
		File configFile = new File("./src/com/amssy/config/configuration.xml");
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration config = null;
		try {
			config = cp.parseConfiguration(configFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XMLParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		try {
			MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
			myBatisGenerator.generate(null);
		} catch (InvalidConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println("生成Mybatis配置成功?");
	}

}

用到的jar包:

mybatis-3.2.2.jar
mybatis-generator-core-1.3.2.jar
mybatis-spring-1.2.2.jar
mysql-connector-java-5.1.21.jar

相關推薦

Mybatis--使用mybatis generator外掛對映資料庫,自動生成pojo物件,dao介面,mapper.xml檔案的方法

五個步驟: 步驟1:首先保證資料庫能正常連線 步驟2:用MAVEN依賴工具安裝mybatis generator外掛 步驟3:建立datasource.properties檔案,為連線資料庫提供支援 步驟4:配置generatorConf

MyBatis逆向工程-根據資料庫表自動生成bean、mapper介面以及對映檔案

說明:偶然看到一個視訊,講到了使用mybatis的逆向工程實現自動生成程式碼的部分(根據資料表生成相應的實體類、對映檔案、介面),因為之前沒有學習過這類東西,今天照著弄了下,然後自己寫了一個測試案例,特來記錄。。。。==============================

自動生成beandao以及xml工具

首先需要一個配置檔案configuration.xml檔案 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.

使用mybatis自帶工具自動生成表對應domain、mapper.xml以及dao

upd stat javac sch val char 數據庫名 tab decimal 引用:http://blog.csdn.net/p793049488/article/details/40422975 1、需要手動建表,如T_PRI_USER 2、新建配置文件g

利用Mybatis-generator自動生成java-beandao和mapper.xml

1.首先百度: mybatis generator ,進入mybatis generator GitHub,然後進入http://www.mybatis.org/generator/. 2.點選左邊 Quick Start Guide ,他告訴我們需要匯入相關聯的jar包(

Spring boot 自動生成mybatis的dao、model、和mapper

1、pom.xml新增內容,最下邊新增plugin <build> <pluginManagement> <plugins> <plugin> <groupId>org.springfr

使用generatorConfig工具自動生成mybatis的實體類以及dao接口和映射文件

color 大寫 nod com auto 主鍵 imm target ont 1:數據準備 創建一個數據庫表 1 CREATE TABLE `logininfo` ( 2 `id` BIGINT(20) NOT NULL AUTO_INCREMENT, 3 `u

在Idea中自動生成實體類和hibernate.cfg.xml檔案

1  按快捷鍵 ctrl+shift+alt+s調出project structure選單, 點選專案名稱, 新增hibernate模組, 在最右側點選+號, 新增hibernate.cfg.xml檔案 2  點選DataBase中的+號 ,連線 mysql資料庫 選擇資料庫名稱,  建立連線

Spring生成bean的三種方式(xml)

1.普通的配置bean物件,呼叫的是其無參構造方法。 <bean id="userDao" class="com.icbc.spring.study1.UserDaoImpl"></bean> 即:該userDao 的例項 是通過

自動生成實體類 dao

自動生成dao方法  實體類 工程前   :借用 mybatis-gennerator外掛自動生成mybatis所需要的dao、bean、mapper xml檔案, 這樣我們可以節省一部分精力,把精力放在業務邏輯上。 程式打包上傳的地址   http://downloa

mybatis,自動生成domain、dao、mapping檔案

資源包下載(Click me!) 1. 將壓縮包解壓到D盤根目錄 2. 開啟generatorConfig.xml檔案,修改如下紅框內容 3. win+R  輸入cmd,進入命令視窗 4. 切換到D:\mybatis-generator這個資料夾路徑下 5.

【java專案實踐】在eclipse中使用利用mybatis-generator自動生成Model、Dao、mapping程式碼

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Conf

用mybatis-generator的逆向工程生成beandao

1、在pom.xml裡新增maven外掛 <plugin> <groupId>org.mybatis.generator</groupId> <artif

使用MyBatis Generator外掛自動生成Dto、Dao、Mapping

一、下載mybatis-generator-core 進入:http://code.google.com/p/mybatis/ 二、生成配置檔案 新建一個空的XML配置檔案,名稱可以隨便取,這裡以generatorConfig.xml為名。最好將這個檔案放在下載後

MyBatis自動生成Entity、Dao、Mapping

  新接的專案使用了MyBatis,Mybatis屬於半自動ORM,在使用這個框架中,工作量最大的就是書寫Mapping的對映檔案,對於其中最基礎的Entity,以及最基礎的CRUD,我們可以利用Mybatis-Generator來幫我們自動生成檔案。   首先下載相關檔案

Eclipse根據wsdl檔案自動生成webservice client圖解,以及呼叫示例,附測試專案

以實際專案中遇到的部分為例 1.已知wsdl檔案:SI_SD01302_GT2ERP_Syn_OutService.wsdl 2.在Eclipse中生成webservice客戶端程式碼,New---->Other---->Webservice---->We

Mybatis中通過generator生成mapper、Dao、mapper.xml

2.更改generator.xml (1)更改資料驅動包的位置,選擇你所安裝的目錄: (2)更改你所要連線的資料庫的名稱,資料庫的user和password (3)更改下圖 ta

spring註解@Component、@Service等自動生成bean的命名規則

參考連結:資訊來源今天碰到一個問題,寫了一個@Service的bean,類名大致為:CUserxml配置:<context:component-scan base-package="com.xxx.xx.x"/>結果啟動報錯:No bean named 'cUse

VS2015、VS2017自動生成roslyn的資料夾以及csc.exe

csc.exe代表C# 編譯器,所以在需要專案nuget包引用”Microsoft.CodeDom.Providers.DotNetCompilerPlatform“以及”Microsoft.Net.Compilers“,然後在編譯的時候,自動會在bin資料夾裡生成rosly

線上自動生成.9png圖的Android設計切圖工具推薦

當我們完成了一套iOS的切圖的時候,當android開發人員讓你切幾個版本的dpi切圖時,你是不是一下子崩潰了! 我想大家都會崩潰,切一套APP設計稿 已經夠麻煩了。 因為在Android的設計過程中,為了適配不同的手機解析度,圖片大多需要拉伸或者壓縮,這樣就出現了可以