zoukankan      html  css  js  c++  java
  • 11.12 Mybatis逆向工程

    11.12 Mybatis逆向工程

    Mybatis逆向工程的定义

    • 根据数据表自动生成针对单表bean类、mapper映射文件、mapper接口。

    使用Mybatis提供的逆向工具来实现

    操作步骤

    • 项目导入

    • 创建数据表

    • 创建项目

    • 执行自动生成代码


    项目导入

    porm.xml中导入项目:

    <denpendency>
    <groupId>org.mybatis.generaton</groupId>
       <artifactId>mybatis-generator-core</artifactId>
       <version>x.x.x</version>
    </denpendency>

    创建数据表

    • User

    • Student

    • StudentCard

    • Website


    user

    drop table if exists user;

    create table user(
       `id` int(11) not null auto_increment,
       `name` varchar(20) default null,
       `pwd` varchar(20) default null,
       primary key (`id`)
    )engine = InnoDB auto_increment = 7 default charset = utf8;

    student

    drop table if exists student;

    create table student(
       `id` int(11) not null auto_increment,
       `name` varchar(20) character set utf8 collate utf8_unicode_ci default null,
       `sex` tinyint(4) default null,
       `cardId` int(20) default null,
       primary key (`id`),
       key `cardId` (`cardId`),
       constraint `student_ibfk_1` foreign key (`cardId`) references `studentcard` (`id`)
    )engine = InnoDB auto_increment = 7 default charset = utf8;

    studentcard

    drop table if exists studentcard;

    create table studentcard(
       `id` int(20) not null auto_increment,
       `studentId` int(20) default null,
       `startDate` date default null,
       `endDate` date default null,
       primary key (`id`),
       key `studentId` (`studentId`)
    )engine = InnoDB auto_increment = 6 default charset = utf8;

    website

    drop table if exists website;

    create table website(
       `id` int(11) not null auto_increment,
       `name` varchar(20) collate utf8_unicode_ci not null,
       `url` varchar(20) collate utf8_unicode_ci default '',
       `age` tinyint(3) unsigned not null,
       `country` char(3) collate utf8_unicode_ci not null default '',
       `createtime` timestamp null default current_timestamp,
       primary key (`id`)
    )engine = InnoDB auto_increment = 9 default charset = utf8 collate = utf8_unicode_ci;

    创建项目

    新建文件夹config,在config下创建genertorConfig.xml文件,配置指定数据库及表

    <!-- 引用mybatis逆向工程的.dtd -->
    <?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/mybatistest"
               userId="root"
               password="root" />

           <!-- 默认为false,把JDBC DECIMAL 和NUMERIC类型解析为Integer,为true时 把JDBC DECIMAL 和NUMERIC类型解析为java.math.BigDecimal -->
           <javaTypeResolver>
               <property name="forceBigDecimals" value="false" />
           </javaTypeResolver>

           <!-- targetProject:生成POJO类的位置 -->
           <javaModelGenerator
               targetPackage="com.junkingboy.pojo" targetProject=".\src">
               <!-- enableSubPackages:是否让schema作为包的后缀 -->
               <property name="enableSubPackages" value="false" />
               <!-- 从数据库返回的值被清理前后的空格 -->
               <property name="trimStrings" value="true" />
           </javaModelGenerator>

           <!-- targetProject:mapper映射文件生成的位置 -->
           <sqlMapGenerator targetPackage="com.junkingboy.mapper"
               targetProject=".\src">
               <!-- enableSubPackages:是否让schema作为包的后缀 -->
               <property name="enableSubPackages" value="false" />
           </sqlMapGenerator>

           <!-- targetProject:mapper接口生成的的位置 -->
           <javaClientGenerator type="XMLMAPPER"
               targetPackage="com.junkingboy.mapper" targetProject=".\src">
               <!-- enableSubPackages:是否让schema作为包的后缀 -->
               <property name="enableSubPackages" value="false" />
           </javaClientGenerator>

           <!-- 指定数据表 -->
           <table tableName="website"></table>
           <table tableName="student"></table>
           <table tableName="studentcard"></table>
           <table tableName="user"></table>
       </context>

    </generatorConfiguration>

    执行生成代码

    • pojo包中有一部分是名字为 XxxExample 的类。类中包含以下 3 个成员变量

      • protected String orderByClause;
        protected boolean distinct;
        protected List<Criteria> oredCriteria;
      • 属性说明:

        • distinct 字段用于指定 DISTINCT 查询。

        • orderByClause 字段用于指定 ORDER BY 条件,这个条件没有构造方法,直接通过传递字符串值指定。

        • oredCriteria 字段用于自定义查询条件。

    It's a lonely road!!!
  • 相关阅读:
    Unity 关于特效和UI显示的优先级问题
    使用Frida神器轻松实现hook C/C++方法
    理解 Android Binder 机制(三):Java层
    理解 Android Binder 机制(二):C++层
    理解 Android Binder 机制(一):驱动篇
    Android Hook Instrumentation
    Cocos Creator 中根据uuid快速定位资源
    android 通用混淆配置
    vToRay + bbr 加速
    SpringBoot项目单元测试
  • 原文地址:https://www.cnblogs.com/JunkingBoy/p/15543481.html
Copyright © 2011-2022 走看看