自动生成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

原文链接:/xml/296444.html

猜你在找的XML相关文章