2014-07-07 26 views
0

我是如此新的ibatis。我已閱讀了一些功能並嘗試工作,但這並不適合我。可能是我正在面對xml文件的放置問題。這裏是我的工作流程>>>我在NetBeans中採用了一個項目,並使用Mysql JDBC Driver將ibatis-2.3.4.726.jar保留在Libraries包下。然後我創建了Employee.xml和SqlMapConfig.xml文件,並將它們保存在Web Pages/WEB-INF包中。然後我創建了Employee.java和IbatisInsert.java文件,並將它們保存在源碼包/ default-package文件夾下。現在,當我運行IbatisInsert.java文件,它會顯示以下信息>>>線程「main」 java.io.IOException的如何使用iBatis

異常:找不到在com.ibatis.common.resources資源SqlMapConfig.xml .Resources.getResourceAsStream(Resources.java:110) at com.ibatis.common.resources.Resources.getResourceAsStream(Resources.java:95) at com.ibatis.common.resources.Resources.getResourceAsReader(Resources.java:161 ) at IbatisInsert.main(IbatisInsert.java:20)

我不理解在哪裏保存文件以及如何配置gure ibatis。我搜索谷歌,但不理解。任何人都可以請幫我這個請?? ??這裏是我的xml文件和java文件初級講座:::

employee.xml >>>

<?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> 

SqlMapConfig.xml >>>

<!--<?xml version="1.0" encoding="UTF-8"?> 
<Context antiJARLocking="true" path="/ibatisExample"/>--> 

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

<sqlMapConfig> 
    <settings useStatementNamespaces="true"/> 
    <transactionManager type="JDBC"> 
     <dataSource type="SIMPLE"> 
      <property name="JDBC.Driver" 
       value="com.mysql.jdbc.Driver"/> 
      <property name="JDBC.ConnectionURL" 
       value="jdbc:mysql://localhost:3306/ibatisexample"/> 
      <property name="JDBC.Username" value="root"/> 
      <property name="JDBC.Password" value="123"/> 
     </dataSource> 
     </transactionManager> 
    <sqlMap resource="Employee.xml"/> 
</sqlMapConfig> 

<property name="JDBC.AutoCommit" value="true"/> 

<property name="Pool.MaximumActiveConnections" value="10"/> 

<property name="Pool.MaximumIdleConnections" value="5"/> 

<property name="Pool.MaximumCheckoutTime" value="150000"/> 

<property name="Pool.MaximumTimeToWait" value="500"/> 

<property name="Pool.PingQuery" value="select 1 from Employee"/> 

<property name="Pool.PingEnabled" value="false"/> 

employee.java >>>

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* @author sumon.bappi 
*/ 
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 */ 

IbatisInsert.java >>>

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* @author sumon.bappi 
*/ 
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 "); 

    } 
} 

我的文件配置爲像下面的圖片>>>

ibatis_file_placement

回答

1

爲了讓閱讀SqlMapConfig.xml文件,只需把它放在配置目錄下的旁文件夾放在默認包路徑中,並將該目錄添加到您的類路徑中。

我注意到你在SqlMapConfig.xml丟失的另一件事是映射器 ..這個配置文件的主要作用是將XML資源的位置以及定義數據源參數。

所以,你可能需要在你的文件的末尾添加如下內容:

<mappers> 
     <mapper resource="PATH_TO_YOUR_DIR/Employee.xml /> 
</mappers> 
+0

感謝您的嘗試,但不工作 –

+0

它應該工作,確保將您的文件夾添加到類路徑,以便它在運行時讀取它..你能告訴我你的文件的新位置? –

+0

我附上了一張照片供你參考。請注意它。 –

0

放置在同一個src文件夾中的所有XML文件,其中的Java文件是有.. 它將工作