提交 df8ec6ad authored 作者: 陈世营's avatar 陈世营

update mybatis

上级 003df6d1
......@@ -28,8 +28,8 @@
<spring-tuple.version>1.0.0.RELEASE</spring-tuple.version>
<druid.version>1.1.12</druid.version>
<mybatis-spring.version>1.3.2</mybatis-spring.version>
<mybatis-plus.version>3.0.4</mybatis-plus.version>
<!-- <mybatis-spring.version>1.3.2</mybatis-spring.version>-->
<!-- <mybatis-plus.version>3.0.4</mybatis-plus.version>-->
<mybatis.version>3.4.6</mybatis.version>
<loit-build-spi-common.version>1.0-SNAPSHOT</loit-build-spi-common.version>
......@@ -44,12 +44,12 @@
<!--Own dependencies -->
<dependency>
<groupId>com.timeloit.project</groupId>
<groupId>com.loit.project</groupId>
<artifactId>loit-build-spi-common</artifactId>
<version>${loit-build-spi-common.version}</version>
</dependency>
<dependency>
<groupId>com.timeloit.project</groupId>
<groupId>com.loit.project</groupId>
<artifactId>loit-build-web-common</artifactId>
<version>${loit-build-web-common.version}</version>
</dependency>
......@@ -64,16 +64,22 @@
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.mybatis.spring.boot</groupId>-->
<!-- <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!-- <version>${mybatis-spring.version}</version>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
<!-- <dependency>-->
<!-- <groupId>com.baomidou</groupId>-->
<!-- <artifactId>mybatis-plus</artifactId>-->
<!-- <version>${mybatis-plus.version}</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatis-plus.version}</version>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
......@@ -125,6 +131,12 @@
<version>${swagger-models.version}</version>
</dependency>
<dependency>
<groupId>com.timeloit.project</groupId>
<artifactId>loit-component-jetcache-client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
......@@ -154,12 +166,12 @@
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://39.100.254.140:12010/repository/maven-releases/</url>
<url>http://113.208.119.11:9101/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://39.100.254.140:12010/repository/maven-snapshots/</url>
<url>http://113.208.119.11:9101/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
......@@ -167,7 +179,7 @@
<repository>
<id>nexus-loit-dev</id>
<name>Nexus Repository</name>
<url>http://39.100.254.140:12010/repository/maven-public/</url>
<url>http://113.208.119.11:9101/repository/maven-public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
......@@ -180,7 +192,7 @@
<pluginRepository>
<id>nexus-loit-dev</id>
<name>Nexus Plugin Repository</name>
<url>http://39.100.254.140:12010/repository/maven-public/</url>
<url>http://113.208.119.11:9101/repository/maven-public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
......@@ -189,4 +201,5 @@
</releases>
</pluginRepository>
</pluginRepositories>
</project>
\ No newline at end of file
......@@ -16,14 +16,18 @@
<dependencies>
<dependency>
<groupId>com.timeloit.project</groupId>
<groupId>com.loit.project</groupId>
<artifactId>loit-build-spi-common</artifactId>
</dependency>
<dependency>
<groupId>com.timeloit.project</groupId>
<groupId>com.loit.project</groupId>
<artifactId>loit-build-web-common</artifactId>
</dependency>
<dependency>
<groupId>com.timeloit.project</groupId>
<artifactId>loit-component-jetcache-client</artifactId>
</dependency>
<!--common-->
<dependency>
......@@ -114,14 +118,19 @@
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.mybatis.spring.boot</groupId>-->
<!-- <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.baomidou</groupId>-->
<!-- <artifactId>mybatis-plus</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
......@@ -149,6 +158,8 @@
<artifactId>swagger-models</artifactId>
</dependency>
</dependencies>
<build>
......
......@@ -13,6 +13,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@MapperScan({"com.loit.mybatis.example"})
//@SpringBootApplication(scanBasePackages = {"com.loit"}, exclude = DataSourceAutoConfiguration.class)
//@SpringBootApplication
@SpringBootApplication(scanBasePackages = {"com.loit.mybatis.example"})
@Slf4j
public class LoitMybatisExampleApplication {
......
......@@ -2,13 +2,14 @@ package com.loit.mybatis.example.controller;
import com.loit.mybatis.example.dto.CommodityDTO;
import com.loit.mybatis.example.entity.TStorage;
import com.loit.mybatis.example.response.ObjectResponse;
import com.loit.mybatis.example.service.ITStorageCacheService;
import com.loit.mybatis.example.service.ITStorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* 前端控制器
*/
......@@ -18,8 +19,12 @@ import javax.annotation.Resource;
public class TStorageController {
@Resource(name = "tStorageServiceImpl")
private ITStorageService storageService;
@Autowired
private ITStorageService iTStorageService;
@Autowired
private ITStorageCacheService iTStorageCacheService;
/**
* 扣减库存
......@@ -27,7 +32,7 @@ public class TStorageController {
@PostMapping("dec_storage")
ObjectResponse decreaseStorage(@RequestBody CommodityDTO commodityDTO) {
log.info("请求库存微服务:{}", commodityDTO.toString());
return storageService.decreaseStorage(commodityDTO);
return iTStorageService.decreaseStorage(commodityDTO);
}
......@@ -40,7 +45,22 @@ public class TStorageController {
commodityDTO.setName("水杯");
log.info("请求库存微服务:{}", commodityDTO.toString());
return storageService.decreaseStorage(commodityDTO);
return iTStorageService.decreaseStorage(commodityDTO);
}
@GetMapping("testCache")
void testCache(Integer storageId) {
TStorage storageCache = iTStorageCacheService.getStorageCache(storageId);
log.info(storageCache.toString());
}
@GetMapping("testCacheRemove")
void testCacheRemove(Integer storageId) {
iTStorageCacheService.removeCache(storageId);
}
}
package com.loit.mybatis.example.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
//默认映射表明t_storage 也可以指定 tablename
@TableName("t_storage")
public class TStorage extends Model<TStorage> {
private static final long serialVersionUID = 1L;
//默认 ID 雪花算法Long
//如果 字段默认叫ID 且雪花算法 可不加注解
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
//默认数据库中字段是下划线commodity_code
// condition 配合查询参数是实体类型
@TableField(condition = SqlCondition.LIKE)
private String commodityCode;
//如果数据库字段和表字段不一致需要使用注解 @TableField
@TableField("name")
private String name;
private Integer count;
//
//
//
//
/**
* 排除非表字段
* private static String remark;
* <p>
* 加 transient 后不参与序列号
* private transient String remark;
*/
@TableField(exist = false)
private String remark;
@Override
protected Serializable pkVal() {
return this.id;
}
@Override
public String toString() {
return "TStorage{" +
", id=" + id +
", commodityCode=" + commodityCode +
", name=" + name +
", count=" + count +
"}";
}
}
......@@ -15,4 +15,8 @@ public interface TStorageMapper extends BaseMapper<TStorage> {
* @Return:
*/
int decreaseStorage(@Param("commodityCode") String commodityCode, @Param("count") Integer count);
// @Select("select * from t_storage ${ew.customSqlSegment}")
// List<TStorage> selectAll(@Param(Constants.WRAPPER) Wrapper<TStorage> wrapper);
}
package com.loit.mybatis.example.service;
import com.loit.mybatis.example.entity.TStorage;
/**
* 仓库Cache服务
*/
public interface ITStorageCacheService {
TStorage getStorageCache(Integer storageId);
void removeCache(Integer storageId);
}
......@@ -6,6 +6,8 @@ import com.loit.mybatis.example.dto.CommodityDTO;
import com.loit.mybatis.example.entity.TStorage;
import com.loit.mybatis.example.response.ObjectResponse;
import java.util.List;
/**
* 仓库服务
*/
......@@ -15,4 +17,18 @@ public interface ITStorageService extends IService<TStorage> {
* 扣减库存
*/
ObjectResponse decreaseStorage(CommodityDTO commodityDTO);
/**
* 查询全部库存
*/
List<TStorage> findAll();
/**
* 分页查询库存信息
*
* @PageNum 当前页
* @PageSize 每页显示数量
*/
List<TStorage> findPage(Long pageNum, Long pageSize);
}
package com.loit.mybatis.example.service.impl;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.RefreshPolicy;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CreateCache;
import com.loit.mybatis.example.entity.TStorage;
import com.loit.mybatis.example.service.ITStorageCacheService;
import com.loit.mybatis.example.service.ITStorageService;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
@Service("iTStorageCacheService")
public class ITStorageCacheServiceImpl implements ITStorageCacheService {
@Resource(name = "iTStorageService")
private ITStorageService itStorageService;
@CreateCache(expire = 100, cacheType = CacheType.REMOTE)
private Cache<Integer, TStorage> storageCache;
@Override
public TStorage getStorageCache(Integer storageId) {
return storageCache.get(storageId);
}
@Override
public void removeCache(Integer id) {
storageCache.remove(id);
}
@PostConstruct
public void init() {
// 自动刷新
RefreshPolicy policy = RefreshPolicy.newPolicy(5, TimeUnit.MINUTES).stopRefreshAfterLastAccess(30, TimeUnit.MINUTES);
storageCache.config().setLoader(this::loadMenuByUserFromDatabase);
storageCache.config().setRefreshPolicy(policy);
}
private TStorage loadMenuByUserFromDatabase(Integer id) {
return itStorageService.getById(id);
}
}
package com.loit.mybatis.example.service.impl;
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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loit.mybatis.example.dto.CommodityDTO;
import com.loit.mybatis.example.entity.TStorage;
......@@ -7,16 +10,20 @@ import com.loit.mybatis.example.enums.RspStatusEnum;
import com.loit.mybatis.example.mapper.TStorageMapper;
import com.loit.mybatis.example.response.ObjectResponse;
import com.loit.mybatis.example.service.ITStorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* <p>
* 库存服务实现类
* </p>
*/
@Service(value = "tStorageServiceImpl")
@Service(value = "iTStorageService")
@Transactional(readOnly = true)
@Slf4j
public class TStorageServiceImpl extends ServiceImpl<TStorageMapper, TStorage> implements ITStorageService {
@Override
......@@ -33,6 +40,37 @@ public class TStorageServiceImpl extends ServiceImpl<TStorageMapper, TStorage> i
response.setStatus(RspStatusEnum.FAIL.getCode());
response.setMessage(RspStatusEnum.FAIL.getMessage());
return response;
}
@Override
public List<TStorage> findAll() {
// 方式一:
// List<TStorage> allStorage = this.list(null);
// 方式二:
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>();
List<TStorage> allStorage = this.list(queryWrapper);
// 方式三:
// TStorage storage = new TStorage();
// List<TStorage> allStorage = storage.selectAll();
return allStorage;
}
@Override
public List<TStorage> findPage(Long pageNum, Long pageSize) {
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>();
Page<TStorage> page = new Page<>(pageNum, pageSize);
//分页数据
IPage<TStorage> pageInfo = this.page(page, queryWrapper);
return pageInfo.getRecords();
}
}
......@@ -49,5 +49,45 @@ spring:
username: root
validation-query: SELECT 1
mybatis:
#mybatis:
# mapper-locations: classpath*:/mapper/*.xml
# # 实体扫描,多个package用逗号或者分号分隔
# type-aliases-package: com.loit.mybatis.example
# configuration:
# # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus:
mapper-locations: classpath*:/mapper/*.xml
# jetcache使用
jetcache:
areaInCacheName: false
# 控制台输出统计数据,统计间隔,0表示不统计
statIntervalMinutes: 1
local:
default:
limit: 300
expireAfterWriteInMillis: 100000
# 缓存类型。tair、redis为当前支持的远程缓存;linkedhashmap、caffeine为当前支持的本地缓存类型
type: linkedhashmap
keyConvertor: fastjson
remote:
default:
keyConvertor: fastjson
valueEncoder: java
valueDecoder: java
poolConfig:
minIdle: 5
maxIdle: 20
maxTotal: 50
type: redis.lettuce
uri: redis://portal2019@39.100.254.140:6379/
#redis://password@127.0.0.1:6379/0
logging:
level:
root: warn
com.loit.mybatis: trace
pattern:
console: '%p%m%n'
......@@ -22,4 +22,5 @@
update t_storage set count = count-${count} where commodity_code = #{commodityCode}
</update>
</mapper>
package com.loit.mybatis.example.dao;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.loit.mybatis.example.entity.TStorage;
import com.loit.mybatis.example.mapper.TStorageMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
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.SpringRunner;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class SimpleTest {
@Autowired
private TStorageMapper tStorageMapper;
@Test
public void select() {
List<TStorage> storageList = tStorageMapper.selectList(null);
log.info(storageList.toString());
}
@Test
public void insert() {
TStorage storage = new TStorage();
storage.setName("pod1").setCount(9999).setCommodityCode("0002");
int count = tStorageMapper.insert(storage);
log.info(String.valueOf(count));
}
@Test
public void selectById() {
TStorage storage = tStorageMapper.selectById(1);
log.info(storage.toString());
}
@Test
public void selectByIds() {
List<Integer> ids = Arrays.asList(1, 2);
List<TStorage> storageList = tStorageMapper.selectBatchIds(ids);
log.info(storageList.toString());
}
@Test
public void selectByMap() {
Map<String, Object> columnMap = new HashMap<>();
columnMap.put("commodity_code", "0002");
columnMap.put("name", "pod1");
List<TStorage> storageList = tStorageMapper.selectByMap(columnMap);
log.info(storageList.toString());
}
/**
* 测试条件构造器
* DEBUG==> Preparing: SELECT id,commodity_code,name,count FROM t_storage WHERE commodity_code LIKE ? AND count < ?
* DEBUG==> Parameters: C%(String), 888(Integer)
*/
@Test
public void selectByWrapper() {
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>();
queryWrapper.likeRight("commodity_code", "C");
//queryWrapper.like("commodity_code", "C");
queryWrapper.lt("count", 888);
List<TStorage> storageList = tStorageMapper.selectList(queryWrapper);
log.info(storageList.toString());
}
/**
* DEBUG==> Preparing: SELECT id,commodity_code,name,count FROM t_storage WHERE commodity_code LIKE ? OR count < ? ORDER BY commodity_code DESC , id ASC
* DEBUG==> Parameters: C%(String), 888(Integer)
*/
@Test
public void selectByWrapperTwo() {
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>();
queryWrapper.likeRight("commodity_code", "C").or();
//queryWrapper.like("commodity_code", "C");
queryWrapper.lt("count", 888).orderByDesc("commodity_code").orderByAsc("id");
List<TStorage> storageList = tStorageMapper.selectList(queryWrapper);
log.info(storageList.toString());
}
/**
* DEBUG==> Preparing: SELECT id,commodity_code,name,count FROM t_storage WHERE date_format(create_time,'%Y-%m-%d') = ?
* DEBUG==> Parameters: 2020-02-10(String)
*/
@Test
public void selectByWrapperThree() {
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>();
queryWrapper.apply("date_format(create_time,'%Y-%m-%d') = {0}", "2020-02-10");
//queryWrapper.inSql("commodity_code","select commodity_code from xxxx where ");
List<TStorage> storageList = tStorageMapper.selectList(queryWrapper);
log.info(storageList.toString());
}
/**
* SELECT id,commodity_code,name,count FROM t_storage WHERE commodity_code LIKE ? AND ( count < ? ) ORDER BY commodity_code DESC , id ASC
*/
@Test
public void selectByWrapperFour() {
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>();
queryWrapper.likeRight("commodity_code", "C").and(qw -> qw.lt("count", 888));
queryWrapper.orderByDesc("commodity_code").orderByAsc("id");
List<TStorage> storageList = tStorageMapper.selectList(queryWrapper);
log.info(storageList.toString());
}
/**
* SELECT id,commodity_code,name,count FROM t_storage WHERE ( commodity_code LIKE ? OR count < ? ) AND id = ? ORDER BY commodity_code DESC , id ASC
*/
@Test
public void selectByWrapperFive() {
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>();
queryWrapper.nested(qw -> qw.likeRight("commodity_code", "C").or().lt("count", 888));
queryWrapper.eq("id", 2);
queryWrapper.orderByDesc("commodity_code").orderByAsc("id");
List<TStorage> storageList = tStorageMapper.selectList(queryWrapper);
log.info(storageList.toString());
}
/**
* DEBUG==> Preparing: SELECT id,commodity_code FROM t_storage WHERE id IN (?,?) limit 1
* DEBUG==> Parameters: 1(Integer), 2(Integer)
*/
@Test
public void selectByWrapperSix() {
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>();
queryWrapper.in("id", Arrays.asList(1, 2)).last(" limit 1");
List<TStorage> storageList = tStorageMapper.selectList(queryWrapper);
log.info(storageList.toString());
}
@Test
public void selectByWrapperSeven() {
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>();
queryWrapper.select("id", "commodity_code").in("id", Arrays.asList(1, 2)).last(" limit 1");
List<TStorage> storageList = tStorageMapper.selectList(queryWrapper);
log.info(storageList.toString());
}
/**
* DEBUG==> Preparing: SELECT id,commodity_code,count FROM t_storage WHERE id IN (?,?) limit 1
* DEBUG==> Parameters: 1(Integer), 2(Integer)
*/
@Test
public void selectByWrapperEight() {
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>();
queryWrapper.in("id", Arrays.asList(1, 2)).last(" limit 1");
queryWrapper.select(TStorage.class, info -> !info.getColumn().equals("create_time") && !info.getColumn().equals("name"));
List<TStorage> storageList = tStorageMapper.selectList(queryWrapper);
log.info(storageList.toString());
}
/**
* 条件表达式
* DEBUG==> Preparing: SELECT id,commodity_code,name,count FROM t_storage WHERE 1=1 ORDER BY commodity_code DESC , id ASC
* DEBUG==> Parameters:
*/
@Test
public void selectByWrapperNight() {
String commodityCode = "";
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>();
queryWrapper.likeRight(StringUtils.isNotEmpty(commodityCode), "commodity_code", "C");
queryWrapper.orderByDesc("commodity_code").orderByAsc("id");
List<TStorage> storageList = tStorageMapper.selectList(queryWrapper);
log.info(storageList.toString());
}
/**
* DEBUG==> Preparing: SELECT commodity_code,name,count,id FROM t_storage WHERE commodity_code=? AND 1=1 ORDER BY commodity_code DESC , id ASC
* DEBUG==> Parameters: 0002(String)
*
* @TableField(condition = SqlCondition.LIKE)
* DEBUG==> Preparing: SELECT commodity_code,name,count,id FROM t_storage WHERE commodity_code LIKE CONCAT('%',?,'%') AND 1=1 ORDER BY commodity_code DESC , id ASC
* DEBUG==> Parameters: 0002(String)
*/
@Test
public void selectByWrapperEntity() {
TStorage whereStorage = new TStorage();
whereStorage.setCommodityCode("0002");
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>(whereStorage);
queryWrapper.orderByDesc("commodity_code").orderByAsc("id");
List<TStorage> storageList = tStorageMapper.selectList(queryWrapper);
log.info(storageList.toString());
}
/**
* DEBUG==> Preparing: SELECT id,commodity_code,name,count FROM t_storage WHERE commodity_code = ? AND name IS NULL
* DEBUG==> Parameters: 0002(String)
* <p>
* queryWrapper.allEq(params, false);
* <p>
* DEBUG==> Preparing: SELECT id,commodity_code,name,count FROM t_storage WHERE commodity_code = ?
* DEBUG==> Parameters: 0002(String)
*/
@Test
public void selectByWrapperAllEq() {
Map<String, Object> params = new HashMap<>();
params.put("commodity_code", "0002");
params.put("name", null);
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>();
//queryWrapper.allEq(params);
//queryWrapper.allEq(params, true);
//queryWrapper.allEq(params, false);
queryWrapper.allEq((k, v) -> !k.equals("commodity_code"), params, false);
List<TStorage> storageList = tStorageMapper.selectList(queryWrapper);
log.info(storageList.toString());
}
/**
* DEBUG==> Preparing: SELECT commodity_code FROM t_storage WHERE commodity_code = ?
* DEBUG==> Parameters: 0002(String)
*/
@Test
public void selectByWrapperSelectMap() {
Map<String, Object> params = new HashMap<>();
params.put("commodity_code", "0002");
params.put("name", null);
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>();
queryWrapper.allEq(params, false).select("commodity_code");
List<Map<String, Object>> maps = tStorageMapper.selectMaps(queryWrapper);
log.info(maps.toString());
}
/**
* DEBUG==> Preparing: SELECT avg(count) avg1,avg(count) avg2 FROM t_storage WHERE 1=1 GROUP BY commodity_code HAVING sum(count) <?
* DEBUG==> Parameters: 999999(Integer)
*/
@Test
public void selectByWrapperSelectMapTwo() {
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>();
queryWrapper.select("avg(count) avg1", "avg(count) avg2");
queryWrapper.groupBy("commodity_code").having("sum(count) <{0}", 999999);
List<Map<String, Object>> maps = tStorageMapper.selectMaps(queryWrapper);
log.info(maps.toString());
}
/**
* selectObjs 是有一列数据
*/
@Test
public void selectByWrapperSelectMapObject() {
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>();
queryWrapper.select("avg(count) avg1", "avg(count) avg2");
queryWrapper.groupBy("commodity_code").having("sum(count) <{0}", 999999);
List<Object> maps = tStorageMapper.selectObjs(queryWrapper);
log.info(maps.toString());
}
/**
* DEBUG==> Preparing: SELECT COUNT(1) FROM t_storage WHERE commodity_code LIKE CONCAT('%',?,'%')
* DEBUG==> Parameters: 0002(String)
*/
@Test
public void selectByWrapperSelectCount() {
TStorage whereStorage = new TStorage();
whereStorage.setCommodityCode("0002");
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>(whereStorage);
Integer integer = tStorageMapper.selectCount(queryWrapper);
log.info(String.valueOf(integer));
}
@Test
public void selectByWrapperSelectOne() {
TStorage whereStorage = new TStorage();
whereStorage.setCommodityCode("0002");
QueryWrapper<TStorage> queryWrapper = new QueryWrapper<>(whereStorage);
TStorage storage = tStorageMapper.selectOne(queryWrapper);
log.info(storage.toString());
}
/**
* DEBUG==> Preparing: SELECT id,commodity_code,name,count FROM t_storage WHERE commodity_code LIKE ?
* DEBUG==> Parameters: %C%(String)
*/
@Test
public void selectByWrapperSelectLambda() {
LambdaQueryWrapper<TStorage> lambda = new QueryWrapper<TStorage>().lambda();
// LambdaQueryWrapper<TStorage> tStorageLambdaQueryWrapper = new LambdaQueryWrapper<>();
lambda.like(TStorage::getCommodityCode, "C");
List<TStorage> storageList = tStorageMapper.selectList(lambda);
log.info(storageList.toString());
}
/**
* 自定义
*
* @Select("select * from t_storage ${ew.customSqlSeg}")
* List<TStorage> selectAll(@Param(Constants.WRAPPER) Wrapper<TStorage> wrapper);
*/
// @Test
// public void selectByWrapperSelectMy() {
//
// LambdaQueryWrapper<TStorage> lambda = new QueryWrapper<TStorage>().lambda();
// lambda.like(TStorage::getCommodityCode, "C");
//
// List<TStorage> storageList = tStorageMapper.selectAll(lambda);
// log.info(storageList.toString());
// }
}
package com.loit.mybatis.example;
\ No newline at end of file
package com.loit.mybatis.example.service;
import com.loit.mybatis.example.entity.TStorage;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class ITStorageServiceTest {
@Resource(name = "iTStorageService")
private ITStorageService itStorageService;
@Test
public void testFindAll() {
List<TStorage> allDate = itStorageService.findAll();
log.info(allDate.toString());
}
@Test
public void testFindPage() {
List<TStorage> allDate = itStorageService.findPage(0L, 20L);
log.info(allDate.toString());
}
@Test
public void testGetById() {
TStorage storage = itStorageService.getById(1);
log.info(storage.toString());
}
}
/*
Navicat MySQL Data Transfer
Source Server : 39.98.202.173seata
Source Server Version : 50725
Source Host : 39.98.202.173:3306
Source Database : loit-example-seata
Target Server Type : MYSQL
Target Server Version : 50725
File Encoding : 65001
Date: 2020-02-10 09:21:32
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for t_storage
-- ----------------------------
DROP TABLE IF EXISTS `t_storage`;
CREATE TABLE "t_storage" (
"id" int(11) NOT NULL AUTO_INCREMENT,
"commodity_code" varchar(255) DEFAULT NULL,
"name" varchar(255) DEFAULT NULL,
"count" int(11) DEFAULT '0',
"create_time" datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY ("id"),
UNIQUE KEY "commodity_code" ("commodity_code")
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论