All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.sunchenbin.mybatis.actable.mapping.system.CreateMysqlTablesMapper.xml Maven / Gradle / Ivy

Go to download

A.CTable is a Maven project based on Spring and Mybatis, which enhances the function of Mybatis

There is a newer version: 1.5.0.RELEASE
Show newest version
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.gitee.sunchenbin.mybatis.actable.dao.system.CreateMysqlTablesMapper">

	<!-- 强制指定查询表结构映射字段,避免被mybatis下划线转驼峰配置影响,导致查询不到值  -->
	<resultMap id="SysMysqlColumnsResultMap" type="com.gitee.sunchenbin.mybatis.actable.command.SysMysqlColumns">
		<result property="table_schema" column="table_schema"/>
		<result property="table_name" column="table_name"/>
		<result property="column_name" column="column_name"/>
		<result property="ordinal_position" column="ordinal_position"/>
		<result property="column_default" column="column_default"/>
		<result property="is_nullable" column="is_nullable"/>
		<result property="data_type" column="data_type"/>
		<result property="character_maximum_length" column="character_maximum_length"/>
		<result property="character_octet_length" column="character_octet_length"/>
		<result property="numeric_precision" column="numeric_precision"/>
		<result property="numeric_scale" column="numeric_scale"/>
		<result property="character_set_name" column="character_set_name"/>
		<result property="collation_name" column="collation_name"/>
		<result property="column_type" column="column_type"/>
		<result property="column_key" column="column_key"/>
		<result property="extra" column="extra"/>
		<result property="privileges" column="privileges"/>
		<result property="column_comment" column="column_comment"/>
	</resultMap>

	<resultMap id="SysMysqlTableResultMap" type="com.gitee.sunchenbin.mybatis.actable.command.SysMysqlTable">
		<result property="table_catalog" column="table_catalog"/>
		<result property="table_schema" column="table_schema"/>
		<result property="table_name" column="table_name"/>
		<result property="table_type" column="table_type"/>
		<result property="engine" column="engine"/>
		<result property="version" column="version"/>
		<result property="row_format" column="row_format"/>
		<result property="table_rows" column="table_rows"/>
		<result property="avg_row_length" column="avg_row_length"/>
		<result property="data_length" column="data_length"/>
		<result property="max_data_length" column="max_data_length"/>
		<result property="index_length" column="index_length"/>
		<result property="data_free" column="data_free"/>
		<result property="auto_increment" column="auto_increment"/>
		<result property="create_time" column="create_time"/>
		<result property="update_time" column="update_time"/>
		<result property="check_time" column="check_time"/>
		<result property="table_collation" column="table_collation"/>
		<result property="checksum" column="checksum"/>
		<result property="create_options" column="create_options"/>
		<result property="table_comment" column="table_comment"/>
	</resultMap>

	<!-- 抽取出来的公共部分 -->
	<sql id="commonSql">
		<if test="fields.fileTypeLength == 0">
			`${fields.fieldName}` ${fields.fieldType}
		</if>
		<if test="fields.fileTypeLength == 1">
			`${fields.fieldName}` ${fields.fieldType}(${fields.fieldLength})
		</if>
		<if test="fields.fileTypeLength == 2">
			`${fields.fieldName}`
			${fields.fieldType}(${fields.fieldLength},${fields.fieldDecimalLength})
		</if>
		<if test="fields.fieldIsNull">
			NULL
		</if>
		<if test="!fields.fieldIsNull">
			NOT NULL
		</if>
		<if test="fields.fieldIsAutoIncrement">
			AUTO_INCREMENT
		</if>
		<!-- 不是自增长的才能有默认值 -->
		<!-- 并且不为null时,因为null是默认的没必要写 -->
		<if test="!fields.fieldIsAutoIncrement and !fields.fieldIsNull and fields.fieldDefaultValue != 'NULL'">
			<if test="fields.fieldType == 'bit'">
				<if test="fields.fieldDefaultValue == 'true'.toString()">
					DEFAULT 1
				</if>
				<if test="fields.fieldDefaultValue == 'false'.toString()">
					DEFAULT 0
				</if>
				<if test="fields.fieldDefaultValue == '1'.toString()">
					DEFAULT 1
				</if>
				<if test="fields.fieldDefaultValue == '0'.toString()">
					DEFAULT 0
				</if>
			</if>
			<if test="fields.fieldType != 'bit'">
				DEFAULT #{fields.fieldDefaultValue}
			</if>
		</if>
		<!-- 不是自增长的才能有默认值 -->
		<!-- 不是非空时,并且默认值不是NUll -->
		<if test="!fields.fieldIsAutoIncrement and fields.fieldIsNull and fields.fieldDefaultValue != 'NULL'">
			<if test="fields.fieldType == 'bit'">
				<if test="fields.fieldDefaultValue == 'true'.toString()">
					DEFAULT 1
				</if>
				<if test="fields.fieldDefaultValue == 'false'.toString()">
					DEFAULT 0
				</if>
				<if test="fields.fieldDefaultValue == '1'.toString()">
					DEFAULT 1
				</if>
				<if test="fields.fieldDefaultValue == '0'.toString()">
					DEFAULT 0
				</if>
			</if>
			<if test="fields.fieldType != 'bit'">
				DEFAULT #{fields.fieldDefaultValue}
			</if>
		</if>
		<if test="fields.fieldComment != ''">
			COMMENT #{fields.fieldComment}
		</if>
	</sql>

	<!-- 创建表的 -->
	<select id="createTable" parameterType="java.util.Map">

		<foreach collection="tableMap" index="key" item="value">
			create table `${key}`(
			<foreach collection="value.list" item="fields" separator=",">
				<include refid="commonSql"></include>
				<if test="fields.fieldIsKey">
					,PRIMARY KEY (`${fields.fieldName}`)
				</if>
			</foreach>
			)
			<foreach collection="value.map" index="ckey" item="cvalue">
				<if test="ckey == 'table_comment'">
					COMMENT = #{cvalue}
				</if>
			</foreach>
			;
		</foreach>

	</select>

	<!-- 验证表是否存在 -->
	<select id="findTableByTableName" resultMap="SysMysqlTableResultMap" parameterType="String">
		select * from information_schema.tables
		where table_name = #{tableName} and table_schema = (select database())
	</select>

	<!-- 根据表名查询表的结构 -->
	<select id="findTableEnsembleByTableName" resultMap="SysMysqlColumnsResultMap" parameterType="String">
		select * from information_schema.columns where table_name = #{tableName} and table_schema = (select database())
	</select>

	<!-- 增加字段 -->
	<select id="addTableField" parameterType="java.util.Map">

		<foreach collection="tableMap" index="key" item="fields" separator=";">
				alter table `${key}` add
				<include refid="commonSql"></include>
				<if test="fields.fieldIsKey">
					PRIMARY KEY
				</if>
		</foreach>

	</select>

	<!-- 删除字段 -->
	<select id="removeTableField" parameterType="java.util.Map">
		<foreach collection="tableMap" index="key" item="field" separator=";">
			alter table `${key}` drop `${field}`
		</foreach>
	</select>

	<!-- 更新表注释 -->
	<select id="modifyTableComment" parameterType="java.util.Map">
		<foreach collection="tableMap" index="key" item="value" separator=";">
			alter table `${key}` comment #{value}
		</foreach>
	</select>

	<!-- 修改字段 -->
	<select id="modifyTableField" parameterType="java.util.Map">

		<foreach collection="tableMap" index="key" item="fields" separator=";">
				alter table `${key}` modify
				<include refid="commonSql"></include>
				<if test="fields.fieldIsKey">
					PRIMARY KEY
				</if>
		</foreach>

	</select>


	<!-- 删除主键字段 -->
	<select id="dropKeyTableField" parameterType="java.util.Map">

		<foreach collection="tableMap" index="key" item="fields" separator=";">
				alter table `${key}` modify
				<include refid="commonSql"></include>
				,drop primary key
		</foreach>

	</select>

	<!-- 验证表是否存在 -->
	<select id="dropTableByName" parameterType="String">
		DROP TABLE IF EXISTS `${tableName}`;
	</select>

	<!-- 查询当前表存在的索引除了主键索引primary -->
	<select id="findTableIndexByTableName" resultType="String" parameterType="String">
		select index_name from information_schema.statistics
		where table_name = #{tableName} and table_schema = (select database()) and lower(index_name) !='primary' and index_name like 'actable_%'
	</select>

	<!-- 删除索引 -->
	<select id="dropTabelIndex" parameterType="java.util.Map">
		<foreach collection="tableMap" index="tableName" item="indexName">
				DROP INDEX `${indexName}` on `${tableName}`
		</foreach>
	</select>

	<!-- 创建索引 -->
	<select id="addTableIndex" parameterType="java.util.Map">

		<foreach collection="tableMap" index="tableName" item="fields" separator=";">
				CREATE INDEX ${fields.filedIndexName}
				ON `${tableName}` (
				<foreach collection="fields.filedIndexValue" index="index" item="columnName">
					<if test="fields.filedIndexValue.size == 1">
						${columnName}
					</if>
					<if test="fields.filedIndexValue.size > 1">
						${columnName}
						<if test="fields.filedIndexValue.size != index + 1">
						,
						</if>
					</if>
				</foreach>
				)
		</foreach>

	</select>

	<!-- 创建唯一约束 -->
	<select id="addTableUnique" parameterType="java.util.Map">

		<foreach collection="tableMap" index="tableName" item="fields" separator=";">
				CREATE UNIQUE INDEX ${fields.filedUniqueName}
				ON `${tableName}` (
				<foreach collection="fields.filedUniqueValue" index="index" item="columnName">
					<if test="fields.filedUniqueValue.size == 1">
						${columnName}
					</if>
					<if test="fields.filedUniqueValue.size > 1">
						${columnName}
						<if test="fields.filedUniqueValue.size != index + 1">
						,
						</if>
					</if>
				</foreach>
				)
		</foreach>

	</select>
</mapper>




© 2015 - 2024 Weber Informatics LLC | Privacy Policy