About mybatis related operations to dynamically create database tables

Let me talk about problems I encountered in this project

Development is the custom forms feature is the first time I encountered a similar problem in need: the need for a dynamic page design user in a form created in the database according to the corresponding database table

Design page is as follows: (I wrote the front page sucks, everyone light spray)















We only know the following design section can customize the column names and types on it

Then the background frame with a mybatis, this demand is not met, then I really did not find mybatis not related to construction of the table function

Then in the development of all kinds of fancy error very hard to accept 

Then through a series of tests to find out if the direct write complete SQL statement can be run successfully

This table is built

And it can only update the label under normal operation !! select insert delete sql can not be studied this source code to see why in the end


But this is just to create a dead table is not what we want then I try this

  <update   id="createDemo">
  		CREATE TABLE ${tableName} (
    		id int primary key auto_increment,
    		applicant varchar(10) not null,
    		createTime timestamp ,
       		isdel tinyint not null
      )character set utf8 collate utf8_bin;
  </update>

The test results are possible explanation table can be dynamically inserted then the field names and field types do I put this into sql

  <update   id="createDemo">
  		CREATE TABLE ${tableName} (
    		
    		${filedName} varchar(10) not null
    		
      )character set utf8 collate utf8_bin;
  </update>

The test results are possible


That coupled with the type if I do?

<update   id="createDemo">
  		CREATE TABLE ${tableName} (
    		
    		${filedName} ${type}(10) not null
    		
      )character set utf8 collate utf8_bin;
  </update>

The test results also can not be made of


After some time I test the first version of sql like this

  <update id="createTable" statementType="STATEMENT" parameterType="map">  
    CREATE TABLE ${tableName}  (
    		id int primary key auto_increment,
    		applicant varchar(10) not null,
    		createTime timestamp ,
       		isdel tinyint not null,
       <foreach collection="list" item="flowtable" separator="," >
       
       	<choose>
       		<when test="flowtable.mycolumn != null and flowtable.mycolumn !=''">${flowtable.mycolumn}</when>
       		<otherwise>${flowtable.id}</otherwise>
       	</choose>
       		 ${flowtable.texttype}<if test="flowtable.max != null">(${flowtable.max})</if>  <!-- <if test="flowtable.isnull != 0">not null</if> -->
       </foreach>
      )character set utf8 collate utf8_bin;
  </update>

Is available later found to statementType = "STATEMENT" may also be used to remove

I do not know without it statementType = "STATEMENT" Why pre-test is not used but then the time to write a blog but it may be too anxious, but where the problem while testing it

If you have friends experience the same problem can add statementType = "STATEMENT" Try

Finally, a very simple function but I spent a lot of effort just finished out of mind collapse wish to encounter similar functions friends more carefully




Published 16 original articles · won praise 21 · views 30000 +

Guess you like

Origin blog.csdn.net/q690080900/article/details/76228208