MYBATIS3 dynamically create tables, determine if tables exist, delete tables

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.