MyBatis的逆向工程

前言

与Hibernate不同,MyBatis是一个半自动映射的框架,之所以称它为半自动,是因为它需要手工匹配提供POJO、SQL和映射关系,而全表映射的Hibernate只需要提供POJO和映射关系便可。Mybatis现在还没有正向工程,我下面就分享一下MyBatis的逆向工程。逆向工程就是根据数据库设计的表来自动生成对应的实体类和一些映射配置文件,以及一些简单的增删改查操作,大大地提高了开发的效率。


1.建表

逆向工程第一步就是建数据表,我建了person表跟product表

CREATE TABLE `person` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=112 DEFAULT CHARSET=utf8;
CREATE TABLE `product` (
  `id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `price` double DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.在pom.xml文件中配置mybatis逆向生成包

<!-- mybatis的逆向工程包 -->
<dependency>
 <groupId>org.mybatis.generator</groupId>
 <artifactId>mybatis-generator-core</artifactId>
 <version>1.3.3</version>
</dependency>

3.配置mbg.xml文件

在项目的根目录下新建mbg.xml文件


打开逆向工程的官网链接http://www.mybatis.org/generator/configreference/xmlconfig.html,复制下面的内容:



将classPathEntry标签里的内容注释掉,可以加上消除注解的配置,这样逆向生成的配置文件就没有多余的备注了,再修改bean,mapper接口,映射文件的生成位置,修改后的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>
  <!-- <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" /> -->

  	<context id="DB2Tables" targetRuntime="MyBatis3">
  	
  	<!-- 消除注释(可选) -->
  	<commentGenerator>
		<property name="suppressAllComments" value="true"/>
	</commentGenerator>
  
  <!-- 配置数据源 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql:///slash"
        userId="root"
        password="123456">
    </jdbcConnection>

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

	<!-- 指定JavaBean实体类生成的位置 -->
    <javaModelGenerator targetPackage="com.dgut.bean" targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>

	<!-- 指定Mapper.xml映射文件生成位置 -->
    <sqlMapGenerator targetPackage="mapper"  targetProject=".\src\main\resources">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

	<!-- 指定DAO接口生成位置,Mapper接口生成位置 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.dgut.dao"  targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

	<!-- 配置生成策略 -->
    <table tableName="person" domainObjectName="Person" ></table>
    <table tableName="product" domainObjectName="Product" ></table>

  </context>
</generatorConfiguration>


4.启动逆向工程

新建一个启动类,启动逆向工程:

package com.dgut.junit;

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 Generator {
	public static void main(String[] main) {
		try {
			List<String> warnings = new ArrayList<String>();
			boolean overwrite = true;
			File configFile = new File("mbg.xml");
			ConfigurationParser cp = new ConfigurationParser(warnings);
			Configuration config;
			config = cp.parseConfiguration(configFile);
			DefaultShellCallback callback = new DefaultShellCallback(overwrite);
			MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
			myBatisGenerator.generate(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

运行启动类,再refresh一下java工程,就会发现在对应的包里已经自动生成了的实体类,mapper接口和配置文件



这样mybatis的逆向工程就搞定了!

猜你喜欢

转载自blog.csdn.net/java_mike/article/details/78071144