MyBatis Generator 自动生成代码

首先需要一个jar包 :mybatis-generator-core-1.3.6.jar   ,将其导入

        下载地址:https://github.com/gblau/mybatis-generator-core-1.3.2/tree/master/lib

接下来,写一个配置文件如下:

<?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="DB2Tables" targetRuntime="MyBatis3">

		<commentGenerator>
			<property name="suppressAllComments" value="true"/>
		</commentGenerator>
		
		<!-- 配置数据库连接 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/teachers" userId="root"
			password="root">
		</jdbcConnection>

		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- 指定javaBean生成的位置 -->
		<javaModelGenerator targetPackage="com.teachers.beans" 
			targetProject=".\src">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>

		<!-- 指定sql映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="com.teachers.mappers"
			targetProject=".\src">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>

		<!-- 指定dao接口生成的位置 ,mapper接口 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.teachers.dao" targetProject=".\src">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>

		<!-- 指定每个表的生成策略 -->
		<table tableName="excel" domainObjectName="Excel"></table>
		<table tableName="video" domainObjectName="Video"></table>
		<table tableName="ppt" domainObjectName="Ppt"></table>
		<table tableName="word" domainObjectName="Word"></table>
		<table tableName="admin" domainObjectName="user"></table>
	</context>
</generatorConfiguration>

上面的配置根据自己的需要修改,然后复制以下Java代码:

package com.teachers.test;

import java.io.File;
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.internal.DefaultShellCallback;

public class helloWorld {
	
//	根据配置文件,运行一下代码即可生成代码
	public static void main(String[] args) throws Exception {
		  List<String> warnings = new ArrayList<String>();
		   boolean overwrite = true;
		   File configFile = new File("mbg.xml");
		   ConfigurationParser cp = new ConfigurationParser(warnings);
		   Configuration config = cp.parseConfiguration(configFile);
		   DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		   MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
		   myBatisGenerator.generate(null);
	}
}

运行上面的Java代码即可生成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值