小编bra*_*orm的帖子

在Spring启动应用程序中包装事务中的多个调用?

我正在使用jdbctemplate在我的spring应用程序中对db执行查询.

这是使用@Transactional注释的方法

@Transactional
public boolean doSomething(){    
    try {
        jdbcTemplate.update(sql1); //1
        jdbcTemplate.update(sql2); //2
        jdbcTemplate.update(sql3); //3
        return true; 
    } catch (Exception ex){
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果1和2成功而3失败,1和2上的交易是否会回滚?我该怎么测试呢?

另外有一个布尔值作为返回值是一个表明交易状态的好习惯吗?

spring spring-transactions spring-boot

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

如何检查一个列表/字典的元素是否存在于python中的另一个列表/字典中

这是一个场景,我正在检查A中的元素是否存在于B中,而这段代码可以工作,当我阅读数百万行时需要花费大量时间.有效的方法是将A和B中的每个列表作为字典,并查看它们是否存在于彼此中.但我无法想到一种简单的字典查找方式.这是针对dict A中的每个键值对,我想检查dictB中是否存在该键值对

A = [['A',[1,2,3]],['D',[3,4]],['E',[6,7]]]

B=  [['A',[1,2,3]],['E',[6,7]],['F',[8,9]]]

count = 0

for line in A:

  if len(line[1]) > 1:

     if line in B:

       count = count + 1

print count
Run Code Online (Sandbox Code Playgroud)

python dictionary list

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

在html中的ol里面不能有ul

我测试HTML时出现以下验证错误.

请查看HTML代码的附加图像. 在此输入图像描述 " 在这种情况下ul,元素不允许作为元素的子元素ol."

html css list html-lists

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

无法从书中了解这些例子javascript,好的部分

我正在阅读javascript:Douglas Crockford的优点.我很难理解一个特定的例子和作者提供的解释.

例1 :(第38页)

var quo=function (status) {
    return {
        get_status:function() {
           return status;
       }
    };
};

var myQuo=quo("amazed");
document.writeln(myQuo.get_status());
Run Code Online (Sandbox Code Playgroud)

书中的解释(我不明白这一点.请解释这一部分):

这个quo函数被设计为在没有前缀的情况下使用,因此名称不是大写的.当我们调用quo时,它返回一个包含get_status方法的新对象.对该对象的引用存储在myQuo中.即使quo已经返回,get_status方法仍然具有对quo的status属性的特权访问权限.get_status无权访问参数的副本.它可以访问参数本身.这是可能的,因为该函数可以访问创建它的上下文.这叫做封闭.

例2(第39页):

//make a function that assigns event handler function to array of nodes
//when you click on a node, an alert box will display the ordinal of the node

var add_the_handlers=function (nodes) {
    var helper=function(i) {
        return function(e) {
            alert(i);
        }
    };
    var i;
    for (i=0;i<nodes.length;i++) {
        nodes[i].onclick=helper(i);
    }
};
Run Code Online (Sandbox Code Playgroud)

我很难理解这段代码是如何工作的,而且函数(e)是做什么的?为什么辅助函数返回函数反过来又不返回任何东西.这让我非常困惑.如果有人能用简单的语言解释,那将会非常有帮助.非常感谢

EDIT:
According to …
Run Code Online (Sandbox Code Playgroud)

javascript function object

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

如何动态分组每个n-div并用JQuery中的新div包装?

我想选择每4个div class "contents"并将其包装在另一个动态创建的新div中Jquery.我的HTML如下;

<body>
<div id="postings">
    <div class="contents"></div>
    <div class="contents"></div>
    <div class="contents"></div>
    <div class="contents"></div>
    <div class="contents"></div>
    <div class="contents"></div>
    <div class="contents"></div>
    <div class="contents"></div>
    <div class="contents"></div>
    <div class="contents"></div>
    <div class="contents"></div>
    <div class="contents"></div>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)

我想要

<body>
<div id="postings">
<div id="newDivforWraping1">
    <div class="contents"></div>
     <div class="contents"></div>
    <div class="contents"></div>
    <div class="contents"></div>
</div>
<div id="newDivforWraping2">
    <div class="contents"></div>
    <div class="contents"></div>
    <div class="contents"></div>
    <div class="contents"></div>
</div>
<div id="newDivforWraping3">
    <div class="contents"></div>
    <div class="contents"></div>
    <div class="contents"></div>
    <div class="contents"></div>
</div>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)

我目前的JScript代码如下;

