gpt4 book ai didi

java - 如何使用 iBatis

转载 作者:行者123 更新时间:2023-11-29 12:51:49 26 4
gpt4 key购买 nike

我对 ibatis 很陌生。我已经阅读了它的一些功能并尝试工作,但它对我来说没有发生。可能我在这里面临 xml 文件的放置问题。这是我的工作流程>>>我在netbeans中做了一个项目,并将ibatis-2.3.4.726.jar保存在带有Mysql JDBC驱动程序的Libraries包下。然后我创建了 Employee.xml 和 SqlMapConfig.xml 文件并将它们保存在 Web Pages/WEB-INF 包下。然后我创建了 Employee.java 和 IbatisInsert.java 文件并将它们保存在 source packages/default-package 文件夹下。现在,当我运行 IbatisInsert.java 文件时,它显示以下消息 >>>

Exception in thread "main" java.io.IOException: Could not find resource SqlMapConfig.xml at com.ibatis.common.resources.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)

我不明白在哪里保存文件以及如何配置 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

最佳答案

为了允许读取SqlMapConfig.xml文件,只需将其放在默认包路径中source文件夹旁边的config目录下即可并将目录添加到您的类路径中。

我注意到您在SqlMapConfig.xml中缺少的另一件事是映射器..此配置文件的主要作用是映射xml资源的位置以及定义数据源参数。

因此您可能需要在文件末尾添加如下内容:

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

关于java - 如何使用 iBatis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24604286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com