Skip to content

Commit 8f6b44b

Browse files
committed
增加Sqlite数据存储支持
1 parent d01006c commit 8f6b44b

File tree

13 files changed

+450
-1
lines changed

13 files changed

+450
-1
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@
4545
<artifactId>fastjson</artifactId>
4646
<version>1.2.47</version>
4747
</dependency>
48+
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
49+
<dependency>
50+
<groupId>org.mybatis</groupId>
51+
<artifactId>mybatis</artifactId>
52+
<version>3.4.5</version>
53+
</dependency>
54+
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
55+
<dependency>
56+
<groupId>org.xerial</groupId>
57+
<artifactId>sqlite-jdbc</artifactId>
58+
<version>3.20.1</version>
59+
</dependency>
4860

4961

5062
</dependencies>

profile/nbs.db

20 KB
Binary file not shown.

src/main/java/UI/panel/StatusPanel.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,13 @@ public void actionPerformed(ActionEvent e) {
129129

130130
JLabel nickLabel = new ContentJLabel(AppMainWindow.PROFILE_NICKNAME);
131131
nickLabel.setFont(ConstantsUI.FONT_LABEL);
132+
133+
132134
nickLabel.addMouseListener(new MouseAdapter() {
133135
@Override
134136
public void mouseClicked(MouseEvent e) {
135137
String newNick = JOptionPane.showInputDialog(AppMainWindow.frame,"昵称",nickLabel.getText());
136-
if(StringUtils.isNotBlank(newNick.trim())&&newNick.trim().length()<=40){
138+
if(StringUtils.isNotBlank(newNick)&&newNick.trim().length()<=40){
137139
try {
138140
String res = IPFSHelper.getInstance().updateNick(newNick);
139141
if(res!=null)AppMainWindow.PROFILE_NICKNAME = res;
@@ -146,6 +148,19 @@ public void mouseClicked(MouseEvent e) {
146148
}
147149
super.mouseClicked(e);
148150
}
151+
152+
@Override
153+
public void mouseEntered(MouseEvent e) {
154+
nickLabel.setToolTipText("点击修改昵称");
155+
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
156+
super.mouseEntered(e);
157+
}
158+
159+
@Override
160+
public void mouseExited(MouseEvent e) {
161+
setCursor(Cursor.getDefaultCursor());
162+
super.mouseExited(e);
163+
}
149164
});
150165
peerInfo.add(nickLabel);
151166

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package com.nbs.biz.dao;
2+
3+
import com.nbs.biz.model.BasicModel;
4+
import org.apache.ibatis.exceptions.PersistenceException;
5+
import org.apache.ibatis.session.SqlSession;
6+
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
/**
12+
* @Package : com.nbs.biz.dao
13+
* @Description : <p></p>
14+
* @Author : lambor.c
15+
* @Date : 2018/6/23-21:08
16+
* Copyright (c) 2018, NBS , lambor.c<[email protected]>.
17+
* All rights reserved.
18+
*/
19+
public class BasicDao {
20+
protected SqlSession session;
21+
private String className;
22+
23+
24+
public BasicDao(SqlSession session, Class clazz)
25+
{
26+
this.session = session;
27+
this.className = clazz.getName();
28+
}
29+
30+
public int insert(BasicModel model)
31+
{
32+
return session.insert(className + ".insert", model);
33+
}
34+
35+
public List findAll()
36+
{
37+
//return session.selectList(className + ".findAll");
38+
return _findAll(0);
39+
}
40+
41+
private List _findAll(int time)
42+
{
43+
if (time > 10)
44+
{
45+
System.out.println("查询到 BasicModelList 对象失败次数>10,放弃查询");
46+
return null;
47+
}
48+
49+
try
50+
{
51+
return session.selectList(className + ".findAll");
52+
} catch (PersistenceException exception)
53+
{
54+
System.out.println("没有查询到 BasicModelList 对象,继续查询");
55+
return _findAll(++time);
56+
}
57+
}
58+
59+
public BasicModel findById(String id)
60+
{
61+
return _findById(id, 0);
62+
}
63+
64+
private BasicModel _findById(String id, int time)
65+
{
66+
if (time > 10)
67+
{
68+
System.out.println("查询到 BasicModel 对象失败次数>10,放弃查询");
69+
return null;
70+
}
71+
72+
try
73+
{
74+
return (BasicModel) session.selectOne(className + ".findById", id);
75+
} catch (PersistenceException exception)
76+
{
77+
System.out.println("没有查询到 BasicModel 对象,继续查询");
78+
return _findById(id, ++time);
79+
}
80+
}
81+
82+
public List find(String field, Object val)
83+
{
84+
Map map = new HashMap();
85+
map.put("field", field);
86+
87+
if (val instanceof String)
88+
{
89+
map.put("val", "'" + val + "'");
90+
}
91+
else
92+
{
93+
map.put("val", val);
94+
}
95+
96+
return session.selectList(className + ".find", map);
97+
}
98+
99+
public int delete(String id)
100+
{
101+
return session.delete(className + ".delete", id);
102+
}
103+
104+
public int deleteAll()
105+
{
106+
return session.delete(className + ".deleteAll");
107+
}
108+
109+
public int update(BasicModel model)
110+
{
111+
return session.update(className + ".update", model);
112+
}
113+
114+
public int updateIgnoreNull(BasicModel model)
115+
{
116+
return session.update(className + ".updateIgnoreNull", model);
117+
}
118+
119+
public List updateField(String field, Object val)
120+
{
121+
Map map = new HashMap();
122+
map.put("field", field);
123+
124+
if (val instanceof String || val instanceof Boolean)
125+
{
126+
map.put("val", "'" + val + "'");
127+
}
128+
else
129+
{
130+
map.put("val", val);
131+
}
132+
133+
return session.selectList(className + ".updateField", map);
134+
}
135+
136+
public int count()
137+
{
138+
return (int) session.selectOne(className + ".count");
139+
}
140+
141+
public boolean exist(String id)
142+
{
143+
return ((int) (session.selectOne(className + ".exist", id))) > 0;
144+
}
145+
146+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.nbs.biz.dao;
2+
3+
import com.nbs.biz.model.NBSTest;
4+
import org.apache.ibatis.session.SqlSession;
5+
6+
import java.util.List;
7+
8+
/**
9+
* @Package : com.nbs.biz.dao
10+
* @Description : <p></p>
11+
* @Author : lambor.c
12+
* @Date : 2018/6/23-21:12
13+
* Copyright (c) 2018, NBS , lambor.c<[email protected]>.
14+
* All rights reserved.
15+
*/
16+
public class NBSTableDao {
17+
private SqlSession session;
18+
19+
public NBSTableDao(SqlSession session) {
20+
this.session = session;
21+
}
22+
23+
public boolean tableExist(String name)
24+
{
25+
return ((int) session.selectOne("tableExist", name)) > 0;
26+
}
27+
28+
public void createNbsContacts(){
29+
session.update("createNbsContacts");
30+
}
31+
32+
33+
public List<NBSTest> findAll(){
34+
return session.selectList("findAll");
35+
}
36+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.nbs.biz.model;
2+
3+
/**
4+
* @Package : com.nbs.biz.model
5+
* @Description : <p></p>
6+
* @Author : lambor.c
7+
* @Date : 2018/6/23-21:12
8+
* Copyright (c) 2018, NBS , lambor.c<[email protected]>.
9+
* All rights reserved.
10+
*/
11+
public class BasicModel {
12+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.nbs.biz.model;
2+
3+
import java.util.Date;
4+
5+
/**
6+
* @Package : com.nbs.biz.model
7+
* @Description : <p></p>
8+
* @Author : lambor.c
9+
* @Date : 2018/6/23-22:04
10+
* Copyright (c) 2018, NBS , lambor.c<[email protected]>.
11+
* All rights reserved.
12+
*/
13+
public class NBSTest {
14+
private Integer id;
15+
private String name;
16+
private String ctime;
17+
private Date lmtime;
18+
19+
public Integer getId() {
20+
return id;
21+
}
22+
23+
public void setId(Integer id) {
24+
this.id = id;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
public String getCtime() {
36+
return ctime;
37+
}
38+
39+
public void setCtime(String ctime) {
40+
this.ctime = ctime;
41+
}
42+
43+
public Date getLmtime() {
44+
return lmtime;
45+
}
46+
47+
public void setLmtime(Date lmtime) {
48+
this.lmtime = lmtime;
49+
}
50+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.nbs.biz.service;
2+
3+
import com.nbs.biz.dao.NBSTableDao;
4+
import com.nbs.biz.model.NBSTest;
5+
import org.apache.ibatis.session.SqlSession;
6+
7+
import java.util.List;
8+
9+
/**
10+
* @Package : com.nbs.biz.service
11+
* @Description : <p></p>
12+
* @Author : lambor.c
13+
* @Date : 2018/6/23-21:19
14+
* Copyright (c) 2018, NBS , lambor.c<[email protected]>.
15+
* All rights reserved.
16+
*/
17+
public class TableService {
18+
private NBSTableDao dao;
19+
20+
public TableService(SqlSession session) {
21+
this.dao = new NBSTableDao(session);
22+
}
23+
24+
public void initNbsContacts(){
25+
String tbNbsContacts = "nbs_contacts";
26+
boolean b = dao.tableExist(tbNbsContacts);
27+
if(!b){
28+
dao.createNbsContacts();
29+
}
30+
}
31+
32+
public List<NBSTest> getAll(){
33+
return dao.findAll();
34+
}
35+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.nbs.utils;
2+
3+
import org.apache.ibatis.io.Resources;
4+
import org.apache.ibatis.session.SqlSession;
5+
import org.apache.ibatis.session.SqlSessionFactory;
6+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
7+
8+
import java.io.IOException;
9+
import java.io.Reader;
10+
11+
/**
12+
* @Package : com.nbs.utils
13+
* @Description : <p></p>
14+
* @Author : lambor.c
15+
* @Date : 2018/6/23-21:53
16+
* Copyright (c) 2018, NBS , lambor.c<[email protected]>.
17+
* All rights reserved.
18+
*/
19+
public class DbUtil {
20+
private static SqlSession sqlSession = null;
21+
static {
22+
try {
23+
Reader reader = Resources.getResourceAsReader("mybatis-conf.xml");
24+
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
25+
sqlSession = factory.openSession(true);
26+
reader.close();
27+
28+
29+
} catch (IOException e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
34+
private DbUtil(){
35+
36+
}
37+
38+
/**
39+
*
40+
* @return
41+
*/
42+
public static SqlSession getSqlSession() {
43+
return sqlSession;
44+
}
45+
}

src/main/resources/db.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
jdbc.driverClassName = org.sqlite.JDBC
2+
jdbc.url = jdbc:sqlite:./profile/nbs.db

0 commit comments

Comments
 (0)