国产chinesehdxxxx野外,国产av无码专区亚洲av琪琪,播放男人添女人下边视频,成人国产精品一区二区免费看,chinese丰满人妻videos

iBATIS的創(chuàng)建操作

2018-12-09 11:01 更新

要執(zhí)行使用iBATIS的創(chuàng)建,讀取,更新和刪除(CRUD)操作,你需要創(chuàng)建一個普通Java對象(PO??JO)對應的表類。本課程介紹對象,這將“模型”的數(shù)據(jù)庫表行。

POJO類將不得不實施所有執(zhí)行所需操作所需要的方法。

讓我們假設我們有在MySQL中下EMPLOYEE表 -

CREATE TABLE EMPLOYEE (
   id INT NOT NULL auto_increment,
   first_name VARCHAR(20) default NULL,
   last_name  VARCHAR(20) default NULL,
   salary     INT  default NULL,
   PRIMARY KEY (id)
);

員工PO??JO類

我們將在Employee.java文件中創(chuàng)建一個Employee類如下 -

public class Employee {
   private int id;
   private String first_name; 
   private String last_name;   
   private int salary;  

   /* Define constructors for the Employee class. */
   public Employee() {}
  
   public Employee(String fname, String lname, int salary) {
      this.first_name = fname;
      this.last_name = lname;
      this.salary = salary;
   }
} /* End of Employee */

可以定義方法來設置表中的各個字段。下一章將介紹如何獲取各個字段的值。

Employee.xml文件

要定義使用iBATIS SQL映射語句中,我們將使用<插入>標記和這個標簽定義中,我們可以定義將在IbatisInsert.java文件中,用于對數(shù)據(jù)庫執(zhí)行SQL INSERT查詢的“身份證”。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="Employee"> 

   <insert id="insert" parameterClass="Employee">
      insert into EMPLOYEE(first_name, last_name, salary)
      values (#first_name#, #last_name#, #salary#)

      <selectKey resultClass="int" keyProperty="id">
         select last_insert_id() as id
      </selectKey>
   </insert> 

</sqlMap>

這里parameterClass -可能需要一個值作為字符串,整數(shù),浮點,雙 ,或根據(jù)需求任意類對象 。在這個例子中,我們將通過Employee對象作為參數(shù),同時呼吁的SqlMap類的插入方法。

如果數(shù)據(jù)庫表使用IDENTITY,AUTO_INCREMENT或串行列或您已經(jīng)定義了一個SEQUENCE /發(fā)電機,可以使用<selectKey元素>元素在<插入>語句中使用或返回數(shù)據(jù)庫生成的值。

IbatisInsert.java文件

該文件將應用程序級別的邏輯插入在雇員表中的記錄 -

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import java.io.*;
import java.sql.SQLException;
import java.util.*;

public class IbatisInsert{
   public static void main(String[] args)throws IOException,SQLException{
      Reader rd = Resources.getResourceAsReader("SqlMapConfig.xml");
      SqlMapClient smc = SqlMapClientBuilder.buildSqlMapClient(rd);

      /* This would insert one record in Employee table. */
      System.out.println("Going to insert record.....");
      Employee em = new Employee("Zara", "Ali", 5000);

      smc.insert("Employee.insert", em);

      System.out.println("Record Inserted Successfully ");
   }
} 

編譯和運行

以下是編譯并運行上述軟件的步驟。請確保您已設置PATH和CLASSPATH在進行適當?shù)木幾g和執(zhí)行之前。

  • 創(chuàng)建Employee.xml如上所示。
  • 創(chuàng)建Employee.java如上圖所示,并對其進行編譯。
  • 創(chuàng)建IbatisInsert.java如上圖所示,并對其進行編譯。
  • 執(zhí)行IbatisInsert二進制運行程序。

你會得到下面的結果,并創(chuàng)下會在EMPLOYEE表中創(chuàng)建。

$java IbatisInsert
Going to insert record.....
Record Inserted Successfully

如果檢查EMPLOYEE表,它應該顯示以下結果 -

mysql> select * from EMPLOYEE;
+----+------------+-----------+--------+
| id | first_name | last_name | salary |
+----+------------+-----------+--------+
|  1 | Zara       | Ali       |   5000 |
+----+------------+-----------+--------+
1 row in set (0.00 sec)

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號