小编Nic*_*ckJ的帖子

Postgres查询执行时间

MySQL命令行界面中,当您执行查询时,它将告诉您在打印结果后执行查询所需的时间.

在Postgres命令行界面(psql)中它没有告诉你.我知道如何配置日志记录,以便我可以从日志中获取信息,但将它打印到标准输出会更方便MySQL.

可以这样做吗?

postgresql

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

Log4j配置 - 不同的日志到不同的文件

对于一些人来说,这可能是一个非常简单的问题,但我个人认为Log4j配置非常困难,并且学习进行脑部手术可能不那么具有挑战性.

我正在尝试将多个记录器记录到不同的文件中.这是我在log4j.properties文件中的内容:

# Root logger option
log4j.rootLogger=INFO, file, admin

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/home/nick/logging/file.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n

log4j.appender.admin=org.apache.log4j.RollingFileAppender
log4j.appender.admin.File=/home/nick/logging/admin.log
log4j.appender.admin.MaxFileSize=1MB
log4j.appender.admin.MaxBackupIndex=1
log4j.appender.admin.layout=org.apache.log4j.PatternLayout
log4j.appender.admin.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n
Run Code Online (Sandbox Code Playgroud)

这是我用来测试配置的(非常简单的)Java应用程序:

public static void main(String[] args) throws Exception {

    Properties resource = new Properties();
    InputStream in = new FileInputStream("/home/nick/logging/log4j.properties");
    resource.load(in);
    PropertyConfigurator.configure(resource);

    Logger admin = Logger.getLogger("admin");
    Logger file = Logger.getLogger("file");

    admin.info("hello admin");
    file.info("hello file");
}
Run Code Online (Sandbox Code Playgroud)

我有两个问题:

一个问题我总是在行中出现异常PropertyConfigurator.configure(resource);:

