admin管理员组

文章数量:1794759

Spring mvc中jdbcDaoSupport和jdbcTemplate的使用

Spring mvc中jdbcDaoSupport和jdbcTemplate的使用

1.注解使用jdbcTemplate:

package xm.zjl.dao; import java.util.ArrayList; import java.util.List; import org.Springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; /** * 测试标签dao * * @author zjl * */ @Repository public class TagTestDao{ @Autowired private JdbcTemplate jdbcTemplate; public List getList(){ List list = new ArrayList(); String sql = "select * from user"; jdbcTemplate.execute(sql); return list; } } 2.配置文件:

<beans xmlns="www.springframework/schema/beans" xmlns:context="www.springframework/schema/context" xmlns:p="www.springframework/schema/p" xmlns:mvc="www.springframework/schema/mvc" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans-3.0.xsd www.springframework/schema/context www.springframework/schema/context/spring-context.xsd www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc-3.0.xsd"> <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射--> <mvc:annotation-driven /> <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean --> <context:component-scan base-package="xm.zjl.*" /> <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" /> <bean id="dataSource" class="org.apachemons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/mydata"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> </beans> 2.继承jdbcDaoSupport类,但是dataSource没有设置成功(注解方式),若果有方法请留言,多谢

本文标签: MVCspringjdbcTemplatejdbcDaoSupport