Skip to content

Commit

Permalink
update: 修改取数据逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyuan_deng committed Nov 9, 2018
1 parent 59c0a36 commit d1f4ca3
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.mustfun.mybatis.plugin.service;

import com.github.mustfun.mybatis.plugin.model.DbSourcePo;
import com.github.mustfun.mybatis.plugin.model.PluginConfig;
import com.github.mustfun.mybatis.plugin.model.Template;

Expand Down Expand Up @@ -142,4 +143,53 @@ public PluginConfig queryPluginConfigByKey(String key){
return null;
}

public DbSourcePo queryLatestConnectLog() {
try {
String sql = "select\n" +
" id,db_name,db_address,user_name,password\n" +
" from db_source order by create_time desc limit 1";

ResultSet rs = statement.executeQuery(sql);
DbSourcePo dbSource = new DbSourcePo();
while (rs.next()) {
dbSource.setId(rs.getInt("id"));
dbSource.setDbName(rs.getString("db_name"));
dbSource.setDbAddress(rs.getString("db_address"));
dbSource.setUserName(rs.getString("user_name"));
dbSource.setPassword(rs.getString("password"));
}
return dbSource;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}

/**
* 新增数据库连接历史 , 还可以做个模糊匹配
* @param dbSourcePo
* @return
*/
public boolean insertDbConnectionInfo(DbSourcePo dbSourcePo) {
try {
String sql = "select max(id) from db_source";
ResultSet rs = statement.executeQuery(sql);
int id = 1;
if (rs.getRow()!=0){
id = rs.getInt("id");
//只存储最近30条历史记录
if (id/30==0){
System.out.println("id小于"+id+"删除了历史记录了");
String deleteSql = "delete from db_source where id<"+id;
statement.executeUpdate(deleteSql);
}
}
id++;
statement.executeUpdate("insert into db_source(id,db_name,db_address,user_name,password) values("+id+",'"+dbSourcePo.getDbName()+"','"+dbSourcePo.getDbAddress()+"','"+dbSourcePo.getUserName()+"','"+dbSourcePo.getPassword()+"')");
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public DialogWrapperPanel getCommonDialog(){
Connection sqlLiteConnection = dbService.getSqlLiteConnection();
ConnectionHolder.addConnection(MybatisConstants.SQL_LITE_CONNECTION,sqlLiteConnection);
SqlLiteService sqlLiteService = SqlLiteService.getInstance(sqlLiteConnection);
//插入连接数据库的信息
sqlLiteService.insertDbConnectionInfo(dbSourcePo);

List<Template> templates = sqlLiteService.queryTemplateList();
CheckBoxList<Integer> templateCheckbox = connectDbSetting.getTemplateCheckbox();
for (Template template : templates) {
Expand Down Expand Up @@ -192,6 +195,10 @@ public DialogWrapperPanel getCommonDialog(){


private void fillPanelText(ConnectDbSetting connectDbSetting) {
//优先从历史记录里面读取
if(readFromConnectLog(connectDbSetting)){
return ;
}
VirtualFile baseDir = project.getBaseDir();
VirtualFile file = JavaUtils.getFileByPattenName(baseDir, "application.properties","application-dev.properties","application.yml","application-dev.yml");
if(file==null){
Expand All @@ -206,6 +213,26 @@ private void fillPanelText(ConnectDbSetting connectDbSetting) {
}
}

/**
* 从数据库中读取最近一条历史记录
* @param connectDbSetting
* @return
*/
private boolean readFromConnectLog(ConnectDbSetting connectDbSetting) {
Connection sqlLiteConnection = DbService.getInstance(project).getSqlLiteConnection();
SqlLiteService sqlLiteService = SqlLiteService.getInstance(sqlLiteConnection);
DbSourcePo dbSourcePo = sqlLiteService.queryLatestConnectLog();
if (dbSourcePo!=null){
connectDbSetting.getDbName().setText(dbSourcePo.getDbName());
connectDbSetting.getAddress().setText(dbSourcePo.getDbAddress());
connectDbSetting.getUserName().setText(dbSourcePo.getUserName());
connectDbSetting.getPassword().setText(dbSourcePo.getPassword());
return true;
}else{
return false;
}
}

private void insertPanelUseProperties(ConnectDbSetting connectDbSetting, File file) {
//读取properties类型文件
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package com.github.mustfun.mybatis.plugin.util;

import com.intellij.ide.plugins.PluginManager;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.psi.util.ReferenceSetBase;

/**
* @author yanglin
* @author itar
*/
public final class MybatisConstants {

public static final String TEMP_DIR_PATH = PathManager.getPluginsPath() + "/temp";
public static final String PLUGIN_NAME = "MyBatis Plugin Free";

public static final String TEMP_DIR_PATH = PathManager.getPluginsPath()+"/"+ PLUGIN_NAME + "/temp";

public static final String SQL_LITE_CONNECTION = "sqlLiteConnection";
public static final String MYSQL_DB_CONNECTION = "mysqlDbConnection";
public static final String MYSQL_DB_CONNECTION = "mysqlDbConnection";

private MybatisConstants() {
throw new UnsupportedOperationException();
}
private MybatisConstants() {
throw new UnsupportedOperationException();
}

public static final String DOT_SEPARATOR = String.valueOf(ReferenceSetBase.DOT_SEPARATOR);
public static final String DOT_SEPARATOR = String.valueOf(ReferenceSetBase.DOT_SEPARATOR);

public static final double PRIORITY = 400.0;
public static final double PRIORITY = 400.0;

}

0 comments on commit d1f4ca3

Please sign in to comment.