mybatis学习(40):逆向工程的创建

简介: mybatis学习(40):逆向工程的创建

image.png

package com.geyao.generator;
 import java.io.File;
 import java.util.*;
 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[] args) throws Exception {
         List<String> warnings = new ArrayList<String>();
         boolean overwrite = true;
         // 指定配置文件
         File configFile = new File("src/generator.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);
     }
 }
 配置文件generator.xml放在配置src文件下
<?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>
     <!-- Mysql数据库连接的信息:驱动类、连接地址、用户名、密码 -->
     <jdbcConnection driverClass="com.mysql.jdbc.Driver"
         connectionURL="jdbc:mysql://localhost:3306/blog_gp1701?serverTimeznotallow=GMT%2B8"
         userId="root"
         password="123">
     </jdbcConnection>
     <!-- Oracle数据库
         <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
             connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
             userId="yycg"
             password="yycg">
         </jdbcConnection> 
     -->
     <!-- 默认为false,把JDBC DECIMAL 和NUMERIC类型解析为Integer,为true时
     把JDBC DECIMAL 和NUMERIC类型解析为java.math.BigDecimal -->
     <javaTypeResolver >
         <property name="forceBigDecimals" value="false" />
     </javaTypeResolver>
     <!-- targetProject:生成POJO类的位置 -->
     <javaModelGenerator targetPackage="com.geyao.mybatis.pojo" targetProject=".\src">
         <!-- enableSubPackages:是否让schema作为包的后缀 -->
         <property name="enableSubPackages" value="false" />
         <!-- 从数据库返回的值被清理前后的空格 -->
         <property name="trimStrings" value="true" />
     </javaModelGenerator>
     <!-- targetProject:mapper映射文件生成的位置 -->
     <sqlMapGenerator targetPackage="com.geyao.mybatis.mapper"  targetProject=".\src">
         <!-- enableSubPackages:是否让schema作为包的后缀 -->
         <property name="enableSubPackages" value="false" />
     </sqlMapGenerator>
     <!-- targetProject:mapper接口生成的的位置 -->
     <javaClientGenerator type="XMLMAPPER" targetPackage="com.geyao.mybatis.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>
     -->
     <table schema="" tableName="blog"></table>
     <!-- 有些表的字段需要指定java类型 
     <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
       <property name="useActualColumnNames" value="true"/>
       <generatedKey column="ID" sqlStatement="DB2" identity="true" />
       <columnOverride column="DATE_FIELD" property="startDate" />
       <ignoreColumn column="FRED" />
       <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
     </table> -->
   </context>
 </generatorConfiguration>

image.pngimage.png

相关文章
|
2月前
|
XML Java 数据库连接
mybatis-plus逆向工程详解
mybatis-plus逆向工程详解
103 0
|
14天前
|
SQL Java 数据库连接
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
|
2月前
|
SQL XML Java
学习Mybatis相关知识
一、什么是Mybatis? 1、Mybatis是一个半ORM(对象关系映射)框架,它内部封装了JDBC,开发时只需要关注SQL语句本身,不需要花费精力去处理加载驱动、创建连接、创建statement等繁杂的过程。开发人员直接编写原生态sql,即可严格控制sql执行性能、且灵活度高。
13 0
|
2月前
|
XML Java 数据库连接
Mybatis-Plus学习小项目及详细教程
Mybatis-Plus学习小项目及详细教程
|
4月前
|
Java 数据库连接 Maven
MyBatis逆向工程可以生成哪些内容?
MyBatis逆向工程可以生成哪些内容?
21 0
|
4月前
|
SQL 缓存 Java
Mybatis学习文章
Mybatis学习文章
|
4月前
|
SQL Java 数据库连接
MyBatis之逆向工程
【1月更文挑战第4天】 正向工程:先创建Java实体类,由框架负责根据实体类生成数据库表。Hibernate是支持正向工程的 逆向工程:先创建数据库表,由框架负责根据数据库表,反向生成如下资源: Java实体类 Mapper接口 Mapper映射文件
34 2
|
4月前
|
SQL Java 数据库连接
还在为学MyBatis发愁?史上最全,一篇文章带你学习MyBatis
还在为学MyBatis发愁?史上最全,一篇文章带你学习MyBatis
|
4月前
|
SQL Java 数据库连接
MyBatis的逆向工程
MyBatis的逆向工程
|
5月前
|
XML SQL Java
今日记录:学习一个Mybatis的技能之choose 和 bind
今日记录:学习一个Mybatis的技能之choose 和 bind
26 1