小编ip6*_*696的帖子

房间更新(或插入,如果不存在)行和返回计数更改行

我需要更新,如果不存在,则插入行到ROOM DB.

我这样做: productRepository.updateProducts(productsResponse.getProductItems());

和:

@Override
public void updateProducts(final List<ProductItem> products) {
    new Thread(() -> {
        for (ProductItem item : products) {
            Product product = createProduct(item);
            productDao.insert(product);
        }
    }).start();
}
Run Code Online (Sandbox Code Playgroud)

在DAO中:

@Insert
void insert(Product products);
Run Code Online (Sandbox Code Playgroud)

但我有方法

@Update
void update(Product product);
Run Code Online (Sandbox Code Playgroud)

我有一些问题:

  1. 两种方法都是无效的.插入后如何返回保存的Product或boolean flag或insert count?

  2. 如果我试着打电话update而且我没有排它会插入吗?

  3. 如何更新(如果不是 - 插入)行和返回计数updatet或插入的行?

android android-room

21
推荐指数
5
解决办法
3万
查看次数

如何使用配置文件在 docker 中启动 spring boot 应用程序?

我有一个简单的 spring-boot 项目:

-resources
 -application.yaml
 -application-test.yaml
Run Code Online (Sandbox Code Playgroud)

我有这个Dockerfile

FROM openjdk:8-jdk-alpine
EXPOSE 8080
ADD micro-boot.jar micro-boot.jar
ENTRYPOINT ["java","-Dspring.profiles.active=test" "-jar","/micro-boot.jar"]
Run Code Online (Sandbox Code Playgroud)

1)我建立形象 - C:\micro-boot>docker build -f Dockerfile -t micro-boot .

2)显示所有图像 - C:\micro-boot>docker image ls -a

micro-boot   latest  ccc9a75ebc24  4 seconds ago 112MB
Run Code Online (Sandbox Code Playgroud)

3)尝试开始 C:\micro-boot>docker image ls -a

我收到一个错误:

/bin/sh: [java,-Dspring.profiles.active=test: not found
Run Code Online (Sandbox Code Playgroud)

java spring docker spring-boot dockerfile

15
推荐指数
2
解决办法
2万
查看次数

如何在没有 xml 的情况下配置 Ehcache 3 + spring boot + java 配置?

我用Ehcache 2 + spring boot. 这是我的配置:

@Bean
public CacheManager cacheManager() {
    return new EhCacheCacheManager(ehCacheCacheManager().getObject());
}

@Bean
public EhCacheManagerFactoryBean ehCacheCacheManager() {
    EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();
    cmfb.setConfigLocation(new ClassPathResource("ehcache.xml"));
    cmfb.setShared(true);
    return cmfb;
}

ehcache.xml - in resources.
Run Code Online (Sandbox Code Playgroud)

现在我想用Ehcache 3 + spring bootJava的配置,而不是XML,但我还没有发现这方面的任何实例。我的问题:

1)为什么几乎所有的例子都是基于xml 的?这怎么能比java config更好呢?

2)如何在不使用xml的情况下在spring boot中使用java config配置Ehcache 3

java spring ehcache spring-boot

13
推荐指数
2
解决办法
7008
查看次数

如何使android edittext提示中心和左侧光标

如何在EditText的中心设置提示文本并将光标设置在EditText的左侧部分?当我开始输入文本时,它从中心EditText开始?

在此输入图像描述

我现在有

在此输入图像描述

<LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:background="@drawable/line_white"
                android:layout_marginBottom="25dp"
                android:paddingBottom="16dp"
                android:gravity="center_horizontal">

                <EditText
                    android:id="@+id/loginEmailField"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:autoText="false"
                    android:ems="10"
                    android:hint="Choose an email adress"
                    android:inputType="textEmailAddress"
                    android:paddingLeft="2dp"
                    android:singleLine="false"
                    android:textColor="#ffffff"
                    android:textColorHint="#9377ab"
                    android:textSize="19sp"
                    android:textStyle="bold"
                    android:background="#00000000"
                    android:layout_marginRight="2dp"
                    android:gravity="center"/>
            </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android android-edittext

