MyBatis Generator逆向工程自动创建实体类

在我们编写程序时,为数据库中的每张表都要编写对应的实体类,在这个过程中会耗费很多时间,许多的表和字段也很难保证不会打错。MyBatis给我们提供了自动生成实体类,实体映射文件,接口的功能。这大大减少了我们的重复工作。使用MyBatis Generator逆向工程只需三步:

1.引入MyBatis Generator jar包
mybatis-generator-core-1.3.5.jar

2.逆向工程生成规则
在工程下新建一个mbg.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>

	<context id="DB2Tables" targetRuntime="MyBatis3">
		
		<commentGenerator>
		<!-- 此属性用于指定MBG是否将在生成的代码中包含任何coments
		当属性为true时,不会向任何生成的元素添加注释 -->
			<property name="suppressAllComments" value="true"></property>
		</commentGenerator>

		<!-- 指定如何连接到目标数据库 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/studentinfomanager"
			userId="root" password="admin">
		</jdbcConnection>
		
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- 用于为生成的Java模型对象指定目标包和目标项目 -->
		<javaModelGenerator targetPackage="com.javaweb.bean"
			targetProject=".\src\main\java">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>

		<!-- 用于为生成的实体映射文件指定目标包和目标项目 -->
		<sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>

		<!-- 用于为生成的接口指定目标包和目标项目 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.javaweb.dao" targetProject=".\src\main\java">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
	
		<!-- 为每张表设置实体类类名 -->
		<table schema="DB2ADMIN" tableName="s_aattendance"
			domainObjectName="StudentAattendance"></table>
		<table schema="DB2ADMIN" tableName="s_badbehavior"
			domainObjectName="StudentBadbehavior"></table>
		<table schema="DB2ADMIN" tableName="s_basicmessage"
			domainObjectName="StudentBasicmessage"></table>
		<table schema="DB2ADMIN" tableName="s_charge" domainObjectName="StudentCharge"></table>
		<table schema="DB2ADMIN" tableName="s_charge_launch"
			domainObjectName="StudentChargeLaunch"></table>
		<table schema="DB2ADMIN" tableName="s_class" domainObjectName="StudentClass"></table>
		<table schema="DB2ADMIN" tableName="s_honour" domainObjectName="StudentHonour"></table>
		<table schema="DB2ADMIN" tableName="s_login" domainObjectName="StudentLogin"></table>
		<table schema="DB2ADMIN" tableName="s_performance"
			domainObjectName="StudentPerformance"></table>
		<table schema="DB2ADMIN" tableName="s_profession"
			domainObjectName="StudentProfession"></table>
		<table schema="DB2ADMIN" tableName="s_reg" domainObjectName="StudentReg"></table>
		<table schema="DB2ADMIN" tableName="s_work" domainObjectName="StudentWork"></table>

	</context>
</generatorConfiguration>

3.运行MyBatis Generator
使用XML配置文件从Java运行MBG

package com.javaweb.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 TestApp {

	public static void main(String[] args) throws Exception {
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		//copy官方的例子,把这里改成自己定义的配置文件
		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);
		System.out.println("===========");
	}
}

刷新项目,我们所需要的实体类等文件就建好了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值