This commit is contained in:
zyyang 2021-03-03 11:00:43 +08:00
parent 04f06f9d4d
commit 2a683c5b9c
5 changed files with 59 additions and 26 deletions

View File

@ -20,7 +20,7 @@ public class WeatherController {
* @return * @return
*/ */
@GetMapping("/init") @GetMapping("/init")
public boolean init() { public int init() {
return weatherService.init(); return weatherService.init();
} }
@ -44,7 +44,7 @@ public class WeatherController {
* @return * @return
*/ */
@PostMapping("/{temperature}/{humidity}") @PostMapping("/{temperature}/{humidity}")
public int saveWeather(@PathVariable int temperature, @PathVariable float humidity) { public int saveWeather(@PathVariable float temperature, @PathVariable int humidity) {
return weatherService.save(temperature, humidity); return weatherService.save(temperature, humidity);
} }

View File

@ -7,15 +7,17 @@ import java.util.List;
public interface WeatherMapper { public interface WeatherMapper {
int insert(Weather weather); void createDB();
int batchInsert(List<Weather> weatherList); void createSuperTable();
void createTable();
List<Weather> select(@Param("limit") Long limit, @Param("offset") Long offset); List<Weather> select(@Param("limit") Long limit, @Param("offset") Long offset);
void createDB(); int insert(Weather weather);
void createTable(); int batchInsert(List<Weather> weatherList);
int count(); int count();

View File

@ -13,19 +13,16 @@
create database if not exists test; create database if not exists test;
</update> </update>
<update id="createTable"> <update id="createSuperTable">
create table if not exists test.weather(ts timestamp, temperature int, humidity float); create table if not exists test.weather(ts timestamp, temporary float, humidity int) tags(location nchar(64), groupId int)
</update> </update>
<sql id="Base_Column_List"> <update id="createTable" parameterType="com.taosdata.example.springbootdemo.domain.Weather">
ts, temperature, humidity create table test.t#{groupId} using test.weather tags(#{location}, #{groupId})
</sql> </update>
<select id="select" resultMap="BaseResultMap"> <select id="select" resultMap="BaseResultMap">
select select * from test.weather order by ts desc
<include refid="Base_Column_List"/>
from test.weather
order by ts desc
<if test="limit != null"> <if test="limit != null">
limit #{limit,jdbcType=BIGINT} limit #{limit,jdbcType=BIGINT}
</if> </if>
@ -50,7 +47,7 @@
</select> </select>
<select id="count"> <select id="count">
select count(*) from test.weather; select count(*) from test.weather
</select> </select>
</mapper> </mapper>

View File

@ -6,12 +6,21 @@ import java.sql.Timestamp;
public class Weather { public class Weather {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS",timezone = "GMT+8") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS", timezone = "GMT+8")
private Timestamp ts; private Timestamp ts;
private float temperature;
private int humidity;
private String location;
private int groupId;
private int temperature; public Weather() {
}
private float humidity; public Weather(Timestamp ts, float temperature, int humidity) {
this.ts = ts;
this.temperature = temperature;
this.humidity = humidity;
}
public Timestamp getTs() { public Timestamp getTs() {
return ts; return ts;
@ -21,19 +30,35 @@ public class Weather {
this.ts = ts; this.ts = ts;
} }
public int getTemperature() { public float getTemperature() {
return temperature; return temperature;
} }
public void setTemperature(int temperature) { public void setTemperature(float temperature) {
this.temperature = temperature; this.temperature = temperature;
} }
public float getHumidity() { public int getHumidity() {
return humidity; return humidity;
} }
public void setHumidity(float humidity) { public void setHumidity(int humidity) {
this.humidity = humidity; this.humidity = humidity;
} }
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getGroupId() {
return groupId;
}
public void setGroupId(int groupId) {
this.groupId = groupId;
}
} }

View File

@ -5,25 +5,34 @@ import com.taosdata.example.springbootdemo.domain.Weather;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.sql.Timestamp;
import java.util.List; import java.util.List;
import java.util.Random;
@Service @Service
public class WeatherService { public class WeatherService {
@Autowired @Autowired
private WeatherMapper weatherMapper; private WeatherMapper weatherMapper;
private Random random = new Random(System.currentTimeMillis());
public boolean init() { public int init() {
weatherMapper.createDB(); weatherMapper.createDB();
weatherMapper.createSuperTable();
weatherMapper.createTable(); weatherMapper.createTable();
return true; long ts = System.currentTimeMillis();
int count = 0;
for (int i = 0; i < 10; i++) {
count += weatherMapper.insert(new Weather(new Timestamp(ts + (1000 * i)), 30 * random.nextFloat(), random.nextInt(100)));
}
return count;
} }
public List<Weather> query(Long limit, Long offset) { public List<Weather> query(Long limit, Long offset) {
return weatherMapper.select(limit, offset); return weatherMapper.select(limit, offset);
} }
public int save(int temperature, float humidity) { public int save(float temperature, int humidity) {
Weather weather = new Weather(); Weather weather = new Weather();
weather.setTemperature(temperature); weather.setTemperature(temperature);
weather.setHumidity(humidity); weather.setHumidity(humidity);