12
推荐指数
2
解决办法
5594
查看次数

如何在Android TextView中使字母间的间距?

我需要从photoshop布局进行设计.布局上有一些字体.设计师给了我这种字体.但是在Photoshop中的布局上,他使用字母之间的间距.我怎么能在android textView中实现这个?我找到了一个解决方案

回答stackoverflow

回答两个

但如果我让它myTextView extends TextView错了.如果我适应一个设计,在具有biger显示的设备上,字母之间的间距增加不成比例.

编辑

public class MyTextView extends TextView {
    private float letterSpacing = 0.0f;
    private CharSequence originalText = "";
    private Typeface typeface;

    public MyTextView(Context context) {
        this(context, null);
        isInEditMode();
    }

    public MyTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        isInEditMode();
    }

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray attributesArray = getResources().obtainAttributes(attrs, R.styleable.MyTextView);
        letterSpacing = attributesArray.getDimension(R.styleable.MyTextView_letterSpacing, 0.0f);
        String fontName = attributesArray.getString(R.styleable.MyTextView_fontName);
        if(!this.isInEditMode()) {
            if (null == fontName) {
                typeface …
Run Code Online (Sandbox Code Playgroud)

android textview

11
推荐指数
2
解决办法
2万
查看次数

spring boot中创建KafkaTemplate的正确方法

我尝试在Spring Boot 应用程序中配置apache kafka。我阅读了此文档并按照以下步骤操作:

1)我将此行添加到aplication.yaml

spring:
  kafka:
    bootstrap-servers: kafka_host:9092
    producer:
      key-serializer: org.apache.kafka.common.serialization.StringDeserializer
      value-serializer: org.apache.kafka.common.serialization.ByteArraySerializer
Run Code Online (Sandbox Code Playgroud)

2)我创建新主题:

    @Bean
    public NewTopic responseTopic() {
        return new NewTopic("new-topic", 5, (short) 1);
    }
Run Code Online (Sandbox Code Playgroud)

现在我想使用KafkaTemplate

private final KafkaTemplate<String, byte[]> kafkaTemplate;

public KafkaEventBus(KafkaTemplate<String, byte[]> kafkaTemplate) {
    this.kafkaTemplate = kafkaTemplate;
}
Run Code Online (Sandbox Code Playgroud)

但 Intellij IDE 强调:

在此处输入图片说明

为了解决这个问题,我需要创建 bean:

@Bean
public KafkaTemplate<String, byte[]> myMessageKafkaTemplate() {
    return new KafkaTemplate<>(greetingProducerFactory());
}
Run Code Online (Sandbox Code Playgroud)

并传递给构造函数属性greetingProducerFactory()

@Bean
public ProducerFactory<String, byte[]> greetingProducerFactory() {
    Map<String, Object> configProps = …
Run Code Online (Sandbox Code Playgroud)

java spring apache-kafka spring-boot spring-kafka

11
推荐指数
2
解决办法
9963
查看次数

如何生成随机值,其中百分比为0?

我创建了一个随机流

Random random = new Random();
Stream<Integer> boxed = random.ints(0, 100000000).boxed();
Run Code Online (Sandbox Code Playgroud)

但我需要将60%的数字生成为0,而其余数字可以是真正随机的.我该怎么做?

编辑:

我只需要正数而且在0到100之间

1
2
0
0
9
0
0
1
12
Run Code Online (Sandbox Code Playgroud)

java random java-8

9
推荐指数
3
解决办法
1525
查看次数

如何在单元测试中模拟JPA存储库的save方法

例如,我在UserService中有以下方法:

  @Override
  @Transactional
  public UserDto create(UserDto userDto) {

    User dbUser = userRepository.findOne(userDto.getId());

    if (dbUser != null) {
      throw new AuthException(AuthException.ErrorCode.DUPLICATE_USER_EXCEPTION);
    }

    User oneByLogin = userRepository.findOneByLogin(userDto.getLogin());
    if (oneByLogin != null) {
      throw new AuthExceptionAuthException.ErrorCode.DUPLICATE_LOGIN_EXCEPTION);
    }

    User newUser = new User();
    newUser.setGuid(UUID.randomUUID().toString());
    newUser.setInsertDate(new Date());
    newUser.setFirstName(userDto.getFirstName());
    newUser.setLastName(userDto.getLastName());
    newUser.setLogin(userDto.getLogin());
    newUser.setPassword(userDto.getPassword());
    newUser.setAuthToken(TokenGenerator.nextToken());
    newUser.setAuthTokenCreatedDate(new Date());

    User savedUser = userRepository.save(newUser);

    userDto.setAuthToken(savedUser.getAuthToken());
    log.info("User {0} created", savedUser.getLogin());
    return userDto;
  }
