时间:2021-07-01 10:21:17 帮助过:17人阅读
注意:当需要查询时必须创建该表对应的RowMapper类(将记录映射成对象)如下:
package com.web.entity; import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; /* * 将emp记录封装为Emp对象 */ public class EmpRowMapper implements RowMapper<Emp>{ public Emp mapRow(ResultSet rs, int index) throws SQLException { //将当前rs指针指向的记录取出,封装成Emp返回 Emp emp=new Emp(); emp.setId(rs.getInt("id")); emp.setName(rs.getString("name")); emp.setSalary(rs.getDouble("salary")); emp.setAge(rs.getInt("age")); return emp; } }
C.编写DAO组件:实现增删改查
package com.web.dao; import java.util.List; import javax.annotation.Resource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import com.web.entity.Emp; import com.web.entity.EmpRowMapper; @Repository //扫描Dao public class EmpDao { @Resource//注入 private JdbcTemplate template;//注入,另一种方法是继承 public void save(Emp emp){ String sql="insert into emp (name,salary,age) values(?,?,?)"; Object[] params={emp.getName(), emp.getSalary(), emp.getAge() }; template.update(sql,params); } public void delete(int id){ String sql="delete from emp where id=?"; Object[] params={id}; template.update(sql,params); } public List<Emp> finalAll(){ String sql="select * from emp"; EmpRowMapper rowMapper=new EmpRowMapper(); List<Emp> list=template.query(sql, rowMapper);//rowMapper return list; } //多行查询用query()方法 //单行查询用queryForObject()方法 public Emp findById(int id){ String sql="select * from emp where id=?"; Object[] params={id}; EmpRowMapper rowMapper=new EmpRowMapper(); Emp emp=template.queryForObject(sql, params,rowMapper); return emp; } }
D.在applicationContext.xml中扫描EmpDao,注入JdbcTemplate
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 开启组件扫描 --> <context:component-scan base-package="com.web"/> <!-- 定义JDBCTemplate --> <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate"> <!-- 注入连接信息 --> <!-- DataSource:数据源,连接池 --> <property name="dataSource" ref="dbcp"></property> </bean> <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource"> <property name="username" value="root"></property> <property name="password" value="1234"></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql:///jsd1507db?useUnicode=true&characterEncoding=utf8"></property> </bean> </beans>
Spring与JDBC整合应用
标签:res text driver ret 数据源 apach base web gets