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>

	<!-- 抽取出来的公共部分 -->
	<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" item="fields" separator=",">
				<include refid="commonSql"></include>
				<if test="fields.fieldIsKey">
					,PRIMARY KEY (`${fields.fieldName}`)
				</if>
			</foreach>
			);
		</foreach>

	</select>
	
	<!-- 验证表是否存在 -->
	<select id="findTableCountByTableName" resultType="int" parameterType="String">
		select count(1) 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="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