Run Code Online (Sandbox Code Playgroud)

如何为该方法创建单元测试?我接下来尝试了:

  @Test
  public void createUser() {

    UserDto userDtoRequest = new UserDto();
    userDtoRequest.setLogin("Alex");
    userDtoRequest.setPassword("123");

    UserDto found = userService.create(userDtoRequest);
    assertThat(found.getAuthToken()).isNotEmpty();
} …
Run Code Online (Sandbox Code Playgroud)

java spring unit-testing mockito

8
推荐指数
3
解决办法
2万
查看次数

获取Spring存储库中的Pageable自定义查询的总计数行数

我实现这样的分页:

List<Products> products = productRepository.findAllProducts(productsRequest.getInitiatorType(), "ACTIVE",
      new PageRequest(page, 100, Sort.Direction.DESC, "insertDate"));
Run Code Online (Sandbox Code Playgroud)

但是如何获得此查询的总大小?只复制这样的查询?

  @Query("SELECT t FROM Products t WHERE t.isApproved = true AND t.partnerId = ?1 AND t.categories.status = ?2")
  List<OpcClProducts> findAllOpcClProducts(String senderId, String status, Pageable pageable);


  @Query("SELECT COUNT(t) FROM Products t WHERE t.isApproved = true AND t.partnerId = ?1 AND t.categories.status = ?2")
  long getTotalCount(String senderId, String status);
Run Code Online (Sandbox Code Playgroud)

并调用2个查询:for totalCount和数据?

java pagination spring-data-jpa spring-repositories

7
推荐指数
1
解决办法
5997
查看次数

如何使用 spring boot + .yaml 创建配置文件?

我有带有 2 个属性文件的 Spring Boot 服务器:application-local.propertiesapplication-test.properties

在每个文件中,我都有用于开发机器和测试的配置。像这样开始:

-Dspring.profiles.active=local
Run Code Online (Sandbox Code Playgroud)

但是在新的 Spring Boot 项目中,我使用了.yaml配置文件。而且我不明白如何使用profileswith .yaml. 我尝试阅读文档,但什么也不懂。你能一步一步解释该怎么做吗?

我需要两个文件?

application-local.yamlapplication-test.yaml

或者我需要在一个application.yaml文件中写入所有内容?如果在一个文件中,我如何分离配置?这是我的配置:

server:
  path: ***
  port: ***

cxf:
  path: ***

spring.datasource:
  type: com.zaxxer.hikari.HikariDataSource
  driver-class-name: oracle.jdbc.OracleDriver
  url: ***
  username: ***
  password: ***
  hikari:
    minimumIdle: 5
    maximumPoolSize: 20
    idleTimeout: 30000
    poolName: SpringBootJPAHikariCP
    maxLifetime: 2000000
    connectionTimeout: 30000
    connection-test-query: SELECT 1 FROM DUAL

spring.jpa:
  show-sql: false
  database-platform: org.hibernate.dialect.Oracle10gDialect
  properties.hibernate.jdbc.batch_size: 30
  properties.hibernate.cache.use_second_level_cache: false
  hibernate:
    ddl-auto: validate


spring.cache:
  ehcache: …
Run Code Online (Sandbox Code Playgroud)

java yaml spring-boot

7
推荐指数
2
解决办法
1万
查看次数