我们在lib目录下,找到“ibatis-2.3.4.726.jar”文件,加入到我们项目的lib目录下,就行。在这里,我们先说下怎么学习这个iBatis框架:上图中,有个simple_example的文件夹,它里面就包含了一个超级简单且容易理解的例子,大家可以去学习一下。By the way,如果你学过Hibernate的话,你会发觉iBatis要比Hibernate好学很多。关于Hibernate和iBatis的争论,网上有很多,大家有兴趣可以去了解一下。
<?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="Article"> <!-- Use type aliases to avoid typing the full classname every time. --> <typeAlias alias="Article" type="cn.simple.pojo.Article"/> <!-- Result maps describe the mapping between the columns returned from a query, and the class properties. A result map isn't necessary if the columns (or aliases) match to the properties exactly. --> <resultMap id="ArticleResult" class="Article"> <result property="id" column="ID"/> <result property="title" column="TITLE"/> <result property="author" column="AUTHOR"/> <result property="content" column="CONTENT"/> <result property="pubtime" column="PUBTIME"/> </resultMap> <!-- Select with no parameters using the result map for Account class. --> <select id="selectAllArticles" resultMap="ArticleResult"> select * from article </select> <!-- A simpler select example without the result map. Note the aliases to match the properties of the target result class. --> <select id="selectArticleById" parameterClass="int" resultClass="Article"> select ID as id, TITLE as title, AUTHOR as author, CONTENT as content, PUBTIME as pubtime from Article where ID=#id# </select> <!-- Insert example, using the Account parameter class --> <insert id="insertArticle" parameterClass="Article"> insert into article ( TITLE, AUTHOR, CONTENT, PUBTIME ) values ( #title#, #author#, #content#, #pubtime# ) </insert> <!-- Update example, using the Account parameter class --> <update id="updateArticle" parameterClass="Article"> update article set TITLE = #title#, AUTHOR = #author#, CONTENT = #content#, PUBTIME = #pubtime# where ID = #id# </update> <!-- Delete example, using an integer as the parameter class --> <delete id="deleteArticleById" parameterClass="int"> delete from article where ID = #id# </delete> </sqlMap>
<?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> <!-- Configure a built-in transaction manager. If you're using an app server, you probably want to use its transaction manager and a managed datasource --> <transactionManager type="JDBC" commitRequired="false"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/> <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/simpledb"/> <property name="JDBC.Username" value="root"/> <property name="JDBC.Password" value="root"/> </dataSource> </transactionManager> <!-- List the SQL Map XML files. They can be loaded from the classpath, as they are here (com.domain.data) --> <sqlMap resource="cn/simple/pojo/Article.xml"/> <!-- List more here <sqlMap resource="com/mydomain/data/Order.xml"/> <sqlMap resource="com/mydomain/data/Documents.xml"/> --> </sqlMapConfig>
一看这代码,也有点复杂,我的说法同上,大不了COPY,再略作修改,呵呵
好了,来写我们的业务逻辑层:
package cn.simple.manager; import java.io.IOException; import java.io.Reader; import java.sql.SQLException; import java.util.List; import cn.simple.pojo.Article; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; publicclass ArticleManager { /** *//** * SqlMapClient instances are thread safe, so you only need one. In this * case, we'll use a static singleton. So sue me. ;-) */ privatestatic SqlMapClient sqlMapper; /** *//** * It's not a good idea to put code that can fail in a class initializer, * but for sake of argument, here's how you configure an SQL Map. */ static{ try{ Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml"); sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); }catch (IOException e) { // Fail fast. thrownew RuntimeException( "Something bad happened while building the SqlMapClient instance." + e, e); } } /** *//** * 查询列表 * @return * @throws SQLException */ publicstatic List<Article> selectAllArticles() throws SQLException { return sqlMapper.queryForList("selectAllArticles"); } /** *//** * 插入数据 * @param article * @throws SQLException */ publicstaticvoid insertArticle(Article article) throws SQLException { sqlMapper.insert("insertArticle", article); } /** *//** * 更新数据 * @param article * @throws SQLException */ publicstaticvoid updateArticle(Article article) throws SQLException { sqlMapper.update("updateArticle", article); }