Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package am.client; import java.io.Reader; import java.util.HashMap; import java.util.List; import java.util.Map; import mybatis.vo.DeptVO; import mybatis.vo.EmpVO; import mybatis.vo.JobVO; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; /** * * @author USER */ public class MyFrame extends javax.swing.JFrame { private SqlSessionFactory factory; /** * Creates new form MyFrame */ public MyFrame() { initComponents(); init(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jButton1 = new javax.swing.JButton(); v_tf = new javax.swing.JTextField(); jComboBox1 = new javax.swing.JComboBox<>(); jScrollPane1 = new javax.swing.JScrollPane(); ta = new javax.swing.JTextArea(); jButton2 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText(" "); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jComboBox1.setModel( new javax.swing.DefaultComboBoxModel<>(new String[] { "", "?", "?", "" })); ta.setColumns(20); ta.setRows(5); jScrollPane1.setViewportView(ta); jButton2.setText(""); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 116, Short.MAX_VALUE) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(v_tf, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) .addComponent(jScrollPane1)); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] { jButton1, jButton2 }); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(v_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton2)).addGap(8, 8, 8) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE))); layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] { jButton1, jButton2, jComboBox1, v_tf }); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // ? ?? search(); }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed // ? ?? .. SqlSession ss = factory.openSession(); List<EmpVO> list = ss.selectList("emp_rel.all"); // viewData(list); }//GEN-LAST:event_jButton2ActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MyFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JComboBox<String> jComboBox1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea ta; private javax.swing.JTextField v_tf; // End of variables declaration//GEN-END:variables private void search() { // ? . String str = v_tf.getText().trim(); if (str.length() > 0) { // ??? ?? ?? (0, 1, 2,...) int idx = jComboBox1.getSelectedIndex(); SqlSession ss = factory.openSession(); //mybatis ? Map //. Map<String, String> map = new HashMap<>(); map.put("searchType", String.valueOf(idx)); map.put("searchValue", str); List<EmpVO> list = ss.selectList("emp_rel.search", map); viewData(list); } } private void init() { try { Reader r = Resources.getResourceAsReader("mybatis/config/config.xml"); factory = new SqlSessionFactoryBuilder().build(r); r.close(); setTitle("!"); } catch (Exception e) { e.printStackTrace();// //? ? . } } private void viewData(List<EmpVO> list) { // // JTextArea? ? ?? ? ?! StringBuffer sb = new StringBuffer( " \t ? \t ? \t ? \t \t \t \n --------------------------------------------------------------------------------------------------------------------"); for (EmpVO vo : list) { //vo? ??? . String empno = vo.getEmployee_id(); String name = vo.getFirst_name(); JobVO jvo = vo.getJvo(); String job_id = jvo.getJob_id(); String job_name = jvo.getJob_title(); DeptVO dvo = vo.getDvo(); String deptno = dvo.getDepartment_id(); String d_name = dvo.getDepartment_name(); String city = dvo.getLvo().getCity(); // ? StringBuffer? ?(append) sb.append("\n"); sb.append(empno); sb.append("\t"); sb.append(name); sb.append("\t"); sb.append(job_id); sb.append("\t"); sb.append(job_name); sb.append("\t"); sb.append(deptno); sb.append("\t"); sb.append(d_name); sb.append("\t"); sb.append(city); } // StringBuffer? ? ? ta? ?! ta.setText(sb.toString()); } }