记一次设计pojo生成工具出现的问题-mysql 获取数据库表的的所有列属性以及其他信息

最近在做一个pojo,dao生成工具,了解了一下mysql相关的东西,这里做个总结:

获取某个库指定表的列的属性:

select COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT from information_schema.COLUMNS where table_name = '表名' and table_schema = '数据库名';

数据库的查询结果:

mybatis的mapper实现:

	<select id="listTableColumns" resultType="com.arror.code_factory.model.TableColumnDO">

		select COLUMN_NAME as
		columnName,DATA_TYPE as dataType,COLUMN_COMMENT as columnComment from
		information_schema.`COLUMNS` where table_name = #{tableName} and
		TABLE_SCHEMA=#{dataBase}
	</select>

这样一次只能查询一个表的属性,我试着改了一下sql改成in查询,查询一个数据库 里面的指定数据库的列属性:

select COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT from information_schema.COLUMNS where table_name in ('gy_user','mqtt_acl') and table_schema = 'gy_account';

最后却是这样的结果,这样的结果对于mybais不是很好处理。然后放弃了这种方式。

工具实现的效果:

可以同时选择我们需要的任何表,任何 库前端选什么我们后端就造什么数据的pojo,baseDao。

这样会存在一个问题,数据库的连接问题,当连接不同的库的时候会需要不同的连接,对于这个问题,有两种解决方式

1.直接初始化多个数据库连接,需要哪个用哪一个

2.将数据库连接写在业务层,调用一次创建一次。

第一种方式是最好的,

一开始我采用的是第二个方式,采用原生的mybatis去连接数据库

package com.arror.code_factory.mybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * @Description
 * @Author DJZ-WWS
 * @Date 2019/5/31 8:54
 */
public class GetSessionFactory {
    private static SqlSessionFactory sqlSessionFactory;
    private GetSessionFactory(){

    }

    synchronized public static SqlSessionFactory getSqlSessionFactory(String resources){
        if(sqlSessionFactory==null){
            InputStream inputStream=null;
            try {
                inputStream= Resources.getResourceAsStream(resources);
            }catch (Exception e){
                e.printStackTrace();
            }
            sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        }
        return sqlSessionFactory;

    }
}

代码如上面,根据不同的库我去加载不同的配置文件,当然mybatis支持多数据源的,这里没有去这么实现

考虑到数据库连接,这种情况一般一个资源就够了,我暂时使用了hashmap做缓存(后期优化使用Guava的本地缓存技术实现),

 private SqlSessionFactory getSqlSessionFactory(String dataName) {
        //在这里使用缓存优化性能
        if(cacheMap.get(dataName)!=null){
            return  cacheMap.get(dataName);
        }
        SqlSessionFactory    sqlSessionFactory=null;
        if(dataName.equals(DataBaseEnum.GY_ACCOUNT.getDataBase())){
            String  resources="mybatisconfig/gy_account.xml";
            sqlSessionFactory  = GetSessionFactory.getSqlSessionFactory(resources);
            cacheMap.put("gy_account",sqlSessionFactory);
        }
        if(dataName.equals(DataBaseEnum.GY_PROJECT.getDataBase())){
            String  resources="mybatisconfig/gy_project.xml";
            sqlSessionFactory=GetSessionFactory.getSqlSessionFactory(resources);
            cacheMap.put("gy_account",sqlSessionFactory);
        }
        if(dataName.equals(DataBaseEnum.GY_CORE.getDataBase())){
            String  resources="mybatisconfig/gy_core.xml";
            sqlSessionFactory=GetSessionFactory.getSqlSessionFactory(resources);
            cacheMap.put("gy_account",sqlSessionFactory);
        }
        if(dataName.equals(DataBaseEnum.GY_MODEL.getDataBase())){
            String  resources="mybatisconfig/gy_model.xml";
            sqlSessionFactory=GetSessionFactory.getSqlSessionFactory(resources);
            cacheMap.put("gy_account",sqlSessionFactory);
        }

        return sqlSessionFactory;
    }

这样就暂时保证了不会重复创建多余的资源。

一些关于系统路径问题:

System.getProperty("user.home")   获取用户系统的用户目录  我的是  C:\Users\DELL

System.getProperty("user.dir")   获取项目部署后tomcat所在的bin目录下,

还有一处优化,原本我采用的是双层for循环,为了减少数据的查询压力使用了map,将双重的循环转成了一层数据库查询循环

public void handler(List<SQLParam>  sqlParams) {
		String baseFile = System.getProperty("user.home") + File.separator
				+ "code" + File.separator;
       //表名数据库map   减少创建数据库连接次数
		Map<String,String>  tableMap=new HashMap<>();
		sqlParams.forEach(e->{
			e.getTableNames().forEach(t->{
				tableMap.put(t,e.getDataName());
			});
		});

		for (Map.Entry<String,String> entry:tableMap.entrySet()){
			createPoJoHandler.createPojo(entry.getKey(), baseFile,entry.getValue());
			System.out.println("Pojo生成完毕");
			createDaoHandler.createDao(entry.getKey(), baseFile,entry.getValue());
			System.out.println("DAO生成完毕");
			createServiceHandler.createService(entry.getKey(), baseFile,entry.getValue());
			System.out.println("SERVICE生成完毕");
			createServiceImplHandler.createServiceImpl(entry.getKey(), baseFile,entry.getValue());
			System.out.println("SERVICEIMPL生成完毕");
			createXmlHandler.createXml(entry.getKey(), baseFile,entry.getValue());
			System.out.println("XML生成完毕");
		}



	}

还有一个问题:数据库连接问题

  <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://192.168.111.111:3306/gy_xxx?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf8&amp;zeroDateTimeBehavior=convertToNull&amp;autoReconnect=true" />
                <property name="username" value="xxxn" />
                <property name="password" value="xxx" />
            </dataSource>
        </environment>
    </environments>

没有使用properties,在URL后面拼接条件,会提示错误信息

解决办法:将&进行转义换成

&amp;

工具还会继续更新。。。。。。

猜你喜欢

转载自blog.csdn.net/qq_35410620/article/details/90720188