我试图在我当前的项目(用 Java 编写)中第一次使用 jooq。我想在 Jooq DSL 上下文中集成 Hikari 连接池。我想明确定义最大连接数。任何推荐的文章,我可以遵循的代码来完成它。
谢谢
我已经设置了 jooq,现在我可以为我的数据库生成代码。
public static void init() {
Target l_target = new Target();
System.out.println("My directory is::"+l_target.getPackageName());
l_target.setDirectory("src/main/java");
l_target.setPackageName("com.my.paas.css.entity");
Configuration configuration = new Configuration()
.withJdbc(new Jdbc()
.withDriver("com.mysql.jdbc.Driver")
.withUrl("jdbc:mysql://localhost:3306/paas")
.withUser("root"))
.withGenerator(new Generator()
.withDatabase(new Database()
.withName("org.jooq.meta.mysql.MySQLDatabase")
.withIncludes(".*")
.withExcludes("")
.withInputSchema("paas"))
.withTarget(l_target));
try {
GenerationTool.generate(configuration);
} catch (Exception ex) {
System.out.println();
ex.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud) 如何避免单元测试中的手动睡眠。假设在下面的代码中,Process和notify需要大约 5 秒的时间进行处理。所以为了完成处理,我添加了 5 秒的睡眠。
public class ClassToTest {
public ProcessService processService;
public NotificationService notificationService;
public ClassToTest(ProcessService pService ,NotificationService nService ) {
this.notificationService=nService;
this.processService = pService;
}
public CompletableFuture<Void> testMethod()
{
return CompletableFuture.supplyAsync(processService::process)
.thenAccept(notificationService::notify);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来处理这个问题?
@Test
public void comletableFutureThenAccept() {
CompletableFuture<Void> thenAccept =
sleep(6);
assertTrue(thenAccept.isDone());
verify(mocknotificationService, times(1)).notify(Mockito.anystring());
}
Run Code Online (Sandbox Code Playgroud) 以下工作完美:
String startDate = "2019-10-12T00:00:00.000-07:00"
LocalDateTime startDateTime = LocalDateTime.parse(startDate,
DateTimeFormatter.ISO_ZONED_DATE_TIME);
Run Code Online (Sandbox Code Playgroud)
但是,对于以下代码:
String startDate = "2019-10-12T00:00:00.000+07:00"
LocalDateTime startDateTime = LocalDateTime.parse(startDate,
DateTimeFormatter.ISO_ZONED_DATE_TIME);
Run Code Online (Sandbox Code Playgroud)
我得到一个例外:
抛出
java.time.format.DateTimeParseException:无法在索引 23 处解析文本“2019-10-12T00:00:00.000 07:00”
有人可以帮助我了解这里可能有什么问题吗?
只是想知道我们是否可以在不编写自定义比较器类的情况下使用 Java 8 根据重复数字的频率对列表进行排序。
我需要根据给定的整数的频率,然后按自然数字顺序对给定的整数进行排序。
我在Comparator.naturalOrder()处遇到错误;
这是我尝试过的代码:
Integer[] given = new Integer[]{0,0,1,22,11,22,22,11,44,555,55,66,77,88,99};
List<Integer> intList = Arrays.asList(given);
Map<Integer, Long> frequencyMap = intList.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
List<Integer> newList = intList.stream().sorted(Comparator.comparing(frequencyMap::get).thenComparing(Comparator.naturalOrder())).collect(Collectors.toList());
System.out.println(newList.toString());
Run Code Online (Sandbox Code Playgroud)
预期的输出是
[1, 44, 55, 66, 77, 88, 99, 555, 0, 0, 11, 11, 22, 22, 22]
Run Code Online (Sandbox Code Playgroud)
PS:在第一行使用数组以避免在多行中使用 list.add() 并清楚理解。
我目前正在试验 liquibase。我的更改日志文件是通过liquibase-maven-plugin基于我的休眠实体类生成的。到目前为止它有效,但它映射java.time.LocalDate到BINARY(255). 是否可以教 liquibase 使用DATE,还是需要手动完成?
我在用
private boolean hasDuplicates(Recipe recipe) {
List<Recipe> currentRecipes = new ArrayList<>();
Stream.of(this.breakfast, this.lunch, this.dinner).forEach(meal -> {
currentRecipes.add(meal.getRecipe());
currentRecipes.add(meal.getSnack());
});
currentRecipes.add(this.snack);
return currentRecipes.contains(recipe);
};
}
Run Code Online (Sandbox Code Playgroud)
// 想象一下所有字段的 getter 和 setter。
public class Menuplan {
private Meal breakfast;
private Meal lunch;
private Meal dinner;
private Recipe snack;
}
public class Meal {
private Recipe recipe;
private Reicpe snack;
}
Run Code Online (Sandbox Code Playgroud)
如果 Menuplan 已经分配了给定的食谱(作为零食或食谱),我使用上述方法进行测试。
我想知道是否有更优雅/更短的方法来编写函数。
有没有办法记录我们使用 spring jpa 内置查询(如 findById!)时生成的密码查询?我有一个复杂的内置查询,我需要查看其密码
newPropertiesFile.keySet().parallelStream()
.filter(value -> oldPropertiesFile.keySet().parallelStream()
.filter(entry -> oldPropertiesFile.get(entry).toString().equals(newPropertiesFile.get(value).toString()))
.filter(values -> !values.equals(value)).count() > 0)
.collect(Collectors.toMap(entryKey -> (String) entryKey, entryKey -> newPropertiesFile.get(entryKey).toString()));
Run Code Online (Sandbox Code Playgroud)
例如,我有mapA = {(1,'a'),(2,'b'),(3,'c')}并mapB = {(5,'a'),(6,'d'),(7,'c')}
比较了两个地图的 valueList,值'a'和'c'inmapA出现在mapB,它们的键是5和7分别。
因此我需要的 o/p:
5,7
我已经完成了上述操作并获得了所需的输出。但是复杂度在 O(n^2) 上太高了。有什么优化的方法吗?
一个更简单的例子:
mapA.keySet().parallelStream()
.filter(v->mapB.keySet().parallelStream()
.filter(e->mapB.get(v).equals(mapA.get(v)))
.filter(v->!v.equals(v)).count()>0)
.forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud) 我试图避免检查(if ... != null)或try{...}catch(NullPointerException e) {...}
让我们举个例子来理解我的问题:
我有一个class Park包含List<Car>
我有一个class Car包含一个Motor object
我有一个class Motor包含一个Name String值
我想从我的 Park 类中返回第一个字符串电机名称:
我想避免:
if (park != null) {
if (park.getCars() != null) {
for (Car car : park.getCars() {
if (car.getMotor() != null) {
return car.getMotor().getName();
Run Code Online (Sandbox Code Playgroud)
我在想这样的事情:
Optional.ofNullable(park).map(Park::getCars).ifPresent(cars -> {
return cars.stream().map(Car::getMotor).map(Motor::getName).findFirst().orElse(null);
});
Run Code Online (Sandbox Code Playgroud)
但它不编译。有任何想法吗 ??
我正在尝试在 Java 8 中学习流媒体。以下是我的代码:
主程序
public class Main {
public static void main(String[] args) {
Person person = new Person("FirstName", "LastName");
List<Person> personList = new ArrayList<>();
personList.add(person);
Place place = new Place("name", "country");
List<Place> placeList = new ArrayList<>();
placeList.add(place);
List<List<Object>> objects = new ArrayList<>();
objects.add(Collections.singletonList(personList));
objects.add(Collections.singletonList(placeList));
List<Object> persons = objects.get(0);
List<String> firstNames = persons.stream()
.map(o -> ((Person)o).getFirstName())
.collect(Collectors.toList());
firstNames.forEach(System.out::println);
}
}
Run Code Online (Sandbox Code Playgroud)
人.java
@Data
public class Person {
String firstName;
String lastName;
public Person(String firstName, String lastName) {
setFirstName(firstName);
setLastName(lastName);
} …Run Code Online (Sandbox Code Playgroud)