Skip to content

Commit

Permalink
delete property 'isId',fix 'autoGen'
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyingYu-Z committed Dec 24, 2020
1 parent 1eaf6c5 commit bee216f
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 204 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import java.util.Date;
@Setter
@Table(name = "user")
public class User {
@Column(name = "uid",type = "int(11)",isId = true,autoGen = false,isKey = true,defaultValue = "0")
@Column(name = "uid",type = "int(11)",autoGen = false,isKey = true,defaultValue = "0")
private Integer uid;
Expand All @@ -47,13 +47,15 @@ public class User {

```aidl
Ready ready = Ready.getInstance();
Ready ready = new Ready();
ready.setDbUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&&useSSL=true&serverTimezone=Asia/Shanghai");
ready.setUserName("root");
ready.setPassWord("123456");
ready.setCharacterSet("utf8mb4");
ready.setCollation("utf8mb4_general_ci");
try {
ReadyCore.init(ready);
ReadyCore.initTable(
ReadyCore.init(ready)
.initTable(
User.class
);
} catch (Exception e) {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/cn/beingyi/mysql/annotation/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

String type();//字段类型及长度

boolean isId() default false;

boolean autoGen() default false;

boolean isKey() default false;
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/cn/beingyi/mysql/core/Ready.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@

public class Ready {

static Ready ready;
private String DbUrl;//数据库地址
private String UserName;
private String PassWord;
private String CharacterSet = "utf8";
private String Collation = "utf8_general_ci";

public static Ready getInstance() {
if(ready==null){
ready=new Ready();
}
return ready;
public Ready() {
}

public String getDbUrl() {
Expand All @@ -39,4 +36,20 @@ public String getPassWord() {
public void setPassWord(String passWord) {
PassWord = passWord;
}

public String getCharacterSet() {
return CharacterSet;
}

public void setCharacterSet(String characterSet) {
CharacterSet = characterSet;
}

public String getCollation() {
return Collation;
}

public void setCollation(String collation) {
Collation = collation;
}
}
48 changes: 32 additions & 16 deletions src/main/java/cn/beingyi/mysql/core/ReadyCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,51 @@

public class ReadyCore {

static Ready ready;
public static List<String> tableNames = new ArrayList<>();
private Ready ready;
private List<String> tableNames = new ArrayList<>();

public static void init(Ready mReady) throws Exception {
ready = mReady;
public static ReadyCore init(Ready mReady) throws Exception {
return new ReadyCore(mReady);
}

public ReadyCore(Ready ready) throws Exception {
this.ready = ready;
if (ready == null) {
throw new Exception("ready is null");
}
try {
Connection con = JDBCUtils.getConnection();
Connection con = getConnection();
DatabaseMetaData db = con.getMetaData();
ResultSet resultSet = db.getTables(null, null, null, new String[]{"TABLE"});
while (resultSet.next()) {
tableNames.add(resultSet.getString(3));
}
resultSet.close();
con.close();

}catch (Exception e){
//e.printStackTrace();
} catch (Exception e) {
throw new Exception("failed to connect to mysql server");
}

}


public static void initTable(Class<?> ... cls) throws Exception{
for(Class<?> clazz:cls){
Thread thread=new Thread(){
public void initTable(Class<?>... cls) throws Exception {
for (Class<?> clazz : cls) {
Thread thread = new Thread() {
@Override
public void run() {
super.run();
//String tableName= AnnoationUtils.getTableName(clazz);
try {
new TableCreator(clazz);
String tableName = AnnoationUtils.getTableName(clazz);
if (tableName == null || tableName.isEmpty()) {
throw new Exception("table name must not be empty");
}

//不存在则创建,再升级
if (!tableNames.contains(tableName)) {
new TableCreator(ReadyCore.this, clazz);
} else {
//如果表存在,则升级
new TableUpdater(ReadyCore.this, clazz);
}
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -53,9 +63,15 @@ public void run() {
}
}

public Connection getConnection() throws Exception {
return JDBCUtils.getConnection(this);
}

public static Ready getReady() {
public Ready getReady() {
return ready;
}

public List<String> getTableNames() {
return tableNames;
}
}
Loading

0 comments on commit bee216f

Please sign in to comment.