iBATIS----配置环境

转载请保留原文链接:http://www.yiibai.com/ibatis/ibatis_environment.html

iBATIS 安装:

这里有几个简单的步骤,需要开展Linux机器上安装iBATIS:

  • 下载iBATIS的最新版本 下载iBATIS.

  • 解压下载的文件,从包中提取.jar文件并将其保存在相应的lib目录下。

  • 在提取 .jar文件适当设置PATH和CLASSPATH变量。

下面是进行Linux机器下载iBATIS的二进制文件的步骤:

$ unzip ibatis-2.3.4.726.zip
  inflating: META-INF/MANIFEST.MF
   creating: doc/
   creating: lib/
   creating: simple_example/
   creating: simple_example/com/
   creating: simple_example/com/mydomain/
   creating: simple_example/com/mydomain/data/
   creating: simple_example/com/mydomain/domain/
   creating: src/
  inflating: doc/dev-javadoc.zip
  inflating: doc/user-javadoc.zip
  inflating: jar-dependencies.txt
  inflating: lib/ibatis-2.3.4.726.jar
  inflating: license.txt
  inflating: notice.txt
  inflating: release.txt
$pwd
/var/home/ibatis
$set PATH=$PATH:/var/home/ibatis/
$set CLASSPATH=$CLASSPATH:/var/home/ibatis\
                       /lib/ibatis-2.3.4.726.jar
 

数据库设置:

使用下面的语法在 MySQL数据库中创建EMPLOYEE表:

mysql> 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)
        );  

创建SqlMapConfig.xml

考虑以下几点:

  • 我们将使用JDBC来访问数据库 testdb.

  • MySQL的JDBC驱动程序是 "com.mysql.jdbc.Driver".

  • 连接URL是 "jdbc:mysql://localhost:3306/testdb".

  • 使用的用户名和密码是 "root" and "root".

  • SQL语句映射的所有操作将被描述在"Employee.xml".

基于上述假设,我们必须创建一个XML配置文件,nameSqlMapConfig.xml以下内容。这就是需要提供所需的iBatis的所有配置:

这两个文件SqlMapConfig.xml和Employee.xml 存在于类路径。现在,我们将保持Employee.xml文件为空,我们将格式转换的在随后的章节内容。

SqlMapConfig.xml:

<?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>
 <settingsuseStatementNamespaces="true"/>
  <transactionManagertype="JDBC">
   <dataSourcetype="SIMPLE">
    <propertyname="JDBC.Driver"value="com.mysql.jdbc.Driver"/>
    <propertyname="JDBC.ConnectionURL"value="jdbc:mysql://localhost:3306/testdb"/>
    <propertyname="JDBC.Username"value="root"/>
    <propertyname="JDBC.Password"value="root"/>
   </dataSource>
  </transactionManager>
 <sqlMapresource="Employee.xml"/>
</sqlMapConfig>  

还有其他一些可选的属性,您可以在SqlMapConfig.xml文件中设置:

<propertyname="JDBC.AutoCommit"value="true"/>
<propertyname="Pool.MaximumActiveConnections"value="10"/>
<propertyname="Pool.MaximumIdleConnections"value="5"/>
<propertyname="Pool.MaximumCheckoutTime"value="150000"/>
<propertyname="Pool.MaximumTimeToWait"value="500"/>
<propertyname="Pool.PingQuery"value="select 1 from Employee"/>
<propertyname="Pool.PingEnabled"value="false"/>
 

猜你喜欢

转载自2277259257.iteye.com/blog/2116122