MyBatis的逆向工程

来源:互联网 发布:山姆.维特维奇 知乎 编辑:程序博客网 时间:2024/06/10 21:11

  所谓逆向工程,就用代码自动生成,数据库中 表对应的 pojo, mapper.xml和mapper接口,这样自己就可以省去写pojo和通用的 增删查改 sql 语句了,大大的提高了开发效率。


mybatis 的逆向工程实现有很多种方法,这里说我经常使用的一种。


步骤:


1.新建一个java工程,导入需要的jar包。如图所示:

mybatis,mybatis-generator-core,数据库驱动(数据库选择一个即可)必须选择,log4j可选可不选。


2.新建一个GeneratorSqlMap类,代码如下:

import java.io.File;import java.io.IOException;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.XMLParserException;import org.mybatis.generator.internal.DefaultShellCallback;public class GeneratorSqlmap {public void generator() throws Exception{List<String> warnings = new ArrayList<String>();boolean overwrite = true;//指定 逆向工程配置文件File configFile = new File("generatorConfig.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);} public static void main(String[] args) throws Exception {try {GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();generatorSqlmap.generator();} catch (Exception e) {e.printStackTrace();}}}

3.新建一个generatorConfig.xml(放在classpath下)

<?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="testTables" targetRuntime="MyBatis3"><commentGenerator><!-- 是否去除自动生成的注释 true:是 : false:否 --><property name="suppressAllComments" value="true" /></commentGenerator><!--数据库连接的信息:驱动类、连接地址、用户名、密码 --><jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/e3mall" userId="root"password="root"></jdbcConnection><!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --><javaTypeResolver><property name="forceBigDecimals" value="false" /></javaTypeResolver><!-- targetProject:生成PO类的位置 --><javaModelGenerator targetPackage="com.e3mall.pojo"targetProject=".\src"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="false" /><!-- 从数据库返回的值被清理前后的空格 --><property name="trimStrings" value="true" /></javaModelGenerator>        <!-- targetProject:mapper映射文件生成的位置 --><sqlMapGenerator targetPackage="com.e3mall.mapper"targetProject=".\src"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="false" /></sqlMapGenerator><!-- targetPackage:mapper接口生成的位置 --><javaClientGenerator type="XMLMAPPER"targetPackage="com.e3mall.mapper"targetProject=".\src"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="false" /></javaClientGenerator><!-- 指定数据库表 --><table schema="" tableName="tb_content"></table><table schema="" tableName="tb_content_category"></table><table schema="" tableName="tb_item"></table><table schema="" tableName="tb_item_cat"></table><table schema="" tableName="tb_item_desc"></table><table schema="" tableName="tb_item_param"></table><table schema="" tableName="tb_item_param_item"></table><table schema="" tableName="tb_order"></table><table schema="" tableName="tb_order_item"></table><table schema="" tableName="tb_order_shipping"></table><table schema="" tableName="tb_user"></table></context></generatorConfiguration>


4.直接执行Generator类的main方法。执行的效果如下:



这样就生成了对应的 pojo,mapper.xml和mapper接口。我们再将它们拷贝到我们的项目中使用,这样我们就省去了写了很多代码。




原创粉丝点击