导入的jar包在
在springjdbc中处理数据的为JdbcTemplate所以现在springmvc中配置,因为也使用了dataSource所以也需要把传入其中
<?xml version="1.0" encoding="UTF-8"?> <!-- 配置最简单的beans --> <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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 配置最简单的context --> <!-- <bean name="/hello" class="cn.jiedada.controller.HelloController"></bean> --> <!-- 扫描@controller --> <context:component-scan base-package="cn.jiedada"></context:component-scan> <!-- 调用@requestMapping() --><!-- 支持注解 --> <mvc:annotation-driven> <!-- 避免在IE浏览器中返回JSON时出现下载文件的情况 --> <mvc:message-converters> <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 放行静态资源 --> <mvc:default-servlet-handler/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property><!-- 加入前缀 --> <property name="suffix" value=".jsp"></property><!-- 加入后缀 --> </bean> <!-- 配置上传下载文件 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="1048576"></property> </bean> <!-- 配置拦截器 --> <!-- <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/*" /> <mvc:exclude-mapping path="/login"/> <bean class="cn.itsource.springmvc._06_interceptor.MyInterceptor" /> </mvc:interceptor> </mvc:interceptors> --> <!-- 是我们能够获得properties文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 连接池,dao,service,的相互依赖关系 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 配置jdbcTemplate,并且把dataSource传入其中 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
@Autowired
private JdbcTemplate jdbc;
通过注入就可以实现了,然后直接使用就好了里面的方法也就,当需要对象的时候需要使用
new BeanPropertyRowMapper<User>(User.class)获得封装对象
package cn.jiedada.dao.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import cn.jiedada.dao.IUserDao; import cn.jiedada.domain.User; @Repository public class UserDaoImpl implements IUserDao { @Autowired private JdbcTemplate jdbc; @Override public List<User> findAll() { return jdbc.query("select * from user", new BeanPropertyRowMapper<User>(User.class)); } @Override public void delete(Integer id) { jdbc.update("delete from user where id=?",id); } @Override public User update(Integer id) { // TODO Auto-generated method stub return jdbc.queryForObject("select * from user where id=?", new BeanPropertyRowMapper<User>(User.class), id); } @Override public void modify(User user) { String sql="update user set name=?,age=?,sal=? where id=?"; jdbc.update(sql, user.getName(),user.getAge(),user.getSal(),user.getId()); } }