(function() {
    "use strict";
    $(document).ready(function() …
Run Code Online (Sandbox Code Playgroud)

html javascript jquery

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

在Java中以通用方式实现comprable方法以进行排序

我正在尝试实现一个通用选择排序,它可以接受任何对象并对其进行排序.我可以向编译器承诺,无论我比较什么对象,都要为它实现compareTo方法.但是我得到以下代码的编译错误

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class SelectionSortGenerics implements Comparable<E> {

    private <E> void swap(E[] a, int i, int j) {
        if (i != j) {
            E temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }

    public <E> void selectionSort(E[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            // find index of smallest element
            int smallest = i;
            for (int j = i + 1; j < a.length; j++) { …
Run Code Online (Sandbox Code Playgroud)

java sorting generics compareto comparable

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

在Java中将比较器传递给collections.sort()?

我有以下代码,我有一个Treeset,如果我通过我的比较器,它工作正常.但是,如果我构造我的Treeset然后调用collections.sort,我会得到编译错误.我的代码在这里

import java.util.*;

public class ComparatorExample {
private static class SbufferComparator implements Comparator<StringBuffer> {

        @Override
        public int compare(StringBuffer s1, StringBuffer s2) {
            return s1.toString().compareTo(s2.toString());

        }

}


    public static void main(String[] args) {
            StringBuffer one = new StringBuffer("one");
            StringBuffer  two = new StringBuffer("two");
            StringBuffer three = new StringBuffer("three");
            Set<StringBuffer> sb=new TreeSet<StringBuffer>();
             //The below line works
            //Set<StringBuffer> sb=new TreeSet<StringBuffer>(new SbufferComparator());
            sb.add(one);
            sb.add(two);
            sb.add(three);
            System.out.println("set before change: "+ sb);
            //This does not work
            Collections.sort(sb, new SbufferComparator());
            System.out.println("set After change: "+ sb);
        }
    } …
Run Code Online (Sandbox Code Playgroud)

java collections comparator treeset

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

在Java中的这个例子中无法理解通配符泛型?

在阅读这篇文章时,我被困在这里.我从链接中粘贴了这个.我不明白为什么List<Number>List<? extends Number>不能在这里使用的推理.

public void doStuff( List<Integer> list ) {     
    list.add(1);
    // do stuff
    list.get(0);    
}

We can generalize this one step further by generalizing the generic parameter:

public void doStuff( List<? super Integer> list ) {     
    list.add(1);
    // do stuff
    list.get(0);    
}
Run Code Online (Sandbox Code Playgroud)

有些读者可能会问为什么更直观的List <Number>不能在这里使用.实际上,我们可以尝试将方法定义为采用List <Number>或List <?扩展Number>,但是第一个定义将排除传入实际ArrayList <Integer>的可能性,而第二个定义将禁止add()方法(因为有人可能会传入ArrayList <Float>,并且会找到调用doStuff后,在Floats之间的整数).

java generics arraylist

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

如何检查python中的iterable实例?

考虑这个例子?

p = [1,2,3,4], (1,2,3), set([1,2,3])]
Run Code Online (Sandbox Code Playgroud)

而不是检查每种类型

for x in p:
   if isinstance(x, list):
      xxxxx
   elif isinstance(x, tuple):
      xxxxxx
   elif isinstance(x, set):
      xxxxxxx
Run Code Online (Sandbox Code Playgroud)

以下是否有一些等价物:

for element in something:
  if isinstance(x, iterable):
      do something
Run Code Online (Sandbox Code Playgroud)

python iterable isinstance

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

Dijkstra 算法是否会跟踪访问过的节点以找到最短路径?

我发现此代码用于Dijkstra algorithm在加权图中找到两个节点之间的最短路径。我看到的是代码没有跟踪访问过的节点。但是它适用于我尝试过的所有输入。我添加了一行代码来跟踪访问过的节点。它仍然可以正常工作。我已经在这段代码中注释掉了。那么是否需要访问节点?有没有影响O

import java.util.PriorityQueue;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

class Vertex implements Comparable<Vertex>
{
    public final String name;
    public Edge[] adjacencies;
    public double minDistance = Double.POSITIVE_INFINITY;
    public Vertex previous;
    public Vertex(String argName) { name = argName; }
    public String toString() { return name; }
    public int compareTo(Vertex other)
    {
        return Double.compare(minDistance, other.minDistance);
    }
}

class Edge
{
    public final Vertex target;
    public final double weight;
    public Edge(Vertex argTarget, double argWeight)
    { target = argTarget; …
Run Code Online (Sandbox Code Playgroud)

java graph dijkstra breadth-first-search visited

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