java.io.FileNotFoundException: /home/nick/logging (Is …
Run Code Online (Sandbox Code Playgroud)

java logging log4j

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

带排序的Spring JPA规范

我正在使用Spring JPA.

是我使用的延伸的信息库更精确的JpaRepositoryJpaSpecificationExecutor因为我需要分页,过滤和排序.

现在我的分页和过滤都工作得很好,但我也无法进行排序.

我有些失望地注意到JpaSpecificationExecutorfindAll()方法:

findAll(Specification, Pageable);
findAll(Specification, Sort);
Run Code Online (Sandbox Code Playgroud)

但我需要的那个:

findAll(Specification, Pageable, Sort); //or something like this
Run Code Online (Sandbox Code Playgroud)

不存在!

因此,计划B包括规范中的排序.

在此问题的接受答案的帮助下:规范中的JpaSpecificationExecutor JOIN + ORDER BY我将以下内容放在一起:

private Specification<MainEntity> matches() {
    return new Specification<MainEntity>() {

        public Predicate toPredicate(Root<MainEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

            List<Predicate> predicates = new ArrayList<Predicate>();

            //Attempt to sort by Surname, has no effect
            query.orderBy(cb.asc(root.get("surname")));

            //add string filters
            for (String field : stringFilterMap.keySet()) {
                String valuePattern = stringFilterMap.get(field);
                predicates.add(cb.like(root.get(field), "%"+valuePattern+"%")); …
Run Code Online (Sandbox Code Playgroud)

java sorting spring spring-data-jpa jpa-criteria

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

HSQLDB中的布尔列,具有默认值

我无法让HSQLDB创建一个带有布尔列的表.似乎每次我尝试指定默认值时,我都会得到异常:

org.hsqldb.HsqlException: unexpected token: DEFAULT
Run Code Online (Sandbox Code Playgroud)

即使使用这个简单的表定义,我也可以创建这个问题:

CREATE TABLE foo (
  bar BOOLEAN NOT NULL DEFAULT FALSE
);
Run Code Online (Sandbox Code Playgroud)

根据文档,我应该能够做到这一点!

请参阅http://www.hsqldb.org/doc/guide/ch09.html#create_table-section中的 columnDefinition

我在这里误解了什么吗?

java hsqldb

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

Java 8函数 - compose和andThen

我发现我的Java知识已经过时了Java 8,并且我正在尝试学习许多新的语言功能.其中之一是函数,特别是composeandThen方法.

我已经写了一个简单的实验来验证的可逆性composeandThen:

/** Wraps a value 
 */
public class Wrap<T> {
    private final T value;
    private Wrap(T value) {
        this.value = value;
    }
    public static <T> Wrap<T> of(T value) {
        return new Wrap<>(value);
    }
}

static void functions() {

    Function<Integer,String> itos = i->"'"+i+"'";
    Function<String,Wrap<String>> stow = s->Wrap.of(s);

    Function<Integer,Wrap<String>> itow = itos.andThen(stow);
    Function<Integer,Wrap<String>> itow2 = stow.compose(itos);

    System.out.println(itow.apply(3));
    System.out.println(itow2.apply(3));
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,正如所料,2个功能itowitow2似乎是等效的.但它们实际上是等同的吗?在这种情况下,是否偶然会有相同的结果?

出现这样的想法:两种方法composeandThen方法都存在,并且函数或BiFunctions可能并不总是以这种方式可逆.你能想到任何可逆性不适用的情况吗?

java functional-programming function

7
推荐指数
4
解决办法
7183
查看次数

Spring Autowire属性对象

我已成功为除了java.util.Properties实例之外的所有内容配置了Spring自动装配.

当我使用注释自动装配其他所有内容时:

@Autowired private SomeObject someObject;
Run Code Online (Sandbox Code Playgroud)

它工作得很好.

但是,当我尝试这个:

@Autowired private Properties messages;
Run Code Online (Sandbox Code Playgroud)

使用此配置:

<bean id="mybean" class="com.foo.MyBean" >
  <property name="messages">
    <util:properties location="classpath:messages.properties"/>
  </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

我收到错误(仅限相关行):

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mybean' defined in class path resource [application.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'messages' of bean class [com.foo.MyBean]: Bean property 'messages' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? …
Run Code Online (Sandbox Code Playgroud)

java spring autowired

4
推荐指数
1
解决办法
5978
查看次数

Java 8+映射列表映射

我有这样定义的列表列表:

Map<Date,List<TimesheetContribution>> groupedByDate;
Run Code Online (Sandbox Code Playgroud)

TimesheetContribution类具有getHours()方法,该方法返回double。

我想要的是:

Map<Date, Double> hoursMap = groupedByDate.entrySet().stream()...
Run Code Online (Sandbox Code Playgroud)

映射值是TimesheetContribution实例的总时数。

我能想到的唯一方法是这样的:

Map<Date, Double> toilAmounts = groupedByDate.entrySet().stream()
    .collect(Collectors.toMap(Function.identity(), value -> ???));
Run Code Online (Sandbox Code Playgroud)

如您所见,尝试定义值映射器时遇到麻烦,并且我需要一个嵌套流,对此感到不舒服。

有什么建议么?还是我必须用老式的方式来做?

java java-8 java-stream

4
推荐指数
1
解决办法
74
查看次数

JDBC - 选择列为NULL的位置

我在Postgres 9.0数据库中有一个简单的表:

create table test (id int not null, value int);
Run Code Online (Sandbox Code Playgroud)

我用几行填充它:

insert into test values (1, 1);
insert into test values (2, null);
insert into test values (3, null);
insert into test values (4, 1);
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试用JDBC阅读它.当我通过列中的非空值选择时value,一切都很好:

PreparedStatement select = c.prepareStatement("select * from test where value=?");
select.setInt(1, 1);
return select.executeQuery();
Run Code Online (Sandbox Code Playgroud)

但是当我想选择其中value为null的行时,结果集不包含任何行.我尝试了这两种方法:

select.setObject(1, null);
Run Code Online (Sandbox Code Playgroud)

select.setNull(1, Types.INTEGER);
Run Code Online (Sandbox Code Playgroud)

都没有工作!

这是怎么回事?我知道检查NULL的正确SQL where value is null代替,where value=null但肯定JDBC很聪明,可以为我排序吗?

java sql postgresql jdbc

3
推荐指数
1
解决办法
5908
查看次数

PHP评论标签

在Java/JSP的精彩世界中,您可以使用以下形式的注释:

<%-- anything in here is ignored and does not get sent to the client 
     it can span multiple lines and is useful for commenting out blocks
     of JSP, including tags and HTML:
     <c:if test="${some.condition}">
       <p>All this is inside the comment</p>
     </c:if>
     <!-- this HTML comment is itself commented out of the JSP so will not be sent to the client --!>
--%>
<!-- but this is just an HTML comment and WILL be sent to the client -->
Run Code Online (Sandbox Code Playgroud)

在一个不太精彩的PHP世界中,我能找到的唯一的评论参考是: …

php tags comments

3
推荐指数
1
解决办法
900
查看次数

MyBatis注解,从bean更新参数

是否可以进行 MyBatis 更新,您可以提供一个包含要更新的值的 bean,而不是提供单独的参数?

澄清:

而不是这个:

@Update("update widget set name=#{name}, manufacturer=#{manufacturer} where id=#{id}")
public void updateWidget(
         @Param("id") int id, 
         @Param("name") String name, 
         @Param("manufacturer") String manufacturer);
Run Code Online (Sandbox Code Playgroud)

你能做这样的事情吗:

@Update("update widget set name=#{name}, manufacturer=#{manufacturer} where id=#{id}")
public void updateWidget(@Param("id") int id, Widget newValues);
Run Code Online (Sandbox Code Playgroud)

其中 newValues 已包含要更新的新值?

我已经尝试过了,但我得到了例外:

Exception in thread "main" org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'forename' not found. Available parameters are [id, 1, param1, param2]
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:368)
at com.sun.proxy.$Proxy8.update(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:254)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:49)
Run Code Online (Sandbox Code Playgroud)

java annotations mybatis

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