日期:2014-05-16  浏览次数:20467 次

SPRING使用JDBC

SPRING中提供了3个模板类:

JdbcTemplate,NamedParameterJdbcTemplate,SimpleJdbcTemplate

?

下面介绍以JdbcTemplate为例

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


?<bean id="dataSource"
??class="org.apache.commons.dbcp.BasicDataSource">
??<property name="driverClassName"
???value="com.mysql.jdbc.Driver">
??</property>
??<property name="url" value="jdbc:mysql://127.0.0.1:3306/mydb"></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"></property>
?</bean>
?
?<bean id="teamDaoImpl" class="com.dao.ObjDaoImpl">
??<property name="jdbcTemplate" ref="jdbcTemplate"></property>
?</bean>
?
</beans>

?

DAO类:

package com.dao;

import com.vo.Team;

public interface ObjDao {
?
?void save(Team team);
?
?Team getTeamById(int id);

}

?

package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.vo.Team;

public class ObjDaoImpl implements ObjDao {

?private JdbcTemplate jdbcTemplate;
?public JdbcTemplate getJdbcTemplate() {
??return jdbcTemplate;
?}
?public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
??this.jdbcTemplate = jdbcTemplate;
?}
?public void save(Team team) {
??String sql = "insert into team (Name) values(?)";
??
??this.jdbcTemplate.update(sql,new Object[]{team.getName()});

?}

?

//
?public Team getTeamById(int id) {
??// TODO Auto-generated method stub
??String sql = "select id,name from team where id=?";
??List teams = this.jdbcTemplate.query(sql,new Object[]{Integer.valueOf(id)},new RowMapper(){
???public Object mapRow(ResultSet rs,int rowNum) throws SQLException,DataAccessException{
????Team team = new Team();
????team.setId(rs.getInt("id"));
????team.setName(rs.getString("name"));
????return team;
????
???}
??});
??return teams.size()>0?(Team)teams.get(0):null;
?}

?

//jdbcTemplate.query方法参数:

1.一个字符串,包含用于从数据库里选择数据的SQL语句

2.一个OBJECT数组,包含于查询里索引参数绑定的值

3.一个ROWMAPPER,他从RESULTSET里提取数值,并构造一个域对象

}

测试类:

package com.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dao.ObjDao;
import com.vo.Team;

public class Client {

?/**
? * @param args
? */
?public static void main(String[] args) {
??/*Team team = new Team();
??team.setName("springtest");*/
??
??ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
??ObjDao dao = (ObjDao)context.getBean("teamDaoImpl");
??//dao.save(team);
??Team team = dao.getTeamById(1);
??System.out.println(team.getName());

?}

}

?

在使用SAVE方法的时候,使用了索引参数,意味着吧参数传递给UPDATE的时候,要以正确的顺序列出相应的值。为了避免这种情况,我们可以使用NamedParameterJdbcTemplate.

?

sql="insert into team (name) values (:name)";

?

public void save(Team team)

{

??? Map parameters = new HashMap();

?? param