Mysql + mybatis Dynamic Build table

Source: Internet
Author: User
Tags uuid

Service tier Business

Package Com.zx.common.service.impl;import Com.zx.common.entity.sysuser;import Com.zx.common.mapper.SysUserMapper; Import Com.zx.common.service.sysuserservice;import Com.zx.common.util.dateutil;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.service;import Org.springframework.transaction.annotation.transactional;import java.util.list;import Java.util.UUID; @Service (" Sysuserservice ") public class Sysuserserviceimpl implements Sysuserservice {@Autowired private sysusermapper Sysuser    Mapper;    @Override public list<sysuser> Selectalluser () {return sysusermapper.selectalluser (); } @Transactional (rollbackfor = {Exception.class}) @Override public void Updateuserinfotimer () {//based on the date of the previous day        Rename Sys_user to Sys_user_ on Day date String beforedatestr = Dateutil.getbeforedatestring ();        String rename = "Sys_user_" + Beforedatestr + uuid.randomuuid (). toString (). substring (0,16); Sysusermapper.renametable(Rename.replace ("-", ""));    New Sys_user table sysusermapper.createtable (); }}

Mapper Layer Interface

Package Com.zx.common.mapper;import Com.zx.common.entity.sysuser;import Org.apache.ibatis.annotations.Param; Import Java.util.list;public interface Sysusermapper {    list<sysuser> selectalluser ();    void Renametable (@Param ("TableName") String tableName);    void CreateTable ();}

Mapper.xml

<?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= "Com.zx.common.mapper.SysUserMapper" > <resultmap id= "baseresultmap" type= " Com.zx.common.entity.SysUser "> <id column=" id "property=" id "jdbctype=" INTEGER "/> <result column=" USER NAME "property=" username "jdbctype=" varchar "/> <result column=" PASSWORD "property=" PASSWORD "jdbctype=" varchar "/> </resultMap> <sql id=" base_column_list "> ID, USERNAME, PASSWORD </sql> <update id=" re  NameTable "parametertype=" java.lang.String "> Rename table Sys_user to ${tablename}; </update> <update id= "createtable" > CREATE TABLE sys_user (ID INT (one) not NULL auto_increment COMM  ENT ' primary key ', USERNAME varchar (+) NOT null default ' ', PASSWORD varchar (+) NOT null default ', PRIMARY Key (ID), key I_sys_user_userNAME (USERNAME)) Engine=innodb DEFAULT charset=utf8 comment= ' user table '; </update></mapper>

  

Mysql + mybatis Dynamic Build table

Related Article

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.