Spring与JDBC整合应用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring与JDBC整合应用相关的知识,希望对你有一定的参考价值。
Spring与JDBC整合背景:
1.Spring提供了编写Dao的工具类:JdbcTemplate
JdbcTemplate.update("insert....",参数);
JdbcTemplate.query();//查询多行记录
JdbcTemplate.queryForObject();//查询单行记录
int rows=JdbcTemplate.queryForInt();
2.Spring提供了AOP式事务管理(不需要在方法中追加事务提交和回滚)
3.提供了统一的异常处理:DataAccessException
Spring整合JDBC步骤:
A.搭建开发环境:引入Spring(ioc,aop,dao)开发包,添加applicationContext.xml文件
配置数据驱动及dbcp连接池
B.编写实体类及
package com.web.entity; import java.io.Serializable; public class Emp implements Serializable { //封装类可以分装null值 private Integer id; private String name; private Double salary; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
注意:当需要查询时必须创建该表对应的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整合应用的主要内容,如果未能解决你的问题,请参考以下文章