<strong><span style="font-size:18px;">Person.java-----------------------------------------------------------------------------
package ajdbc.bean;
public class Person {
private Long id;
private String name;
public Person() {
}
public Person(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
footBallService.java ------------------------------------------------------------
package ajdbc.service;
import java.util.List;
import ajdbc.bean.Person;
public interface FootBallService {
public void insert(Person person);
public Person queryById(Integer id);
public List<Person> queryAll();
}
FootBallServiceImpl101iBatis.java ------------------------------------------------------------------
package ajdbc.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import ajdbc.bean.Person;
import ajdbc.service.FootBallService;
public class FootBallServiceImpl101iBatis implements FootBallService{
private SqlMapClientTemplate sqlMapClientTemplate;
public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
this.sqlMapClientTemplate = sqlMapClientTemplate;
}
/**
* 插入
*/
public void insert(Person person) {
sqlMapClientTemplate.insert("personiBatis.insert", person);
}
/**
* 根据id查询
*/
public Person queryById(Integer id) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", id);
return (Person)sqlMapClientTemplate.queryForObject("personiBatis.find", map);
}
/**
* 查询全部
*/
@SuppressWarnings("unchecked")
public List<Person> queryAll() {
return (List<Person>)sqlMapClientTemplate.queryForList("personiBatis.find", null);
}
}
applicationContextiBatis.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="initialSize" value="${initialSize}" />
<property name="maxActive" value="${maxActive}" />
<property name="minIdle" value="${minIdle}" />
<property name="maxIdle" value="${maxIdle}" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:sql-map-config.xml" />
</bean>
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<bean id="footBallServiceImpl101iBatis" class="ajdbc.service.impl.FootBallServiceImpl101iBatis">
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate" />
</bean>
<context:property-placeholder location="classpath:jdbc.properties" />
</beans>
jdbc.properties--------------------------------------------------------------------
driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=scott
password=scott
initialSize=2
maxActive=10
minIdle=1
maxIdle=2
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>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="256"
maxSessions="64"
maxTransactions="16"
useStatementNamespaces="true"
/>
<sqlMap resource="PersonMap.xml" />
</sqlMapConfig>
PersonMap.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="personiBatis">
<typeAlias alias="person1" type="ajdbc.bean.Person" />
<sql id="Select_Field_Clause">
id,
name
</sql>
<resultMap id="person-result" class="person1">
<result column="id" property="id" jdbcType="DECIMAL" />
<result column="name" property="name" jdbcType="VARCHAR" />
</resultMap>
<insert id="insert" parameterClass="person1">
<selectKey resultClass="java.lang.Long" keyProperty="id">
SELECT PERSON_SEQ.nextval FROM DUAL
</selectKey>
insert into person(id, name) values(#id:DECIMAL#, #name:VARCHAR#)
</insert>
<select id="find" parameterClass="java.util.Map" resultMap="person-result">
select
<include refid="Select_Field_Clause" />
from person
<dynamic prepend="where">
<isNotEmpty prepend="and" property="id">
id=#id#
</isNotEmpty>
</dynamic>
</select>
</sqlMap>
测试类TestByUnit.java -----------------------------------------------------------------
package ajdbc.test;
import java.util.List;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import ajdbc.bean.Person;
import ajdbc.service.FootBallService;
public class TestByiBatis extends TestCase{
ApplicationContext ctx = null;
@Override
protected void setUp() throws Exception {
System.out.println("%%%%%%%%%%%%%");
ctx = new ClassPathXmlApplicationContext("applicationContextiBatis.xml");
}
/**
* 测试iBatis
*/
public void testiBatis(){
FootBallService footBallService = (FootBallService)ctx.getBean("footBallServiceImpl101iBatis");
// insert方法
// footBallService.insert(new Person("奇迹"));
Person ps = footBallService.queryById(90);
System.out.println(ps.getId() + "-->"+ ps.getName());
System.out.println("-----------------------------------------------");
List<Person> personLs = footBallService.queryAll();
for(Person p : personLs){
System.out.println(p.getId() + "-->"+ p.getName());
}
}
}</span></strong>
<strong><span style="font-size:18px;">用SqlMapClientDaoSupport做:
只需要在1的基础上变换2个文件
FootBallServiceImpl101iBatis.java --------------------------------------------------------------------
package ajdbc.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import ajdbc.bean.Person;
import ajdbc.service.FootBallService;
public class FootBallServiceImpl101iBatis extends SqlMapClientDaoSupport implements FootBallService{
/**
* 插入
*/
public void insert(Person person) {
this.getSqlMapClientTemplate().insert("personiBatis.insert", person);
}
/**
* 根据id查询
*/
public Person queryById(Integer id) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", id);
return (Person)this.getSqlMapClientTemplate().queryForObject("personiBatis.find", map);
}
/**
* 查询全部
*/
@SuppressWarnings("unchecked")
public List<Person> queryAll() {
return (List<Person>)this.getSqlMapClientTemplate().queryForList("personiBatis.find", null);
}
}
applicationContextiBatis.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="initialSize" value="${initialSize}" />
<property name="maxActive" value="${maxActive}" />
<property name="minIdle" value="${minIdle}" />
<property name="maxIdle" value="${maxIdle}" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:sql-map-config.xml" />
</bean>
<bean id="footBallServiceImpl101iBatis" class="ajdbc.service.impl.FootBallServiceImpl101iBatis">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<context:property-placeholder location="classpath:jdbc.properties" />
</beans></span></strong>