-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySql.java
313 lines (301 loc) · 8.99 KB
/
MySql.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package com.dao;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
import org.springframework.stereotype.Repository;
import javax.servlet.http.HttpServletRequest;
import java.sql.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
interface MySqlDao{
@SelectProvider(type = UserMapper.class, method = "returnSql")
List<LinkedHashMap<String,Object>> select(String sql);
@UpdateProvider(type = UserMapper.class, method = "returnSql")
int update(String sql);
@InsertProvider(type = UserMapper.class, method = "returnSql")
int insert(String sql);
@DeleteProvider(type = UserMapper.class, method = "returnSql")
int delete(String sql);
}
/**
* 自定义Sql运行类
* <p>
* 常见用法:List<LinkedHashMap<String, Object>> list=ms.setSql("select * from linkman where
* name=?").set("张三").runlist();
* <p>
* @date 2023-3-23
* @author aotmd
*/
@Repository
public class MySql {
static ThreadLocal<ThreadCache> TL = new ThreadLocal<ThreadCache>(){
@Override
protected ThreadCache initialValue() {
return new ThreadCache();
}
};
public static class ThreadCache {
public String sql = "";
public PreparedStatement ps = null;
/**
* 总条目数
*/
public int sum = 0;
/**
* 索引
*/
public int index = 0;
/**
* 数据库表字段(Map键值)
*/
public String[] top = null;
public ThreadCache() {
}
}
MySqlDao mySqlDao;
private Connection conn = null;
public MySql(MySqlDao mySqlDao) {
this.mySqlDao = mySqlDao;
connectToTheDatabase();
}
/**
* 反复使用时的更新Sql语句方法
* @param sql mysql语句
* @return this
*/
public MySql setSql(String sql) {
TL.get().sum = 0;
TL.get().index = 1;
try {
//检查连接存活状态
if (conn.isClosed()){
connectToTheDatabase();
}
TL.get().ps = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return this;
}
/**
* 调试用方法
* @return 返回sql语句
*/
public String getSql() {
return TL.get().sql;
}
/**
* 连接数据库
*
*/
private void connectToTheDatabase() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/inventory_system?useUnicode=true&characterEncoding=utf-8";
conn = DriverManager.getConnection(url, "root", "123456");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
/**
* 从前往后一个一个修改
* @param object 修改数据库?号所对值
* @return this
*/
public MySql set(Object object) {
try {
TL.get().ps.setObject(TL.get().index++,object);
} catch (SQLException e) {
e.printStackTrace();
}
return this;
}
/**
* 更新的形式提交
* @return (1) SQL 数据操作语言 (DML) 语句的行数 (2) 对于无返回内容的 SQL 语句,返回 0
*/
public int run() {
int i = 0;
try {
String temp = TL.get().ps.toString();
TL.get().ps.close();
String sql1 = temp.substring(temp.indexOf(": ") + 2);
TL.get().sql = sql1;//记录用
if (sql1.contains("?")){
System.out.println("sql语句错误!有多余的?号");
}
String type=sql1.trim().split("\\s+")[0];
if (type.equalsIgnoreCase("update")){
i=mySqlDao.update(sql1);
}else if (type.equalsIgnoreCase("insert")){
i=mySqlDao.insert(sql1);
}else if (type.equalsIgnoreCase("delete")){
i=mySqlDao.delete(sql1);
}else {
System.out.println("不是增删改的数据库语句");
}
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
/**
* 运行executeQuery(),即查询内容
* <P>调用方法:for(LinkedHashMap<String, Object> map:list){map.get(key)}
* <p>
* 示例:topname为getName()取得的String[]
* <ul>
* <li>for(LinkedHashMap<String, Object> map:list) {</li>
* <li> for(int j = 0; j < topname.length; j++){</li>
* <li> System.out.print(map.get(topname[j])+"\t");</li>
* <li> }</li>
* <li>System.out.println();</li> }
* </ul>
*
* @return 返回List对象
*/
public List<LinkedHashMap<String, Object>> runList() {
// 声明返回的对象
List<LinkedHashMap<String, Object>> list = new ArrayList<>();
try {
String temp = TL.get().ps.toString();
String sql1;
sql1 =temp.substring(temp.indexOf(": ")+2);
TL.get().sql = sql1;//记录用
if (sql1.contains("?")){
System.out.println("sql语句错误!有多余的?号");
System.out.println(sql1);
TL.get().ps.close();
return list;
}
String type= sql1.trim().split("\\s+")[0];
if (type.equalsIgnoreCase("select")){
list=mySqlDao.select(sql1);
}else {
System.out.println("不是查询的数据库语句");
System.out.println(sql1);
TL.get().ps.close();
return list;
}
TL.get().sum = list.size();
//当MyBatis取得到值时获取数据库列字段,取不到时使用JDBC取数据库列字段
if (!list.isEmpty()){
TL.get().top = list.get(0).keySet().toArray(new String[0]);
} else {
System.out.println("调用JDBC获取字段");
ResultSet resu = TL.get().ps.executeQuery();
// 分析结果集
ResultSetMetaData rsmd = resu.getMetaData();
// 获取列数
int cols = rsmd.getColumnCount();
// 初始化
TL.get().top = new String[cols];
for (int i = 0; i < cols; i++) {
// 获取列名存入String[]
TL.get().top[i] = rsmd.getColumnName(i + 1);
}
}
TL.get().ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
/**
* 调用该方法需正确设置sql语句,<p>"select * from linkman where uid=? order by id asc limit ?,?"<p>
* <b>order后的两个?号不需要set()设置</b>
* @param request request
* @param url 当前Servlet网址
* @param pageMax 以多少记录为一页
*/
public void runPagination(HttpServletRequest request, String url, int pageMax) {
//当前页
int currentPage = 1;
// 判断传递页码是否有效
if (request.getParameter("currentPage") != null) {
currentPage = Integer.parseInt(request.getParameter("currentPage"));
}
List<LinkedHashMap<String, Object>> list = this.set((currentPage - 1) * pageMax).set(pageMax).runList();
request.getSession().setAttribute("list", list);
//重新查询未分页时的记录数(更新sum值)
this.setSql(TL.get().sql.substring(0, TL.get().sql.indexOf("order")-1)).runList();
// 总页数
int pages;
if (this.getSum() % pageMax == 0) {
pages = this.getSum() / pageMax;
} else {
pages = this.getSum() / pageMax + 1;
}
StringBuilder sb = new StringBuilder();
//格式化url,传值有/则截去
if(url.indexOf("/")==0) {url=url.substring(1);}
// 构建分页条
for (int i = 1; i <= pages; i++) {
if (i == currentPage) {
sb.append(String.format("<b>%d</b>", i));
} else {
sb.append(String.format("<a href='%s?currentPage=%d'>%d</a>", url, i, i));
}
if (i != pages) {
sb.append(" ");
}
}
request.getSession().setAttribute("bar", sb.toString());
}
/**
* 获取数据库表字段(Map键值)
*
* @return 返回String[](Map键值)
*/
public String[] getTop() {
return TL.get().top;
}
/**
* 返回executeQuery()获得记录的行数,
* <p>若使用分页方法则返回的是未分页时的记录条数
* @return 返回executeQuery()获得记录的行数
*/
public int getSum() {
return TL.get().sum;
}
// public static void main(String[] args) {
/* MySql ms = new MySql("select * from linkman where uid=?");
List<Map<String, Object>> list = ms.set(1).runlist();
//-------------------------获取表头(Map的Key)----------------------------
String[] topname = ms.getTop();
for (int i = 0; i < topname.length; i++) {
System.out.print(topname[i] + "\t");
}
System.out.println();
//----------------------------------------------------------------------
for (Map<String, Object> map : list) {
for (int j = 0; j < topname.length; j++) {
System.out.print(map.get(topname[j]) + "\t");
}
System.out.println();
}
System.out.println("共" + ms.getSum() + "条数据");
System.out.println(ms.getSql());*/
/*
* //下为增强for实际代码
* for(int i=0;i<list.size();i++) {
* Map<String, Object> map=list.get(i);
* for (int j = 0; j < topname.length; j++) {//指定key
* System.out.print(map.get(topname[j])+"\t"); }
* System.out.println(); }
* //增强for
* for (String key : map.keySet())
* {//map为无序输出,因此不一定会按加入顺序输出,要按顺序输出请指定key或者改为list
* System.out.print(key + " :" +map.get(key));
* }
* System.out.println();
* //下为增强for实际代码
* for (Iterator<String> iterator = map.keySet().iterator(); iterator.hasNext();) {
* String key=iterator.next();
* System.out.print(key + " :" + map.get(key));
* }
*/
// }
}