总结:SPRNG2.5.6+IBATIS

libs:

c3p0-0.9.1.2.jar

jtds-1.2.5.jar

commons-logging.jar

log4j-1.2.15.jar

spring.jar

ibatis-2.3.4.726.jar

环境:JDK1.6+SPRING2.5.6+IBATIS2

DB:SQLSERVER2000(驱动JTDS,c3p0连接池)

 

=====Spring配置如下(service.xml)============

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

           http://www.springframework.org/schema/context 

        http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<import resource="sql-map-daos.xml" />

<bean id="productService" class="com.me.service.ProductService">

<property name="productDao">

<ref bean="productDao" />

</property>

</bean>

...

</beans>

============sql-map-daos.xml===========

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 

<bean id="productDao" class="com.me.dao.ibatis.SqlMapProductDao">

<property name="sqlMapClient" ref="sqlMapClient" />

</bean>

<bean id="supplierDao" class="com.me.dao.impl.SupplierDaoImpl" />

 

<bean

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<value>classpath:conn/jdbc.properties</value>

</property>

</bean>

<bean id="sqlMapClient"

class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">

<property name="configLocation" value="sql-map-config.xml" />

<property name="dataSource" ref="dataSource" />

</bean>

<bean id="dataSource"

class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close">

<property name="driverClass" value="${jdbc.driverClassName}" />

<property name="jdbcUrl" value="${jdbc.url}" />

<property name="user" value="${jdbc.username}" />

<property name="password" value="${jdbc.password}" />

 

<property name="minPoolSize" value="1" />

<property name="initialPoolSize" value="2" />

<property name="maxPoolSize" value="20" />

 

<property name="maxIdleTime" value="1800" />

<property name="acquireIncrement" value="2" />

<property name="maxStatements" value="3" />

 

<property name="idleConnectionTestPeriod" value="1800" />

<property name="acquireRetryAttempts" value="3" />

</bean>

</beans>

============sql-map-config.xml==============
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
    "http://www.ibatis.com/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
<sqlMap resource="com/me/dao/ibatis/maps/Product.xml" />
</sqlMapConfig>
===============Product.xml====================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap namespace="Product">

  <cacheModel id="oneDayProduct" type="MEMORY">
    <flushInterval hours="24"/>
    <property name="reference-type" value="WEAK"/>
  </cacheModel>

  <resultMap id="result" class="com.me.domain.Product">
    <result property="productId" column="productid" columnIndex="1"/>
    <result property="name" column="name" columnIndex="2"/>
    <result property="description" column="descn" columnIndex="3"/>
    <result property="categoryId" column="category" columnIndex="4"/>
  </resultMap>

  <select id="getProduct" resultMap="result">
    select productid, name, descn, category from product where productid = #value#
  </select>

</sqlMap>
================= SqlMapProductDao =======
public class SqlMapProductDao extends SqlMapClientDaoSupport implements
ProductDao {

public List getProductListByCategory(String categoryId)
throws DataAccessException {
return getSqlMapClientTemplate().queryForList(
"getProductListByCategory", categoryId);
}

public Product getProduct(String productId) throws DataAccessException {
return (Product) getSqlMapClientTemplate().queryForObject("getProduct",
productId);
}
....
================ Product ========
public class Product implements Serializable {

  /* Private Fields */

  private String productId;
  private String categoryId;
  private String name;
  private String description;
....setter/getter...
===============测试如下:
public class IBATISDBTest {

/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "services.xml", "sql-map-*.xml" });
ProductService productService = (ProductService) context
.getBean("productService");
String productId="AV-CB-01";
productService.getProductByID(productId);
}

}

 

 

你可能感兴趣的:(ibatis)