p平常写sql语句基本都是DML的CURD,很少有DML的操作,刚实习的时候有一个项目里面就有一个功能是点击查询的时候自动把查询的结果在数据库里面创建成一个表,当时觉得好难,了解了DDL和mybatis之后实现起来简单的功能还是蛮简单的:
mapper文件`
<mapper namespace="edu.hrbeu.platform.modeling.table.dao.TableMapper">
<!-- 查看表是否存在 -->
<select id="existTable" resultType="Integer">
SELECT count(TABLE_NAME)
FROM
INFORMATION_SCHEMA.TABLES
where
TABLE_SCHEMA = #{databaseName, jdbcType=VARCHAR}
AND
TABLE_NAME=#{tableName, jdbcType=VARCHAR}
</select>
<!-- 获取当前表的创建表语句和表名称 -->
<select id="showCretaTable" resultType="java.util.Map" parameterType="java.lang.String">
show create table ${value}
</select>
<!-- 删除表 update -->
<update id="dropTable" parameterType="java.lang.String">
drop table IF EXISTS ${value}
</update>
<!--创建表 update -->
<update id="createNewTable" parameterType="java.lang.String">
${value}
</update>
<update id="createNewTable" parameterType="String">
create table ${tableName} (
id int not null generated by default as identity,
author_id int not null,
title varchar(255),
primary key (id))
</update>
<update id="alterTable">
alter table ${tableName} allocate extent
</update>
<update id="truncateTable">
truncate table ${tableName}
</update>
<update id="createNewTableAndInsertData">
create table ${newTableName} as select * from ${originalTableName}
</update>
还是挺简单的是不是,不过还是要注意两点:
第一点是:平时我们传入参数时使用#{param},如果是数据库名字的话必须使用${tablename}
第二点是:在dao即使是一个参数也要用@param(“”)注解
心动不如行动!!!!