Skip to content

Commit cb9287d

Browse files
committed
jdbc MySQL
1 parent cae36a5 commit cb9287d

9 files changed

Lines changed: 201 additions & 16 deletions

File tree

demo2/pom.xml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
<parent>
1515
<groupId>org.springframework.boot</groupId>
1616
<artifactId>spring-boot-starter-parent</artifactId>
17-
<version>2.0.3.RELEASE</version>
18-
<relativePath/> <!-- lookup parent from repository -->
17+
<version>1.5.8.RELEASE</version>
18+
<relativePath /> <!-- lookup parent from repository -->
1919
</parent>
2020

2121
<properties>
@@ -39,6 +39,22 @@
3939
<groupId>org.springframework.boot</groupId>
4040
<artifactId>spring-boot-starter-web</artifactId>
4141
</dependency>
42+
43+
<dependency>
44+
<groupId>com.google.code.gson</groupId>
45+
<artifactId>gson</artifactId>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-jdbc</artifactId>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>mysql</groupId>
55+
<artifactId>mysql-connector-java</artifactId>
56+
</dependency>
57+
4258
</dependencies>
4359

4460
<build>

demo2/src/main/java/com/example/demo/Demo2Application.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55

66
@SpringBootApplication
7-
public class Demo2Application {
7+
public class Demo2Application{
88

99
public static void main(String[] args) {
1010
SpringApplication.run(Demo2Application.class, args);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.example.demo.db;
2+
3+
import java.sql.ResultSet;
4+
import java.sql.SQLException;
5+
import java.text.SimpleDateFormat;
6+
import java.util.Date;
7+
import java.util.List;
8+
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.jdbc.core.JdbcTemplate;
11+
import org.springframework.jdbc.core.RowMapper;
12+
import org.springframework.stereotype.Repository;
13+
14+
import com.example.demo.model.WalkingDataPojo;
15+
16+
//导入要使用到的自己创建的工具类
17+
import com.example.demo.util.DateUtils;
18+
19+
20+
@Repository
21+
public class DataRepository {
22+
//@Autowired 这个注解就是spring可以自动帮你把bean里面引用的对象的setter/getter方法省略,它会自动帮你set/get。
23+
@Autowired
24+
protected JdbcTemplate jdbc;
25+
26+
27+
@Autowired
28+
private DateUtils dateUtil = new DateUtils();
29+
30+
SimpleDateFormat dt = dateUtil.getSimpleDateFormat();
31+
32+
33+
/**
34+
* 查询walking历史
35+
*
36+
* @param uid
37+
* @param sDate
38+
* @param eDate
39+
* @return
40+
*/
41+
public List<WalkingDataPojo> getWalkingDataOfWholeHistory(long uid, Date sDate, Date eDate) {
42+
String sDateStr = dt.format(sDate);
43+
String eDateStr = dt.format(eDate);
44+
String sql = "select * from Walking where uid = ? and str_to_date(record_date,'%Y-%m-%d') "
45+
+ " between str_to_date(?,'%Y-%m-%d %H:%i:%s') and str_to_date(?,'%Y-%m-%d %H:%i:%s');";
46+
47+
return jdbc.query(sql, walkingDataMapper, uid, sDateStr, eDateStr);
48+
}
49+
50+
private static final RowMapper<WalkingDataPojo> walkingDataMapper = new RowMapper<WalkingDataPojo>() {
51+
public WalkingDataPojo mapRow(ResultSet rs, int rowNum) throws SQLException {
52+
WalkingDataPojo walkingData = new WalkingDataPojo();
53+
walkingData.setUid(rs.getLong("uid"));
54+
walkingData.setRecord_date(rs.getDate("record_date").getTime() / 1000);
55+
walkingData.setStep(rs.getInt("step") );
56+
walkingData.setCalorie(rs.getFloat("calorie"));
57+
walkingData.setDistance(rs.getFloat("distance"));
58+
walkingData.setData_from(rs.getString("data_from"));
59+
return walkingData;
60+
}
61+
};
62+
63+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.example.demo.model;
2+
3+
public class WalkingDataPojo {
4+
5+
private long uid;
6+
private long record_date;
7+
private String date;
8+
private int step;
9+
private float calorie;
10+
private float distance;
11+
private String data_from;
12+
13+
public long getUid() {
14+
return uid;
15+
}
16+
public void setUid(long uid) {
17+
this.uid = uid;
18+
}
19+
20+
public long getRecord_date() {
21+
return record_date;
22+
}
23+
public void setRecord_date(long record_date) {
24+
this.record_date = record_date;
25+
}
26+
27+
public int getStep() {
28+
return step;
29+
}
30+
public void setStep(int step) {
31+
this.step = step;
32+
}
33+
34+
public float getCalorie() {
35+
return calorie;
36+
}
37+
public void setCalorie(float calorie) {
38+
this.calorie = calorie;
39+
}
40+
41+
public float getDistance() {
42+
return distance;
43+
}
44+
public void setDistance(float distance) {
45+
this.distance = distance;
46+
}
47+
48+
public String getData_from() {
49+
return data_from;
50+
}
51+
public void setData_from(String data_from) {
52+
this.data_from = data_from;
53+
}
54+
55+
public String getDate() {
56+
return date;
57+
}
58+
59+
public void setDate(String date) {
60+
this.date = date;
61+
}
62+
63+
public WalkingDataPojo() {
64+
super();
65+
66+
}
67+
68+
}

demo2/src/main/java/com/example/demo/sport/SportController.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
package com.example.demo.sport;
22

3+
//导入一些需要使用到的系统类库
34
import java.text.ParseException;
45
import java.text.SimpleDateFormat;
56
import java.util.Date;
67
import java.util.Map;
78
import java.util.HashMap;
9+
import java.util.List;
810
import java.util.regex.Matcher;
911
import java.util.regex.Pattern;
1012

13+
import org.springframework.beans.factory.annotation.Autowired;
1114
import org.springframework.web.bind.annotation.RequestMapping;
1215
import org.springframework.web.bind.annotation.RequestMethod;
1316
import org.springframework.web.bind.annotation.RestController;
1417

18+
import com.example.demo.db.*;
19+
import com.example.demo.model.WalkingDataPojo;
20+
//导入要使用到的自己创建的工具类
1521
import com.example.demo.util.*;
22+
import com.example.demo.db.DataRepository;
23+
1624

1725

1826
@RestController
1927
public class SportController {
2028

2129
private DateUtils dateUtil = new DateUtils();
2230

31+
@Autowired
32+
private DataRepository dbRepository;
33+
2334
@RequestMapping(value = "/getstep", method = RequestMethod.GET)
2435
public String getStepData(long uid,String date) {
2536
try {
@@ -31,11 +42,12 @@ public String getStepData(long uid,String date) {
3142
SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");
3243
java.util.Date sDate =(java.util.Date) sdf.parse(date);
3344
Date sqlsDate = dateUtil.getSqlDate(sDate);
34-
//根据 uid 和 date 查询数据,一般是去查询数据库;这里暂时使用假数据;
35-
Map m = getStepDataFromDB(uid,sqlsDate) ;
45+
//根据 uid 和 date 查询数据,一般是去查询数据库;
46+
Date eDate = dateUtil.getEndTimeByStartDate(sqlsDate, 0);
47+
List<WalkingDataPojo> list = dbRepository.getWalkingDataOfWholeHistory(uid, sqlsDate, eDate);
3648
//构造json返回值
3749
Return2Client response = new Return2Client(ErrorResponseBuilder.SUCCESS);
38-
response.setContent(m);
50+
response.setContent(list);
3951
return response.toJson();
4052

4153
} catch (ParseException e) {
@@ -45,6 +57,7 @@ public String getStepData(long uid,String date) {
4557

4658

4759
/************************************************* Private ***********************************************************/
60+
//检测时间是否合理的私有方法
4861
private boolean VerifyStartTime(String st){
4962
Pattern p = Pattern.compile("^\\d{8}$");
5063
Matcher m = p.matcher(st);
@@ -55,13 +68,15 @@ private boolean VerifyStartTime(String st){
5568
return true;
5669
}
5770

71+
/*
72+
//获取步数数据的私有方法,在这个方法里面,使用的是假数据
5873
private Map<String,String> getStepDataFromDB(long uid,Date date){
5974
Map<String,String> m1 = new HashMap<String,String>();
6075
m1.put("step", "8888");
6176
m1.put("calorie", "88");
6277
return m1;
6378
}
64-
79+
*/
6580

6681

6782
}

demo2/src/main/java/com/example/demo/util/DateUtils.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
import java.text.ParseException;
55
import java.text.SimpleDateFormat;
66
import java.util.Calendar;
7+
import java.util.TimeZone;
78
import java.util.regex.Matcher;
89
import java.util.regex.Pattern;
910

11+
import org.springframework.stereotype.Service;
12+
13+
@Service
1014
public class DateUtils {
1115

1216
public SimpleDateFormat df_ymd = new SimpleDateFormat("yyyyMMdd");
@@ -21,6 +25,23 @@ public boolean VerifyStartTime(String st){
2125
}
2226
return true;
2327
}
28+
29+
/**
30+
* 通过开始时间计算向后第N天
31+
* @param sDate 开始日期
32+
* @param ds 天数
33+
* @return
34+
*/
35+
public Date getEndTimeByStartDate(java.util.Date sDate,int ds){
36+
Calendar calendar=Calendar.getInstance();
37+
calendar.setTime(sDate);
38+
calendar.set(Calendar.HOUR_OF_DAY, 23);
39+
calendar.set(Calendar.MINUTE, 59);
40+
calendar.add(Calendar.DATE, ds);
41+
java.util.Date eDate = calendar.getTime();
42+
Date sqlDate = new java.sql.Date(eDate.getTime());
43+
return sqlDate;
44+
}
2445

2546

2647
/**
@@ -45,6 +66,13 @@ public java.util.Date numberToDate(int numberDate){
4566
return null;
4667
}
4768
}
69+
70+
/*返回统一时区,不受本地时间影响*/
71+
public SimpleDateFormat getSimpleDateFormat() {
72+
SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
73+
dt.setTimeZone(TimeZone.getTimeZone("GMT+0"));
74+
return dt;
75+
}
4876

4977

5078
public int getYear() {

demo2/src/main/java/com/example/demo/util/Message.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package com.example.demo.util;
22
import com.google.gson.Gson;
33
import com.google.gson.GsonBuilder;
4-
/**
5-
*
6-
* @author hzy
7-
*
8-
*/
4+
95
public class Message {
106
private int retCode;
117
private String msg;
@@ -19,8 +15,6 @@ public int getRetCode() {
1915
return retCode;
2016
}
2117

22-
23-
2418
public void setRetCode(int retCode) {
2519
this.retCode = retCode;
2620
}
@@ -41,7 +35,6 @@ public void setMsg(String msg) {
4135
* @return
4236
*/
4337
public String toJson() {
44-
// Gson gson = new Gson();
4538
Gson gson = new GsonBuilder()
4639
.setDateFormat("yyyy-MM-dd HH:mm:ss").create();
4740
return gson.toJson(this);

demo2/src/main/java/com/example/demo/util/Return2Client.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public Return2Client(int retCode) {
88

99
private Object content;
1010

11-
1211

1312
public Object getContent() {
1413
return content;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring.datasource.url=jdbc:mysql://localhost:3306/Health
2+
spring.datasource.username=root
3+
spring.datasource.password=123456

0 commit comments

Comments
 (0)