Spring中的jdbc模板使用

##(一) 手动利用代码实现(Spring自带的数据库连接池)

import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**JDBC模板使用
 * @author kiosk
 */
public class JDBCDemo1 {
    @Test
    public void demo1(){
        //创建连接池
        DriverManagerDataSource source = new DriverManagerDataSource();
        //创建JDBC的模板
        JdbcTemplate template = new JdbcTemplate(source);
        source.setDriverClassName("com.mysql.jdbc.Driver");
        source.setUrl("jdbc:mysql://localhost:3306/westos?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false");
        source.setUsername("root");
        source.setPassword("123");
        template.update("insert into account values (null,?,?)","sunmomou",1000d);
    }
}

(二)利用Spring的Bean管理实现

配置文件(c3p0)

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx.xsd">
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置连接池-->
    <!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
        <!--<property name="username" value="${jdbc.username}"/>-->
        <!--<property name="password" value="${jdbc.password}"/>-->
        <!--<property name="driverClassName" value="${jdbc.driver}"/>-->
        <!--<property name="url" value="${jdbc.url}"/>-->
    <!--</bean>-->
    <!--配置DBCP连接池-->
    <!--<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">-->
        <!--<property name="url" value="${jdbc.url}"/>-->
        <!--<property name="driverClassName" value="${jdbc.driver}"/>-->
        <!--<property name="username" value="${jdbc.username}"/>-->
        <!--<property name="password" value="${jdbc.password}"/>-->
        <!--<property name="maxIdle" value="${jdbc.max}"/>-->
        <!--<property name="minIdle" value="${jdbc.min}"/>-->
    <!--</bean>-->
    <!--配置c3p0连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="driverClass" value="${jdbc.driver}"/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

编写实体类


public class Person {
    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 "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

编写映射类(实体类的名字与表名)

import org.springframework.jdbc.core.RowMapper;

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

/**
 * 属性的映射
 * @author kiosk
 */
public class MyAccount implements RowMapper<Person> {
    public Person mapRow(ResultSet resultSet, int i) throws SQLException {
        Person person = new Person();
        person.setId(resultSet.getInt("id"));
        person.setName(resultSet.getString("name"));
        person.setMoney(resultSet.getDouble("money"));
        return person;
    }
}

测试类

import com.aop.damain.MyAccount;
import com.aop.damain.Person;
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 javax.annotation.Resource;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:springJDBC.xml")
public class Test2 {
    @Resource(type = JdbcTemplate.class)
    private JdbcTemplate jdbcTemplate;

    @Test
    public void test1() {
        jdbcTemplate.update("insert into account  values (null,?,?)", "java and linux", 2000000000d);

    }

    @Test
    public void test2() {
        jdbcTemplate.update("update account set name = ? ,money = ? where id = ?", "linux", "12000", 7);

    }

    @Test
    public void test3() {
        String s = jdbcTemplate.queryForObject("select name from account where id = ?", String.class, 1);
        System.out.println(s);
    }

    @Test
    public void test4() {
        List<Person> personList = jdbcTemplate.query("select * from account", new MyAccount());
        for (Person person : personList) {
            System.out.println(person);
        }

    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40843624/article/details/86765983
今日推荐