1. 程式人生 > >演示Spring框架的JDBC模板的簡單操作

演示Spring框架的JDBC模板的簡單操作

1. 步驟一:建立資料庫的表結構
    create database spring_day03;
    use spring_day03;
    create table t_account(
        id int primary key auto_increment,
        name varchar(20),
        money double
    );

2. 引入開發的jar包
    * 先引入IOC基本的6個jar包
    * 再引入Spring-aop的jar包
    * 最後引入JDBC模板需要的jar包
        * MySQL資料庫的驅動包
        * Spring-jdbc.jar
        * Spring-tx.jar

3.編寫配置檔案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"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"
> <!-- 管理DBCP連線池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///spring-day03"/> <property name="username" value
="root"/> <property name="password" value="root"/> </bean> <!-- spring管理模板類 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> </beans>

4.建立UserBean類用於將查詢結果進行儲存或顯示:

package com.huida.bean;

public class UserBean {

    private int id;
    private String name;
    private double money;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getMoney() {
        return money;
    }
    public void setMoney(double money) {
        this.money = money;
    }
    @Override
    public String toString() {
        return "UserBean [id=" + id + ", name=" + name + ", money=" + money + "]";
    }
    
    
}

5.建立BeanMapper類用於記錄查詢結果:

package com.huida.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.huida.bean.UserBean;

public class BeanMapper implements RowMapper<UserBean>{

    @Override
    public UserBean mapRow(ResultSet rs, int arg1) throws SQLException {

        UserBean user=new UserBean();
        user.setId(rs.getInt("id"));
        user.setName(rs.getString("name"));
        user.setMoney(rs.getDouble("money"));
        return user;
    }

    
}

6. 增刪改查操作:

package com.huida.jdbc;

import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.huida.bean.UserBean;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo3 {

    @Resource(name="jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    
    @Test
    //插入操作
    public void run1(){
        jdbcTemplate.update("insert into user values(null,?,?)","小明",10000);
    }
    
    @Test
    //修改操作
    public void run2(){
        jdbcTemplate.update("update user set name=?,money=? where id=?","思雨",1000,2);
        
    }
    
    @Test
    //刪除操作
    public void run3(){
        jdbcTemplate.update("delete from user where id=?",2);
    }
    
    @Test
    //查詢一條記錄
    public void run4(){
        UserBean user=(UserBean) jdbcTemplate.queryForObject("select * from user where id=?", new BeanMapper(),1);
        System.out.println(user);
    }
    
    @Test
    //查詢所有記錄
    public void run5(){
        List<UserBean> list=jdbcTemplate.query("select * from user",new BeanMapper());
        for(UserBean userBean:list){
            System.out.println(userBean);
        }
    }
    
}