Skip to content

Commit b74c82b

Browse files
author
root
committed
PersistentState mysql config data
1 parent 959347f commit b74c82b

File tree

14 files changed

+594
-68
lines changed

14 files changed

+594
-68
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,15 @@
22

33
### .travis.yml 不能用
44

5+
### 使用
6+
7+
idea -> Other
8+
9+
ConfigMysql 配置mysql信息
10+
11+
code generator 配置代码生成路径 生成代码
12+
13+
14+
515

616

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baomidou.plugin.idea.mybatisx.action;
2+
3+
import com.baomidou.plugin.idea.mybatisx.codegenerator.view.DBInfo;
4+
import com.intellij.openapi.actionSystem.AnAction;
5+
import com.intellij.openapi.actionSystem.AnActionEvent;
6+
7+
public class ConfigMysql extends AnAction {
8+
9+
@Override
10+
public void actionPerformed(AnActionEvent e) {
11+
DBInfo dialog = new DBInfo();
12+
dialog.pack();
13+
dialog.setVisible(true);
14+
dialog.setLocationRelativeTo(null);
15+
dialog.setSize(500,300);
16+
}
17+
}

src/main/java/com/baomidou/plugin/idea/mybatisx/action/TestAction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.baomidou.plugin.idea.mybatisx.action;
22

