change
This commit is contained in:
parent
5349141dca
commit
c6c7ab3bdd
|
@ -75,6 +75,24 @@
|
|||
</dependencies>
|
||||
|
||||
<build>
|
||||
<resources>
|
||||
<!-- <resource>-->
|
||||
<!-- <directory>src/main/resources</directory>-->
|
||||
<!-- <includes>-->
|
||||
<!-- <include>**/*.properties</include>-->
|
||||
<!-- <include>**/*.xml</include>-->
|
||||
<!-- </includes>-->
|
||||
<!-- <filtering>true</filtering>-->
|
||||
<!-- </resource>-->
|
||||
<resource>
|
||||
<directory>src/main/java</directory>
|
||||
<includes>
|
||||
<include>**/*.properties</include>
|
||||
<include>**/*.xml</include>
|
||||
</includes>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -7,6 +7,8 @@ import org.springframework.context.annotation.Configuration;
|
|||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
|
||||
/** mybatis 3.4.1 pagination config start ***/
|
||||
// @Bean
|
||||
// public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
// MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
|
@ -21,7 +23,12 @@ public class MybatisPlusConfig {
|
|||
|
||||
@Bean
|
||||
public PaginationInterceptor paginationInterceptor() {
|
||||
return new PaginationInterceptor();
|
||||
// return new PaginationInterceptor();
|
||||
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
|
||||
//TODO: mybatis-plus do not support TDengine, use mysql Dialect
|
||||
paginationInterceptor.setDialectType("mysql");
|
||||
|
||||
return paginationInterceptor;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,39 +0,0 @@
|
|||
package com.taosdata.example.mybatisplusdemo.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.taosdata.example.mybatisplusdemo.domain.Weather;
|
||||
import com.taosdata.example.mybatisplusdemo.mapper.WeatherMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/weathers")
|
||||
public class WeatherController {
|
||||
|
||||
@Autowired
|
||||
private WeatherMapper mapper;
|
||||
|
||||
@GetMapping
|
||||
public List<Weather> findAll() {
|
||||
Integer total = mapper.selectCount(null);
|
||||
final int pageSize = 3;
|
||||
IPage<Weather> page = new Page<>(1, pageSize);
|
||||
|
||||
IPage<Weather> currentPage = mapper.selectPage(page, null);
|
||||
|
||||
System.out.println("total : " + currentPage.getTotal());
|
||||
System.out.println("pages : " + currentPage.getPages());
|
||||
|
||||
// System.out.println("countId : " + currentPage.getCountId());
|
||||
// System.out.println("maxLimit: " + currentPage.getMaxLimit());
|
||||
|
||||
return currentPage.getRecords();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.taosdata.example.mybatisplusdemo.domain;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
@Data
|
||||
public class Temperature {
|
||||
|
||||
private Timestamp ts;
|
||||
private float temperature;
|
||||
private String location;
|
||||
private int tbIndex;
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.taosdata.example.mybatisplusdemo.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.taosdata.example.mybatisplusdemo.domain.Temperature;
|
||||
|
||||
public interface TemperatureMapper extends BaseMapper<Temperature> {
|
||||
|
||||
int createSuperTable();
|
||||
|
||||
int createTable(String tbName, String location);
|
||||
|
||||
void dropSuperTable();
|
||||
|
||||
int insertOne(Temperature one);
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
|
||||
<mapper namespace="com.taosdata.example.mybatisplusdemo.mapper.TemperatureMapper">
|
||||
|
||||
<update id="createSuperTable">
|
||||
CREATE TABLE if not exists temperature(ts timestamp, temperature float) tags(location nchar(64), tbIndex int);
|
||||
</update>
|
||||
|
||||
<update id="createTable" parameterType="java.lang.String">
|
||||
create table #{tbname} using temperature tags( #{location} );
|
||||
</update>
|
||||
|
||||
<update id="dropSuperTable">
|
||||
drop table if exists temperature
|
||||
</update>
|
||||
|
||||
<insert id="insertOne" parameterType="com.taosdata.example.mybatisplusdemo.domain.Temperature">
|
||||
insert into t${tbIndex}(ts, temperature) values(ts, temperature)
|
||||
</insert>
|
||||
|
||||
</mapper>
|
|
@ -1,8 +1,6 @@
|
|||
package com.taosdata.example.mybatisplusdemo.mapper;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.taosdata.example.mybatisplusdemo.domain.Weather;
|
||||
|
||||
public interface WeatherMapper extends BaseMapper<Weather> {
|
||||
|
|
|
@ -1,17 +1,25 @@
|
|||
spring:
|
||||
datasource:
|
||||
# driver-class-name: com.taosdata.jdbc.TSDBDriver
|
||||
# url: jdbc:TAOS://localhost:6030/mp_test
|
||||
# user: root
|
||||
# password: taosdata
|
||||
# charset: UTF-8
|
||||
# locale: en_US.UTF-8
|
||||
# timezone: UTC-8
|
||||
# driver-class-name: org.h2.Driver
|
||||
# schema: classpath:db/schema-mysql.sql
|
||||
# data: classpath:db/data-mysql.sql
|
||||
# url: jdbc:h2:mem:test
|
||||
# username: root
|
||||
# password: test
|
||||
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://master:3306/test?useSSL=false
|
||||
url: jdbc:mysql://master:3306/test?useSSL=false
|
||||
username: root
|
||||
password: 123456
|
||||
|
||||
# driver-class-name: com.taosdata.jdbc.TSDBDriver
|
||||
# url: jdbc:TAOS://localhost:6030/mp_test
|
||||
# user: root
|
||||
# password: taosdata
|
||||
# charset: UTF-8
|
||||
# locale: en_US.UTF-8
|
||||
# timezone: UTC-8
|
||||
|
||||
logging:
|
||||
level:
|
||||
com:
|
||||
|
|
|
@ -0,0 +1,119 @@
|
|||
package com.taosdata.example.mybatisplusdemo.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.taosdata.example.mybatisplusdemo.domain.Temperature;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest
|
||||
public class TemperatureMapperTest {
|
||||
|
||||
private static Random random = new Random(System.currentTimeMillis());
|
||||
private static String[] locations = {"北京", "上海", "深圳", "广州", "杭州"};
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
// create table temperature
|
||||
mapper.createSuperTable();
|
||||
// create table t_X using temperature
|
||||
for (int i = 0; i < 10; i++) {
|
||||
mapper.createTable("t_" + i, locations[random.nextInt(locations.length)]);
|
||||
}
|
||||
// insert into table
|
||||
int affectRows = 0;
|
||||
// insert 10 tables
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// each table insert 5 rows
|
||||
for (int j = 0; j < 5; j++) {
|
||||
Temperature one = new Temperature();
|
||||
one.setTs(new Timestamp(1605024000000l));
|
||||
one.setTemperature(random.nextFloat() * 50);
|
||||
one.setLocation("望京");
|
||||
one.setTbIndex(i);
|
||||
affectRows += mapper.insertOne(one);
|
||||
}
|
||||
}
|
||||
Assert.assertEquals(50, affectRows);
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
mapper.dropSuperTable();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private TemperatureMapper mapper;
|
||||
|
||||
@Test
|
||||
public void testSelectList() {
|
||||
List<Temperature> temperatureList = mapper.selectList(null);
|
||||
temperatureList.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsert() {
|
||||
Temperature one = new Temperature();
|
||||
one.setTs(new Timestamp(1605024000000l));
|
||||
one.setTemperature(random.nextFloat() * 50);
|
||||
one.setLocation("望京");
|
||||
int affectRows = mapper.insertOne(one);
|
||||
Assert.assertEquals(1, affectRows);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectOne() {
|
||||
QueryWrapper<Temperature> wrapper = new QueryWrapper<>();
|
||||
wrapper.eq("location", "beijing");
|
||||
Temperature one = mapper.selectOne(wrapper);
|
||||
System.out.println(one);
|
||||
Assert.assertEquals(12.22f, one.getTemperature(), 0.00f);
|
||||
Assert.assertEquals("beijing", one.getLocation());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectByMap() {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("location", "beijing");
|
||||
List<Temperature> temperatures = mapper.selectByMap(map);
|
||||
Assert.assertEquals(1, temperatures.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectObjs() {
|
||||
List<Object> ts = mapper.selectObjs(null);
|
||||
System.out.println(ts);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectCount() {
|
||||
int count = mapper.selectCount(null);
|
||||
Assert.assertEquals(5, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectPage() {
|
||||
IPage page = new Page(1, 2);
|
||||
IPage<Temperature> temperatureIPage = mapper.selectPage(page, null);
|
||||
System.out.println("total : " + temperatureIPage.getTotal());
|
||||
System.out.println("pages : " + temperatureIPage.getPages());
|
||||
for (Temperature temperature : temperatureIPage.getRecords()) {
|
||||
System.out.println(temperature);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -28,19 +28,18 @@ public class WeatherMapperTest {
|
|||
|
||||
@Test
|
||||
public void testSelectList() {
|
||||
List<Weather> weatherList = mapper.selectList(null);
|
||||
// Assert.assertEquals(5, weatherList.size());
|
||||
weatherList.forEach(System.out::println);
|
||||
List<Weather> weathers = mapper.selectList(null);
|
||||
weathers.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsert() {
|
||||
Weather weather = new Weather();
|
||||
weather.setTs(new Timestamp(1605024000000l));
|
||||
weather.setTemperature(random.nextFloat() * 50);
|
||||
weather.setHumidity(random.nextInt(100));
|
||||
weather.setLocation("望京");
|
||||
int affectRows = mapper.insert(weather);
|
||||
Weather one = new Weather();
|
||||
one.setTs(new Timestamp(1605024000000l));
|
||||
one.setTemperature(random.nextFloat() * 50);
|
||||
one.setHumidity(random.nextInt(100));
|
||||
one.setLocation("望京");
|
||||
int affectRows = mapper.insert(one);
|
||||
Assert.assertEquals(1, affectRows);
|
||||
}
|
||||
|
||||
|
@ -48,11 +47,10 @@ public class WeatherMapperTest {
|
|||
public void testSelectOne() {
|
||||
QueryWrapper<Weather> wrapper = new QueryWrapper<>();
|
||||
wrapper.eq("location", "beijing");
|
||||
Weather weather = mapper.selectOne(wrapper);
|
||||
System.out.println(weather);
|
||||
Assert.assertEquals(12.22f, weather.getTemperature(), 0.00f);
|
||||
Assert.assertEquals(45, weather.getHumidity());
|
||||
Assert.assertEquals("beijing", weather.getLocation());
|
||||
Weather one = mapper.selectOne(wrapper);
|
||||
System.out.println(one);
|
||||
Assert.assertEquals(12.22f, one.getTemperature(), 0.00f);
|
||||
Assert.assertEquals("beijing", one.getLocation());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -67,30 +65,24 @@ public class WeatherMapperTest {
|
|||
public void testSelectObjs() {
|
||||
List<Object> ts = mapper.selectObjs(null);
|
||||
System.out.println(ts);
|
||||
// Assert.assertEquals(5, ts.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectCount() {
|
||||
int count = mapper.selectCount(null);
|
||||
Assert.assertEquals(5, count);
|
||||
// Assert.assertEquals(5, count);
|
||||
System.out.println(count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectPage() {
|
||||
// Page<Weather> page = new Page<>(1, 2);
|
||||
|
||||
IPage page = new Page(1, 2);
|
||||
|
||||
IPage<Weather> weatherPage = mapper.selectPage(page, null);
|
||||
// Page<Weather> weatherPage = mapper.selectPage(page, null);
|
||||
|
||||
System.out.println("total : " + weatherPage.getTotal());
|
||||
System.out.println("pages : " + weatherPage.getPages());
|
||||
for (Weather weather : weatherPage.getRecords()) {
|
||||
IPage<Weather> weatherIPage = mapper.selectPage(page, null);
|
||||
System.out.println("total : " + weatherIPage.getTotal());
|
||||
System.out.println("pages : " + weatherIPage.getPages());
|
||||
for (Weather weather : weatherIPage.getRecords()) {
|
||||
System.out.println(weather);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue