Ibatis使用知识总结

1.  从官方网站下载,ibatis的jar及相关的文件,http://ibatis.apache.org/
2 . 新建一个工程, 我们如果要进行ibatis相关的操作, 就一个要导入ibatis-2.3.3.720.jar
3 . 建立pojo与pojo.xml形成映射.
4.  一般一个pojo对应一个pojo.xml, 例如.User.java.与User.xml
pojo的类最好实现java.io.Serializable接口, 以备应用时, 进一步的扩展, 还要提供一个缺省的构造方法(空构造方法)。
5. ibatis的配置文件.SqlMapConfig.xml, 这个文件一般放到src下.
<?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 --
        <properties resource="dbconfig.properties"/>/*数据库的驱动配置文件,一般与SqlMapConfig.xml是同一级目录*/
                                                  
  <transactionManager type="JDBC" commitRequired="false">
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver" value="${driver}"/>
      <property name="JDBC.ConnectionURL" value="${url}"/>
      <property name="JDBC.Username" value="${user}"/>
      <property name="JDBC.Password" value="${password}"/>/*配置文件的Key*/
    </dataSource>
  </transactionManager>
  
 
  <sqlMap resource="com/lxit/test/LXGroup.xml"/>

  /*指定pojo的映射文件,  这句话一定要加上, 否则, 找不到配置文件,包名/类名.xml*/
</sqlMapConfig>
LXGroup.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="LXGroup">

    <typeAlias alias="LXGroup" type="com.lxitedu.pojo.permission.LXGroup" />
     类型别名 , 为一个pojo类取一个别名, 以便在下面用时候, 直接用别名引用对象(上面的解释)
    <resultMap class="LXGroup" id="paginationList">
        <result property="groupId" column="ID" />
        <result property="groupName" column="groupName" />
        <result property="description" column="description" />
    </resultMap>
     数据返回类型,我用的是一张表 , id 与select 标签的id一致, 就可以指定返回是多个pojo
    <select id="userGroupRowCount" resultClass="int">
        select count(*) from userGroup
    </select>
     我们可以, 根据自己的SQL查询语句, 及传入的参数来决定它的返回类型,
       resutlClass="返回类型" parameterClass="传入的参数类型"
    <select id="userGroupList" resultMap="paginationList">
        select * from userGroup
     </select>
    <delete id="deleteUserGroup" parameterClass="String">
        delete from userGroup where groupName=#value#
            #value#由方法的传入参数填充
     </delete>
    <select id="userGroup" parameterClass="String"
        resultClass="LXGroup">
        select ID as groupId,groupName as groupName,description as description from
        userGroup where groupName=#value#
     </select>
    <insert id="addUserGroup" parameterClass="com.lxitedu.pojo.permission.LXGroup">
        insert into userGroup(groupName,description)values
        (#groupName#,## where
        ID=#groupId#
     </update>description#)
        <selectKey resultClass="int" keyProperty="groupId">
            select LAST_INSERT_ID() as value
      </selectKey>(这是主键自动增长)
    </insert>
    <update id="updateUserGroup" parameterClass="LXGroup">
        update userGroup set groupName=#groupName#,description=#description
</sqlMap>
6. 这里所有配置文件都完成了
之后就是得到SqlMapClient对象, 调用相应的方法, 完成相应操作
private static SqlMapClient sqlMapper;
    public static synchronized SqlMapClient getSqlMapClientInstance()
    {  
          if(sqlMapper==null)
          {
              try {
                  Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
                  sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
                  reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
          }
            return sqlMapper;
    }


                         ibatis与spring的整合

1. ibatis与spring整合, 我们要导入相应的jar.commons-dbcp-1.2.jar,commons-pool-1.4.jar,spring2.5.5.jar,这些是Spring的jar
2. 要想spring与ibatis整合起来, 我们只需修改一下配置文件就可以了,如果成功之后, 我们就会发现, 他会使的我们的程序越来越简单
第一步:在SqlMapConfig.xml里 , 要删除 连接数据库驱动。
第二步:在ApplicationContext.xml里修改驱动

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>
                <value>classpath:dbconfig.properties</value>
            </list>
        </property>
    </bean>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName">
            <value>${driver}</value>
        </property>
        <property name="url">
            <value>${url}</value>
        </property>
        <property name="username">
            <value>${user}</value>
        </property>
        <property name="password">
            <value>${password}</value>
        </property>
    </bean>
第三步: 整合ibatis与spring
  <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation" value="classpath:SqlMapConfig.xml"/>(src下)
    <property name="dataSource" ref="dataSource"/>
  </bean>

第四步:修改实例对象方式
如果是单独的ibatis, 我们要通过读取SqlMapConfig.xml来得到SqlMapConfig来得到SqlMapClient对象
现在与spring整合之间, 我们只需要修改类的继承关系就可以了, 一般继承SqlMapClientDaoSupport
this.getSqlMapClient()就可以实现得到数据库的操作对象,简化了操作。
第五步:在sqlmapconfig.xml里面我们只要写映射pojo.xml的路径就可以了。

posted on 2009-11-12 17:00 飞熊 阅读(644) 评论(0)  编辑  收藏 所属分类: Ibatis


只有注册用户登录后才能发表评论。


网站导航:
 
<2009年11月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

文章分类

文章档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