Friday 26 February 2010

用NetBeans6.8开发iBATIS3程序

    这真是一个艰难的探索,iBATIS也许是个不错的Framework,但是文档写的很不好,从头到尾都缺少一个完整的例子。如果习惯了微软MSDN和NetBeans.org上面的文章风格,你会觉得iBATIS的文章作者真的是不懂如何写文章。iBATIS文章中很多例子片段都不能使用,不知道为什么?
    先交代一下开发环境:UBuntu9.10x86+ NetBeans6.8 + JDK1.6 +iBATIS最新源代码Beta9。
第一步,获取最新的iBATIS源代码
最新源代码,trunk目录中的Beta9已经相当稳定了。
首先安装subversion,通过命令sudo apt-get install subversion
然后调用命令获取最新代码 svn checkout http://svn.apache.org/repos/asf/ibatis
代码获取完成后,会在当前目录下发现有一个ibatis目录

第二步,安装ant
windows下面安装方式参考我的另一文章,UBuntu下生活很轻松:sudo apt-get install ant

第三步,进入ibatis/java/ibatis-3/trunk 目录,运行ant命令
期间,会进行编译(还会自动下载一些东西),自动运行unit test,
最后结果可能会有些错误,比如我的机器上:
BUILD FAILED
/home/chenshu/work/ibatis/java/ibatis-3/trunk/build.xml:29: The following error occurred while executing this line:
/home/chenshu/work/ibatis/java/ibatis-3/trunk/build.xml:63: The following error occurred while executing this line:
/home/chenshu/work/ibatis/java/ibatis-3/trunk/common.xml:32: Unable to resolve artifact: Missing:
----------
1) org.apache.ibatis:ibatis-3-core:jar:3.0-SNAPSHOT

  Try downloading the file manually from the project website.

  Then, install it using the command:
      mvn install:install-file -DgroupId=org.apache.ibatis -DartifactId=ibatis-3-core -Dversion=3.0-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file

  Alternatively, if you host your own repository you can deploy the file there:
      mvn deploy:deploy-file -DgroupId=org.apache.ibatis -DartifactId=ibatis-3-core -Dversion=3.0-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

  Path to dependency:
      1) org.apache.ibatis:ibatis-3-compat:jar:3.0-SNAPSHOT
      2) org.apache.ibatis:ibatis-3-core:jar:3.0-SNAPSHOT

----------
1 required artifact is missing.

for artifact:
  org.apache.ibatis:ibatis-3-compat:jar:3.0-SNAPSHOT

from the specified remote repositories:
  maven2-repository.dev.java.net (http://download.java.net/maven/2/),
  central (http://repo1.maven.org/maven2)



不管它,检查一下ibatis/java/ibatis-3/trunk/build/ibatis-3-core/dist目录,看是否存在ibatis-3-core-3.0.0.229.zip文件,如果有就成功了

第四步,解压ibatis-3-core-3.0.0.229.zip文件
mkdir ibatis-3-core-3.0.0.229
unzip ./ibatis-3-core-3.0.0.229.zip -d ./ibatis-3-core-3.0.0.229

第五步,启动NetBeans6.8,创建一个新的Class Library叫iBATIS3.0Beta9,设置相关jar包文件和源代码,类似下图(图我没有随着版本更新而更新):







第六步,创建一个Java Application工程T1
加入iBATIS3.0Beta9库,因为我要连接的是MySQL数据库,所以也要加入MySQL JDBC库

第七布,配置xml文件
创建两个xml文件,一个负责数据库配置,一个负责进行SQL查询
先创建xml 包,然后创建sqlMapConfig.xml文件,内容如下:

    可以右键点击xml文件,然后选择Validate xml菜单进行验证。
现在在xml包下创建user.map.xml文件,内容如下:


    这里有一个select语句,返回的是bean包下的User对象,namespace加上id是我们后面调用代码的参数:
先看以下User类:


package bean;


public class User {
    private String personID;
    private String password;
    private String sex;


    public String getPersonID() {
        return personID;
    }


    public void setPersonID(String personID) {
        this.personID = personID;
    }


    public String getPassword() {
        return password;
    }


    public void setPassword(String password) {
        this.password = password;
    }


    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

现在在Main类里面完成调用代码:
package t1;

import bean.User;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


public class Main {

    public static void main(String[] args) throws IOException {
        String resource = "xml/sqlMapConfig.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = sqlMapper.openSession(true);

        try {
            User user = (User) session.selectOne("bin.UserMapper.getUser", "chenshu");
            System.out.println(user.getPersonID());
            System.out.println(user.getPassword());
            System.out.println(user.getSex());
        } finally {
            session.close();
        }
    }
}

运行结果:
run:
chenshu
1
male


数据库很简单,就不要介绍了,大家能猜出来吧?












Followers

Contributors