Project training record (seven) - mybatis generation table

Table of contents

1. What have you done recently?

2. Problems and implementation

1. Problem

2. Get it from the backend and store it in the map

3. Call the service method to create a table

4. Add another JDBC implementation

1. What have you done recently?

It's still work in May,

At the end of May, the system functions were still being improved.

The first is the bug problem on the client side - it has been resolved

Then there is the table creation part on the administrator side - although it is implemented using JDBC later. But it was first implemented using mybatis.

Because the table name, field name, and field type passed from the front end are all of String type, the database table must be generated at the end. Document this process.
 

2. Problems and implementation

1. Problem

The front end posts the field name and field type to the back end through axios.

code show as below:

    click3(){
        //这里就直接复制粘贴即可
        this.submitForm3();
          this.dialogVisible=true
      },
         submitForm3() {
       this.columnList.forEach((item,index) => {
         let params = new URLSearchParams()
         params.append('DBid',this.dbid)
         params.append('CHARTid',this.chartid)
         params.append('MEN',this.columnList[index])
         params.append('Types',this.aboutColumns.types[index])
         params.append('ZDes',this.aboutColumns.ZDescrs[index])
         params.append('ZN',this.aboutColumns.ZNames[index])
         axios.post(`api/insertMeta`,params).then(res=>{
           console.log('表的详细信息第 '+index+' 列已经发出去了')
           console.log(res.data)
         })
});
     //这里可以查表名,也能根据查表有哪些列和类型
        //创建实体表
      let params1 = new URLSearchParams()
      params1.append('DBid',this.dbid)
      params1.append('CHARTid',this.chartid)
      params1.append('EN',this.biaoF.Ename)
      axios.post(`api/createUTable`,params1).then(res=>{
         console.log('已发出实体表相关信息')
         console.log(res.data)
         })
    
       
       
    },

2. Get it from the backend and store it in the map

The code is as follows (example):

List<Meta> list = metaService.findMetasByCId(chartid,dbid);

        if (list.size()==0){
            System.out.println(("该实体表的字段信息为空!无法创建实体表!"));
        }

        Map<String, String> mymap = new HashMap<>(); //使用map的key,value进行存值,key:"存的是field" ,value:"存储的是type"
        for (Meta metas : list) {
            String type = metas.getCN();
            String field = metas.getEN();
            mymap.put(field, type);//遍历template中的值将其分别存储到key,value中
        }

3. Call the service method to create a table

The final corresponding mapper.xml file code is as follows:

  <update id="createUTable" parameterType="map">
        create table ${name} (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        <foreach collection="mymap" index="key"  item="value" separator=",">
            ${key} ${value}(255)
        </foreach>
        ,PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    </update>

4. Add another JDBC implementation

The implementation code in the service layer is as follows:

 @Override
    public int createUTable(String cName, Map<String, String> mymap,String dName) throws SQLException {

        String url="jdbc:mysql://localhost:3306/?allowPublicKeyRetrieval=true&useSSL=false";
        String user="root";
        String pw="xxxx";
        Connection con = DriverManager.getConnection(url,user,pw);//获得数据库链接对象
        con.prepareStatement("USE "+dName).execute();//使用test 表
        PreparedStatement  ps=con.prepareStatement("SHOW TABLES LIKE \""+cName+"\"");
        ResultSet rs=ps.executeQuery();
        if(rs.next())
            System.out.println("表已存在");
        else {
            String sql="CREATE TABLE "+cName+"(id INT(11) PRIMARY KEY,";
//            这里应该依次遍历map
            for (String key : mymap.keySet()) {
                System.out.println("key= "+ key + " and value= " + mymap.get(key));
                sql+=key +" "+mymap.get(key)+"(10), ";
            }

            sql=sql.substring(0,sql.length()-2);
            sql+=")";
            System.out.println(sql);
            ps = con.prepareStatement(sql);
            ps.executeUpdate();
            System.out.println("表创建成功");

        }
        return 0;
    }

Guess you like

Origin blog.csdn.net/pinkray_c/article/details/125111756