• ibatis和spring整合


    以前写在百度空间的,搬楼了

    -------------------------分割线----------------------------

     

    研究了下,网上资料也不少,看来ORM的ibatis还是很多人热衷的。

    因为ibatis是作为数据访问的,必然有自己的数据源,我们使用spring中的数据源来代替ibatis的,删除ibatis中数据源相关datasource部分,在spring配置文件中添加以下代码:

     

    代码
    <bean id="dataSource"
    class
    ="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${driver}" />
    <property name="url" value="${url}" />
    <property name="username" value="${username}" />
    <property name="password" value="${password}" />
    </bean>

    可以作为datasource的类有好多个,选择一个

    spring的Datasource解释:

    http://envoydada.javaeye.com/blog/95166

    同样在spring中使用配置文件properties:

     

    代码
    <bean id="propertyManager" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>classpath:SqlMapConfigExample.properties</value>
    </list>
    </property>
    </bean>

    这个方法我也觉得很神奇-。-!

    建立DAO类,继承org.springframework.orm.ibatis.support.SqlMapClientDaoSupport

    其中的方法使用getSqlMapClientTemplate()来进行数据库操作

    例如:

     

    java代码
    public Student selectByPrimaryKey(String sno) {
    Student key
    = new Student();
    key.setSno(sno);
    Student record
    = (Student) getSqlMapClientTemplate().queryForObject("selectByPrimaryKey", key);
    return record;
    }

    这个类需要spring注入2个属性sqlmapclient和datasource

    sqlmapclient就是需要你的ibatis的配置文件,参数等等

    datasource就是刚才spring配置的那个bean

    不过sqlmapclient没有一个类可以直接提供。。。所以使用SqlMapClientFactoryBean工厂类来生成一个sqlmapclient(spring中的bean就是这样越来越多的……怪不得配置越来越麻烦),例如:

     

    代码
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation" value="classpath:sql-map-config.xml" />
    <property name="dataSource" ref="dataSource" />
    </bean>

    这样就搞定sqlmapclient和datasource了

    dao的bean配置:

     

    <!-- dao -->
    <bean id="studentDao" class="dal.StudentDAOImpl">
    <property name="dataSource" ref="dataSource" />
    <property name="sqlMapClient" ref="sqlMapClient" />
    </bean>

    剩下就接下去配置spring的事务管理什么的,不写了。。。

  • 相关阅读:
    动态更新活动条件
    使用本地服务异步执行自定义活动业务逻辑
    隐藏自定义复合活动的内部实现
    如何开发一个基本的复合活动
    HTML5性能之争 —— 单线程:缺点还是特点?
    CDNJS —— Web 上最快的 JavaScript 资源库
    Comfusion 4.1 发布,桌面 Linux 发行
    FreeBSD 9.1 正式版已经可以下载
    Squid Analyzer 5.0 发布,Squid日志统计
    MemSQL 1.8 发布,号称最快的关系数据库
  • 原文地址:https://www.cnblogs.com/xiziyin/p/1664882.html
Copyright © 2020-2023  润新知