3+
import com.baomidou.plugin.idea.mybatisx.codegenerator.view.DBInfo;
34
import com.baomidou.plugin.idea.mybatisx.codegenerator.view.ShowTableInfo;
45
import com.intellij.database.actions.DatabaseObjectRefactoring;
56
import com.intellij.database.model.DasObject;
@@ -38,6 +39,7 @@ public class TestAction extends AnAction {
3839

3940
@Override
4041
public void actionPerformed(AnActionEvent e) {
42+
4143
String projectRoot = e.getProject().getBasePath();
4244
ShowTableInfo dialog = new ShowTableInfo(projectRoot);
4345
dialog.pack();

src/main/java/com/baomidou/plugin/idea/mybatisx/codegenerator/MysqlUtil.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.baomidou.plugin.idea.mybatisx.codegenerator.domain.vo.ColumnInfo;
44
import com.baomidou.plugin.idea.mybatisx.codegenerator.domain.vo.TableInfo;
5+
import com.intellij.ide.util.PropertiesComponent;
56

67
import java.lang.reflect.Field;
78
import java.sql.*;
@@ -11,48 +12,42 @@
1112
public class MysqlUtil {
1213

1314
// MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
14-
private static String jdbcDriver = "com.mysql.jdbc.Driver";
15-
private static String dbUrl = "jdbc:mysql://localhost:3306/eladmin?useSSL=false&serverTimezone=UTC";
15+
// private static String jdbcDriver = "";
16+
// private static String dbUrl = "";
1617

1718
// MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
1819
//private String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
1920
//private String DB_URL = "jdbc:mysql://localhost:3306/unionfund?useSSL=false&serverTimezone=UTC";
2021

2122

2223
// 数据库的用户名与密码,需要根据自己的设置
23-
private static String username = "root";
24-
private static String password = "123456";
24+
// private static String username = "root";
25+
// private static String password = "123456";
2526

2627
public String getJdbcDriver() {
28+
String jdbcDriver = PropertiesComponent.getInstance().getValue("mybatisplus_jdbcDriver");
2729
return jdbcDriver;
2830
}
2931

30-
public void setJdbcDriver(String jdbcDriver) {
31-
this.jdbcDriver = jdbcDriver;
32-
}
32+
3333

3434
public String getDbUrl() {
35+
String dbUrl = PropertiesComponent.getInstance().getValue("mybatisplus_dbUrl");
3536
return dbUrl;
3637
}
3738

38-
public void setDbUrl(String dbUrl) {
39-
this.dbUrl = dbUrl;
40-
}
39+
4140

4241
public String getUsername() {
42+
String username = PropertiesComponent.getInstance().getValue("mybatisplus_username");
4343
return username;
4444
}
4545

46-
public void setUsername(String username) {
47-
this.username = username;
48-
}
49-
5046
public String getPassword() {
47+
String password = PropertiesComponent.getInstance().getValue("mybatisplus_password");
5148
return password;
5249
}
53-
public void setPassword(String password) {
54-
this.password = password;
55-
}
50+
5651

5752
private static MysqlUtil mysqlUtil;
5853

@@ -83,10 +78,10 @@ public List<ColumnInfo> getColumns(String tableName) {
8378
Statement stmt;
8479
try {
8580
// 注册 JDBC 驱动
86-
Class.forName(jdbcDriver);
81+
Class.forName(getJdbcDriver());
8782
// 打开链接
8883
System.out.println("连接数据库...");
89-
conn = DriverManager.getConnection(dbUrl,username,password);
84+
conn = DriverManager.getConnection(getDbUrl(),getUsername(),getPassword());
9085
// 执行查询
9186
System.out.println(" 实例化Statement对象...");
9287
stmt = conn.createStatement();
@@ -128,10 +123,10 @@ public List<TableInfo> getTableInfo() {
128123
Statement stmt;
129124
try {
130125
// 注册 JDBC 驱动
131-
Class.forName(jdbcDriver);
126+
Class.forName(getJdbcDriver());
132127
// 打开链接
133128
System.out.println("连接数据库...");
134-
conn = DriverManager.getConnection(dbUrl,username,password);
129+
conn = DriverManager.getConnection(getDbUrl(),getUsername(),getPassword());
135130
// 执行查询
136131
System.out.println(" 实例化Statement对象...");
137132
stmt = conn.createStatement();
@@ -164,4 +159,19 @@ public List<TableInfo> getTableInfo() {
164159
return new ArrayList<>();
165160
}
166161

162+
public boolean testConnect() {
163+
// 注册 JDBC 驱动
164+
try {
165+
Class.forName(getJdbcDriver());
166+
// 打开链接
167+
System.out.println("连接数据库...");
168+
DriverManager.getConnection(getDbUrl(),getUsername(),getPassword());
169+
return true;
170+
} catch (ClassNotFoundException e) {
171+
e.printStackTrace();
172+
} catch (SQLException e) {
173+
e.printStackTrace();
174+
}
175+
return false;
176+
}
167177
}

src/main/java/com/baomidou/plugin/idea/mybatisx/codegenerator/domain/vo/ColumnInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class ColumnInfo {
2929
private String columnQuery;
3030

3131
/** 是否在列表显示 **/
32-
private String columnShow;
32+
private String columnShow = "true";
3333

3434
@Override
3535
public String toString() {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.baomidou.plugin.idea.mybatisx.codegenerator.setting;
2+
3+
import com.baomidou.plugin.idea.mybatisx.generate.AbstractStatementGenerator;
4+
import com.baomidou.plugin.idea.mybatisx.generate.GenerateModel;
5+
import com.google.gson.Gson;
6+
import com.google.gson.reflect.TypeToken;
7+
import com.intellij.openapi.components.PersistentStateComponent;
8+
import com.intellij.openapi.components.ServiceManager;
9+
import com.intellij.openapi.components.State;
10+
import com.intellij.openapi.components.Storage;
11+
import org.jdom.Element;
12+
import org.jetbrains.annotations.Nullable;
13+
14+
import java.lang.reflect.Type;
15+
import java.util.Set;
16+
17+
import static com.baomidou.plugin.idea.mybatisx.generate.AbstractStatementGenerator.*;
18+
19+
/**
20+
* @author yanglin
21+
*/
22+
@State(
23+
name = "MybatisSettings",
24+
storages = @Storage(file = "$APP_CONFIG$/mybatis-mysql.xml"))
25+
public class MysqlSetting implements PersistentStateComponent<Element> {
26+
27+
private GenerateModel statementGenerateModel;
28+
29+
private Gson gson = new Gson();
30+
31+
private Type gsonTypeToken = new TypeToken<Set<String>>() {
32+
}.getType();
33+
34+
public MysqlSetting() {
35+
statementGenerateModel = GenerateModel.START_WITH_MODEL;
36+
}
37+
38+
public static MysqlSetting getInstance() {
39+
return ServiceManager.getService(MysqlSetting.class);
40+
}
41+
42+
/**
43+
* 保存到文件
44+
*/
45+
@Nullable
46+
@Override
47+
public Element getState() {
48+
Element element = new Element("MybatisSettings");
49+
element.setAttribute(INSERT_GENERATOR.getId(), gson.toJson(INSERT_GENERATOR.getPatterns()));
50+
element.setAttribute(DELETE_GENERATOR.getId(), gson.toJson(DELETE_GENERATOR.getPatterns()));
51+
element.setAttribute(UPDATE_GENERATOR.getId(), gson.toJson(UPDATE_GENERATOR.getPatterns()));
52+
element.setAttribute(SELECT_GENERATOR.getId(), gson.toJson(SELECT_GENERATOR.getPatterns()));
53+
element.setAttribute("statementGenerateModel", String.valueOf(statementGenerateModel.getIdentifier()));
54+
return element;
55+
}
56+
57+
/**
58+
* 从文件读取数据
59+
* @param state
60+
*/
61+
@Override
62+
public void loadState(Element state) {
63+
loadState(state, INSERT_GENERATOR);
64+
loadState(state, DELETE_GENERATOR);
65+
loadState(state, UPDATE_GENERATOR);
66+
loadState(state, SELECT_GENERATOR);
67+
statementGenerateModel = GenerateModel.getInstance(state.getAttributeValue("statementGenerateModel"));
68+
}
69+
70+
private void loadState(Element state, AbstractStatementGenerator generator) {
71+
String attribute = state.getAttributeValue(generator.getId());
72+
if (null != attribute) {
73+
generator.setPatterns((Set<String>) gson.fromJson(attribute, gsonTypeToken));
74+
}
75+
}
76+
77+
public GenerateModel getStatementGenerateModel() {
78+
return statementGenerateModel;
79+
}
80+
81+
public void setStatementGenerateModel(GenerateModel statementGenerateModel) {
82+
this.statementGenerateModel = statementGenerateModel;
83+
}
84+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baomidou.plugin.idea.mybatisx.codegenerator.utils;
2+
3+
import com.intellij.ide.util.PropertiesComponent;
4+
5+
public interface MybatisConst {
6+
public final static String MYBATISPLUS_DBURL = "mybatisplus_dbUrl";
7+
public final static String MYBATISPLUS_JDBCDRIVER = "mybatisplus_jdbcDriver";
8+
public final static String MYBATISPLUS_USERNAME = "mybatisplus_username";
9+
public final static String MYBATISPLUS_PASSWORD = "mybatisplus_password";
10+
}

0 commit comments

Comments
 (0)