zoukankan      html  css  js  c++  java
  • 【转】MyBatis执行DDL:create table,drop table等等

    出处    https://www.cnblogs.com/zjrodger/p/5567085.html

    【前言】

      对MyBatis一直停留在仅仅会用的阶段,常用的场景就是通过MyBatis对表数据进行DML(insert, delete, update等)操作,从来没有想过通过MyBatis对数据库

    进行DDL(create, alter, drop)操作,最近的项目需要利用MyBatis对数据库进行DDL操作,自己就尝试了一把,对MyBatis又多了点儿了解。

    【具体代码】

    1、mapper接口文件内容如下。

    复制代码
    /**
     * 执行备份数据库相关表的Mapper
     */
    public interface BackupDataMapper {
        
        /**
         * 修改数据库的表名字
         * @param originalTableName
         * @param newTableName
         * @return
         */
        int alterTableName(@Param("originalTableName") String originalTableName,
                @Param("newTableName") String newTableName);
        
        /**
         * truncate指定数据库表的数据
         * @param tableName
         * @return
         */
        int truncateTable(@Param("tableName") String tableName);
    
        
        /**
         * 根据传入的表明,创建新的表并且将原表的数据插入到新的Occur表中
         * @param newTableName
         * @param originalTableName
         */
        void createNewTableAndInsertData(@Param("newTableName") String newTableName,
                @Param("originalTableName") String originalTableName);
        
        /**
         * 统计某张表中的总数据条数。
         * @param tableName
         * @return 指定表中的总记录条数。
         */
        int getRecordCount(@Param("tableName") String tableName);
        
        /**
         * 获得当前数据库的名字
         * @return
         */
        String getCurDataBaseName();
        
        /**
         * 从指定数据库中,查询是否存在某张表
         * @param dataBaseName
         * @param tableName
         * @return
         */
        String isTargetTableExistInDB(@Param("dataBaseName") String dataBaseName, 
                @Param("tableName") String tableName);
    }
    复制代码

    2、mapper.xml文件内容如下。

    复制代码
    <mapper namespace="com.zjrodger.dao.iface.BackupDataMapper">
    
        <update id="alterTableName">
            alter table ${originalTableName} rename ${newTableName}
        </update>
    
        <update id="truncateTable">
            truncate table ${tableName}
        </update>
        
        <update id="createNewTableAndInsertData">
            create table ${newTableName} as select * from ${originalTableName}
        </update>
        
        <select id="getRecordCount" resultType="int">
            select count(1) from ${tableName}
        </select>
        
        <select id="getCurDataBaseName" resultType="string">
            select database();
        </select>
        
        <select id="isTargetTableExistInDB" resultType="string">
            SELECT table_name FROM information_schema.tables WHERE table_schema = #{dataBaseName} and TABLE_NAME = #{tableName}
        </select>
    
    </mapper>
    复制代码

    3、注意点

      在传入“表名”作为参数时,一定要使用“${tableName}”的格式,而不能使用“#{tableName}”的格式。

  • 相关阅读:
    不要跟亲戚谈生意
    docker学习连接
    docker之docker-compose——容器管理
    docker之镜像管理——仓库使用
    docker之入门——安装及基本命令
    orcle数据库新建用户
    hibernate select 查询方式总结
    Hibernate 主配置文件详解
    ActionContext和ServletActionContext小结(struts2)
    理解Java常量池
  • 原文地址:https://www.cnblogs.com/telwanggs/p/13815428.html
Copyright © 2011-2022 走看看