spring配置文件applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>oracle.jdbc.driver.OracleDriver</value> </property> <property name="username"> <value>scott</value> </property> <property name="password"> <value>tiger</value> </property> <property name="url"> <value>jdbc:oracle:thin:@localhost:1521:orcl</value> </property> </bean> <bean id="sqlMapClientFactoryBean" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>classpath:ibatis.xml</value> </property> </bean> <bean id="personDao" class="org.pxw.dao.imple.PersonDaoImple"> <property name="dataSource" ref="dataSource"></property> <property name="sqlMapClient" ref="sqlMapClientFactoryBean"></property> </bean> </beans>
ibatis 的sqlmapclicent.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="32" maxSessions="10" maxTransactions="5" useStatementNamespaces="true" /> <sqlMap resource="person.xml"/> </sqlMapConfig>
jopo的配置加操作数据的配置 person.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="Person"> <typeAlias type="org.pxw.entity.Person" alias="person"/> <resultMap id="ibatisTest" class="person" > <result column="pid" property="pid" jdbcType="number" /> <result column="pname" property="pname" jdbcType="VARCHAR2" /> <result column="page" property="page" jdbcType="number"/> </resultMap> <!-- 获得全查询列表 --> <select id="findperson" resultMap="ibatisTest"> select * from person </select> </sqlMap>
PersonDao.xml
import java.util.List; public interface PersonDao { //查询 public List findall(); }
PersonDaoImple.xml
import java.util.List; import org.pxw.dao.PersonDao; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; public class PersonDaoImple extends SqlMapClientDaoSupport implements PersonDao { @Override public List findall() { // TODO Auto-generated method stub return getSqlMapClientTemplate().queryForList("Person.findperson",null); } }
测试:
public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); persondao = (PersonDao)context.getBean("personDao"); List<Person> persons = persondao.findall(); System.out.println(persons.size()); for (Person person : persons) { System.out.println(person.getPname()); }