第一步:導入相關Jar包:
第二步:配置相關配置文件
jdbc.properties
driverName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/test
username=root
password=123456
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: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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 讀取配置文件(必須添加相應的名字空間(context)) -->
<context:property-placeholder
location="WEB-INF/classes/jdbc.properties" />
<!-- 註冊action -->
<bean id="testAction" class="com.dx.mobile_scm.test.TestAction">
<property name="testService" ref="testService" />
</bean>
<!-- 註冊service服務 -->
<bean id="testService" class="com.dx.mobile_scm.service.TestServiceImpl">
<property name="dao" ref="dao"></property>
</bean>
<!-- 配置數據源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driverName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="maxActive" value="30"></property>
<property name="maxIdle" value="10"></property>
<property name="minIdle" value="5"></property>
<property name="maxWait" value="5000"></property>
</bean>
<!-- 配置ibatis工廠 -->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="/WEB-INF/classes/SqlMapConfig.xml">
</property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 註冊Dao -->
<bean id="dao" class="com.dx.mobile_scm.dao.CommonDaoImpl">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
</beans>
第三步:測試
CommonDao.java
package com.dx.mobile_scm.dao;
public interface CommonDao {
public Object queryForObject(String sqlId, Object parameter);
}
CommonDaoImpl.java
package com.dx.mobile_scm.dao;
import java.sql.SQLException;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
public class CommonDaoImpl extends SqlMapClientTemplate implements CommonDao {
@Override
public Object queryForObject(String sqlId, Object parameter) {
Object o = null;
try {
o = getSqlMapClient().queryForObject(sqlId, parameter);
} catch (SQLException e) {
e.printStackTrace();
}
return o;
}
}
在Service層調用dao
package com.dx.mobile_scm.service;
import com.dx.mobile_scm.dao.CommonDao;
import com.dx.mobile_scm.test.User;
public class TestServiceImpl implements TestService {
private CommonDao dao;
public void setDao(CommonDao dao) {
this.dao = dao;
}
public String validate(){
System.out.println(dao);
User u = (User) dao.queryForObject("findUser", 3);
System.out.println(u.getName());
return "hello world";
}
}