1、添加模板的配置文件
在spring的配置文件中加入如下代码
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
注意:ref 中的 dataSource 是可变的,与连接数据库的资源ID相匹配,例如:
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="CCPITDS" name="CCPITDS">
<property name="driverClassName" value="${CCPIT.connection.driver_class}"/>
<property name="username" value="${CCPIT.connection.username}"/>
<property name="password" value="${CCPIT.connection.password}"/>
<property name="url" value="${CCPIT.connection.url}"/>
<property name="maxIdle" value="${CCPIT.minPoolSize}"/>
<property name="maxActive" value="${CCPIT.maxPoolSize}"/>
</bean>
<!-- JdbcTemplate -->
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<property name="dataSource" ref="CCPITDS"/>
</bean>
2、代码部分
@Controller
public class JdbcController {
//@Autowired必须加上,要不就是空指针喽!!!
@Autowired
private JdbcTemplate jdbcTemplate;
//直接执行SQL语句
@RequestMapping(value = "/item", method = RequestMethod.GET)
@ResponseBody
public ArrayList item(){
ArrayList ls = (ArrayList)jdbcTemplate.queryForList("select * from TASK");
return ls;
} }