1.引入相應的jar包後,配置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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 配置jdbcTemplate要用的連接池,採用dbcp數據源 -->
<bean id="dataSourceJdbcTemplate"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" ></property>
<!-- 測試MySQL -->
<property name="url" value="jdbc:mysql://192.168.188.208:3306/discuz_cn"></property>
<property name="username" value="zentalk_cn" ></property>
<property name="password" value="zentalk#cn"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
<property name="validationQuery" value="SELECT 1" />
<property name="testOnBorrow" value="true"/>
</bean>
<!-- 將配置好的dataSource注入到jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource"><ref bean="dataSourceJdbcTemplate" />
</property>
</bean>
<!-- 將配置好的jdbcTemplate注入到類dataSouceClass中,可以在這個類中使用jdbcTemplate -->
<bean id="dataSouceClass" class="cn.springmvc.query.Dme="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>
2.創建dataSourceClass工具類
package cn.springmvc.query;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
public class DataSourceClaemplate;
public static JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jt) {
jdbcTemplate = jt;
}
}
3.使用jdbcTemplate的一些方法
public class User {
public static List<UserSignInfo> getUserInfo(int userid) {
String sql = "SELECT * FROM pre_plugin WHERE uid = " + userid;
final List<UserSignInfo> ret = new ArrayList<UserSignInfo>();
JdbcTemplate jt = DataSourceClass.getJdbcTemplate();
jt.query(sql, null, null, new RowCallbackHandler(){
public void processRow(ResultSet rs) throws SQLException{
UserSignInfo us = new UserSignInfo();
us.setDays(rs.getInt("days"));
us.setMdays(rs.getInt("mdays"));
us.setLasted(rs.getInt("lasted"));
us.setLastreword(rs.getInt("lastreward"));
us.setReworad(rs.getInt("reward"));
us.setTime(rs.getInt("time"));
us.setUserid(rs.getInt("uid"));
us.setRow(rs.getInt("row"));
ret.add(us);
}
});
return ret;
}
}
4.創建測試方法類
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.springmvc.query.UserSign;
public class signTest {
public static void main(String[] args) {
//引入jdbcTemplate相關配置
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:conf/applicationContext.xml");
List<UserSignInfo> result=User.getUserInfo(24875);
}
}