服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - MyBatis动态创建表的实例代码

MyBatis动态创建表的实例代码

2021-05-14 11:06Joanna.Yan Java教程

在项目需求中,我们经常会遇到动态操作数据表的需求,常见的我们会把日志、设备实时位置信息等存入数据表,并且以一定时间段生成一个表来存储。接下来通过本文给大家介绍MyBatis动态创建表的方法,感兴趣的朋友一起看看吧

项目中业务需求的不同,有时候我们需要动态操作数据表(如:动态建表、操作表字段等)。常见的我们会把日志、设备实时位置信息等存入数据表,并且以一定时间段生成一个表来存储,log_201806、log_201807等。在这里我们用mybatis实现,会用到动态sql。

动态sql是mybatis的强大特性之一,mybatis在对sql语句进行预编译之前,会对sql进行动态解析,解析为一个boundsql对象,也是在此对动态sql进行处理。

在动态sql解析过程中,#{ }与${ }的效果是不一样的:

#{ } 解析为一个jdbc预编译语句(prepared statement)的参数标记符。

如以下sql语句:

?
1
select * from user where name = #{name};

会被解析为:

?
1
select * from user where name = ?;

可以看到#{ }被解析为一个参数占位符 ? 。

${ } 仅仅为一个纯粹的string替换,在动态sql解析阶段将会进行变量替换。

如以下sql语句:

?
1
select * from user where name = ${name};

当我们传递参数“joanna”时,sql会解析为:

?
1
select * from user where name = “joanna”;

可以看到预编译之前的sql语句已经不包含变量name了。

综上所述,${ }的变量的替换阶段是在动态sql解析阶段,而#{ } 的变量的替换是在dbms中。

下面实现mybatis动态创建表,判断表是否存在,删除表功能。

mapper.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?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="xx.xxx.xx.mapper.operatetablemapper" >
  <select id="existtable" parametertype="string" resulttype="integer">
    select count(*)
    from information_schema.tables
    where lcase(table_name)=#{tablename}
  </select>
  <update id="droptable">
    drop table if exists ${tablename}
  </update>
  <update id="createnewtable" parametertype="string">
    create table ${tablename} (
     id bigint(20) not null auto_increment,
     entityid bigint(20) not null,
     dx double not null,
     dy double not null,
     dz double not null,
     ntype varchar(32) not null,
     gnsstime bigint(20) not null,
     speed float default null,
     direction float default null,
     attributes varchar(255) default null,
     primary key (id))
  </update>
  <insert id="insert" parametertype="xx.xxx.xx.po.trackpoint">
    insert into ${tablename}
    (entityid,dx,dy,dz,ntype,gnsstime,speed,direction,attributes)
    values
    (#{trackpoint.entityid},
    #{trackpoint.dx},
    #{trackpoint.dy},
    #{trackpoint.dz},
    #{trackpoint.ntype},
    #{trackpoint.gnsstime},
    #{trackpoint.speed},
    #{trackpoint.direction},
    #{trackpoint.attributes})
  </insert>
</mapper>

mapper.java

package xx.xxx.xx.mapper;

?
1
2
3
4
5
6
7
8
import org.apache.ibatis.annotations.param;
import xx.xxx.xx.po.trackpoint;
public interface operatetablemapper {
  int existtable(string tablename);
  int droptable(@param("tablename")string tablename);
  int createnewtable(@param("tablename")string tablename);
  int insert(@param("tablename")string tablename,@param("trackpoint")trackpoint trackpoint);
}

总结

以上所述是小编给大家介绍的mybatis动态创建表的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://www.cnblogs.com/Joanna-Yan/p/9187538.html

延伸 · 阅读

精彩推荐