如何在IN Clause中传递参数,在jdbcTemplate中传递另一个参数

时间:2017-12-02 02:12:55

标签: sql spring jdbctemplate

我有一个查询,它接受一个I​​N子句参数和一个简单的where子句参数,如:

import com.sun.glass.events.KeyEvent;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public interface Engine {
        public void stop();
        public void start();
    }

    public class TestPane extends JPanel implements Engine {

        private Timer timer;
        private JLabel label = new JLabel("Waiting");

        public TestPane() {
            setLayout(new GridBagLayout());
            add(label);
            timer = new Timer(1000, new ActionListener() {
                private int counter = 0;
                @Override
                public void actionPerformed(ActionEvent e) {
                    counter++;
                    label.setText(Integer.toString(counter));
                }
            });
            InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap actionMap = getActionMap();

            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "stop");
            actionMap.put("stop", new StopAction(this));

            start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        public void stop() {
            timer.stop();
            label.setText("Stopped");
        }

        @Override
        public void start() {
            timer.start();
        }

    }

    public class StopAction extends AbstractAction {

        private Engine engine;

        public StopAction(Engine engine) {
            this.engine = engine;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            engine.stop();
        }

    }

}

我试图使用SELECT count(*) FROM employee WHERE empId in (?) and empName = ? ,但它只接受NamedParameterJdbcTemplate中的IN子句参数。

MapSqlParameterSource

以下是我的代码段。

List<String> empList = namedJdbcTemplate.queryForList(query, parameters,String.class);

如何传递另一个参数,即String query="Select count(*) from employee where empId in (?) and empName =?"; List<String> list = new ArrayList<String>(Arrays.asList(appType.split(" , "))); MapSqlParameterSource parameters = new MapSqlParameterSource(); parameters.addValue("listOfId", list); List<String> empList = namedJdbcTemplate.queryForList(query, parameters,String.class);

1 个答案:

答案 0 :(得分:0)

您需要为参数命名:

SELECT count(*) FROM employee WHERE empId in (:listOfId) and empName = :empName

然后将其添加到parameters

parameters.addValue("listOfId", list);
parameters.addValue("empName", empName);