1. MYBATIS3 dynamically create tables, determine if tables exist, delete tables
Mapper configuration file:
<span style= "FONT-SIZE:18PX;" ><?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" > <map Per namespace= "Com.doctor.mybatis3practice.mapper.BlogMapper" ><resultmap type= "Blog" id= "Baseresultmap" > <id property= "id" column= "id"/><result property= "Authorid" column= "author_id"/><result property= " Title "column=" title "/></resultmap><sql id=" All_collum ">id,author_id,title</sql><insert Id= "Insertblog" usegeneratedkeys= "true" keyproperty= "id" >insert into blog (author_id,title) VALUES (#{authorid},#{ Title}) </insert><select id= "Querybyid" parametertype= "Long" resultmap= "Baseresultmap" >select < Include refid= "All_collum"/>from blogwhere id = #{id}</select><select id= "existtable" parameterType= " String "resulttype=" Integer ">select count (1) from Sys.systableswhere LCASE (tablename) =#{tablename}</select> <update id= "droptable" >drop table ${tablename} </update><update id= "createnewtable" parameterType= " String ">create table ${tablename} (ID int not NULL generated by default as IDENTITY,AUTHOR_ID int. not Null,title Varcha R (255), primary key (ID)) </update></mapper></span>
Note the difference between ${tablename} and #{tablename} in the configuration file .
The Mapper interface is as follows:
<span style= "FONT-SIZE:18PX;" >package Com.doctor.mybatis3practice.mapper;import Org.apache.ibatis.annotations.param;import Com.doctor.mybatis3practice.domain.blog;public interface Blogmapper {blog Querybyid (Long id); int insertblog (Blog blog ); int createnewtable (@Param ("TableName") string tableName); int droptable (@Param ("TableName") string tableName); int Existtable (String tableName);} </span>
Note
<span style= "FONT-SIZE:18PX;" > @Param ("TableName") annotations. </span>
MYBATIS3 dynamically create tables, determine if tables exist, delete tables