添加到ibatis相关jar包!
实体类:
package com.spring.model; public class DogPet { private int id; private String name; private int age; private String kind; private String sex; private String health; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getKind() { return kind; } public void setKind(String kind) { this.kind = kind; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getHealth() { return health; } public void setHealth(String health) { this.health = health; } public String toString() { return id+"--"+name+"--"+kind+"--"+age+"--"+health; } }
接口Service:
package com.spring.service; public interface DogPetService { public void queryAllDogPets(); }
实现类ServiceImpl:
package com.spring.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import com.spring.service.DogPetService; import com.spring.dao.DogPetDAO; import com.spring.model.DogPet; @Controller(value="DogPetService") public class DogPetServiceImpl implements DogPetService{ private DogPetDAO dogPetDAO; public DogPetDAO getDogPetDAO() { return dogPetDAO; } @Resource(name="dogPetDAO") public void setDogPetDAO(DogPetDAO dogPetDAO) { this.dogPetDAO = dogPetDAO; } @Override public void queryAllDogPets() { Listlist = dogPetDAO.queryAllDogPets(); if(list != null) { for(DogPet d:list) { System.out.println(d.toString()); } } } }
Service调用的DAO:
package com.spring.dao; import java.util.List; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import com.spring.model.DogPet; public class DogPetDAO extends SqlMapClientDaoSupport { public ListqueryAllDogPets() { List list = null; try { list = getSqlMapClientTemplate().queryForList("c_queryDog"); } catch (Exception e) { e.printStackTrace(); } return list; } }
配置文件beans.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.spring">context:component-scan>
beans>
配置文件dao.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.propertiesvalue>
property>
bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${user}" />
<property name="password" value="${password}" />
bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" >
<value>classpath:sqlMapConfig.xmlvalue>
property>
<property name="dataSource" ref="dataSource"/>
bean>
<bean id="dogPetDAO" class="com.spring.dao.DogPetDAO">
<property name="sqlMapClient" ref="sqlMapClient"/>
bean>
beans>
配置文件sqlMapConfig.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="dogPet.xml" />
sqlMapConfig>
映射文件dogPet.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="Dog">
<typeAlias alias="dog" type="com.spring.model.DogPet" />
<select id="c_queryDog" resultClass="dog">
select *
from t_dog
]]>
select>
<delete id="c_deleteDog" parameterClass="dog">
delete t_dog
where name=#name#
and id = #id#
]]>
delete>
<delete id="c_updateDog" parameterClass="dog">
update t_dog
set name=#name#
where id = #id#
]]>
delete>
<select id="c_queryDogById" parameterClass="dog"
resultClass="dog">
select *
from t_dog
where name=#name#
and id = #id#
]]>
select>
sqlMap>
test类:
package com.spring.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.service.DogPetService; public class ServiceTest { @Test public void queryAllDogPets() { ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml","dao.xml"}); DogPetService dogPetService = (DogPetService)ctx.getBean("DogPetService"); dogPetService.queryAllDogPets(); } }