mybatis3动态创建表及删除表

1.mybatis3动态创建表,判断表是否存在,删除表

mapper配置文件:

 

[html] view plain copy
 
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   
  3.    "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >  
  4.      
  5. <mapper namespace="com.doctor.mybatis3practice.mapper.BlogMapper" >  
  6.     <resultMap type="Blog" id="baseResultMap">  
  7.         <id property="id" column="id"/>  
  8.         <result property="authorId" column="author_id"/>  
  9.         <result property="title" column="title"/>  
  10.     </resultMap>  
  11.     <sql id="all_collum">  
  12.         id,author_id,title  
  13.     </sql>  
  14.       
  15.     <insert id="insertBlog" useGeneratedKeys="true" keyProperty="id">  
  16.         insert into blog ( author_id,title)  
  17.          values (#{authorId},#{title})  
  18.     </insert>  
  19.       
  20.     <select id="queryById" parameterType="Long" resultMap="baseResultMap">  
  21.         select <include refid="all_collum"/>  
  22.         from blog  
  23.         where id = #{id}  
  24.     </select>  
  25.       
  26.     <select id="existTable" parameterType="String" resultType="Integer">  
  27.         select count(1)  
  28.         from sys.systables  
  29.         where LCASE(tablename)=#{tableName}  
  30.     </select>  
  31.     <update id="dropTable">  
  32.         drop table ${tableName}     
  33.     </update>  
  34.       
  35.     <update id="createNewTable" parameterType="String">  
  36.         create table ${tableName} (  
  37.                             id int not null generated by default as identity,  
  38.                             author_id int not null,  
  39.                             title varchar(255),  
  40.                             primary key (id))  
  41.     </update>  
  42. </mapper></span>  


注意配置文件中的${tableName}  和#{tableName}的区别.

 

mapper接口如下:

 

[java] view plain copy
 
    1. <span style="font-size:18px;">package com.doctor.mybatis3practice.mapper;  
    2.   
    3. import org.apache.ibatis.annotations.Param;  
    4.   
    5. import com.doctor.mybatis3practice.domain.Blog;  
    6.   
    7. public interface BlogMapper {  
    8.     Blog queryById(Long id);  
    9.   
    10.     int insertBlog(Blog blog);  
    11.     int createNewTable(@Param("tableName") String tableName);  
    12.     int dropTable(@Param("tableName") String tableName);  
    13.     int existTable(String tableName);  
    14. }  
    15. </span>  
posted @ 2017-12-19 19:26  W&L  阅读(336)  评论(0编辑  收藏  举报