mybaits逆向一招鲜

发布于:2024-06-22 ⋅ 阅读:(75) ⋅ 点赞:(0)

mybaits逆向一招鲜

一、maven坐标

<!-- MyBatis Generator -->
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.4.0</version>
</dependency>
  <!--数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.27</version>
        </dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.10</version>
</dependency>

二、generatorConfig.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="MySQLTables" targetRuntime="MyBatis3">
        <!-- 配置生成注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="false"/>
            <property name="addRemarkComments" value="true"/>
        </commentGenerator>

        <!-- 配置数据库连接 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/work10"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <!-- 配置 Java Model 生成策略 -->
        <javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 配置 SQL Map XML 文件生成策略 -->
        <sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 配置 Java Client 生成策略 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- 配置表及生成策略 -->
        <table tableName="tb_cls"/>
    </context>
</generatorConfiguration>




三、MyBatisGeneratorMain

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;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MyBatisGeneratorMain {
    public static void main(String[] args) {
        try {
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            File configFile = new File("src/main/resources/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);
            for (String warning : warnings) {
                System.out.println(warning);
            }
            System.out.println("代码生成成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

四、生成的接口案例解释

long countByExample(TbClsExample example)

  • 作用:根据指定的查询条件 example 统计满足条件的记录数。
  • 返回值:满足查询条件的记录数。

int deleteByExample(TbClsExample example)

  • 作用:根据指定的查询条件 example 删除满足条件的记录。
  • 返回值:删除的记录数。

int deleteByPrimaryKey(Integer clsId)

  • 作用:根据主键 clsId 删除对应的记录。
  • 返回值:删除的记录数。

int insert(TbCls record)

  • 作用:插入一条新的记录,record 是待插入的记录对象,包含所有字段。
  • 返回值:插入的记录数。

int insertSelective(TbCls record)

  • 作用:选择性插入一条新的记录,仅插入 record 中非空的字段。
  • 返回值:插入的记录数。

List<TbCls> selectByExample(TbClsExample example)

  • 作用:根据指定的查询条件 example 查询满足条件的记录。
  • 返回值:满足查询条件的记录列表。

TbCls selectByPrimaryKey(Integer clsId)

  • 作用:根据主键 clsId 查询对应的记录。
  • 返回值:主键匹配的记录对象,如果没有找到则返回 null

int updateByExampleSelective(@Param("record") TbCls record, @Param("example") TbClsExample example)

  • 作用:根据指定的查询条件 example 选择性更新满足条件的记录,只更新 record 中非空的字段。
  • 返回值:更新的记录数。

int updateByExample(@Param("record") TbCls record, @Param("example") TbClsExample example)

  • 作用:根据指定的查询条件 example 更新满足条件的记录,更新 record 中所有字段。
  • 返回值:更新的记录数。

int updateByPrimaryKeySelective(TbCls record)

  • 作用:根据主键 clsId 选择性更新对应的记录,只更新 record 中非空的字段。
  • 返回值:更新的记录数。

int updateByPrimaryKey(TbCls record)

  • 作用:根据主键 clsId 更新对应的记录,更新 record 中所有字段。
  • 返回值:更新的记录数。

网站公告

今日签到

点亮在社区的每一天
去签到