SpringJDBC

2019-09-26 14:53:26 浏览数 (1)

除了Spring自带的Jar包,还要下载几个Jar包,这里我吐槽下CSDN,下个Jar包都要积分,真是想钱想疯了,其实可以Google得到

我把所有依赖的Jar包贴出来

MySql数据库的搭建和使用我就不说了,百度即可。现在的MySQL Workbench挺好用的

我们先来看下

普通的JDBC的写法

代码语言:javascript复制
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class JdbcDemo1 {
    @Test
    public void demo1() {
        //创建连接池
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///spring_database");
        dataSource.setUsername("root");
        dataSource.setPassword("1234");
        //创建jdbc模板
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.update("insert into account values (null,?,?)", "大傻", 10000d);
    }
}

下面是SpringJDBC写法

代码语言:javascript复制
public class Account {
    private Integer id;
    private String name;
    private Double money;

    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 getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{"  
                "id="   id  
                ", name='"   name   '''  
                ", money="   money  
                '}';
    }
}

配置文件

applicationContext6.xml

代码语言:javascript复制
<?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:context="http://www.springframework.org/schema/context"
       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">

    <!--引入属性文件-->
    <!--第一种方式:通过bean标签(很少用)-->
    <!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
         <property name="location" value="classpath:jdbc.properties"/>
     </bean>-->
    <!--第二种方式:通过context标签引入-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--配置Spring内置的连接池-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!--属性注入-->
        <property name="driverClassName" value="${jdbc.driverClass}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--配置DBCP连接池-->
   <!-- <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClass}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>-->

    <!--配置C3P0连接池-->
    <!--<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>-->

    <!--配置Spring的JDBC的模板-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

jdbc.properties

代码语言:javascript复制
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring_database
jdbc.username=root
jdbc.password=1234

执行

代码语言:javascript复制
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext6.xml")
public class SpringJDBC {
    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Test
    //保存操作
    public void demo() {
        jdbcTemplate.update("insert into account value (null,?,?)", "大静", 20000d);
    }

    @Test
    //修改操作
    public void demo1() {
        jdbcTemplate.update("update account set name = ?,money = ? where id = ?", "dbcp", 3000d, 4);
    }

    @Test
    //删除操作
    public void demo2() {
        jdbcTemplate.update("delete from account where id = ?", 6);
    }

    @Test
    //查询操作
    public void demo3() {
        String name = jdbcTemplate.queryForObject("select name from account where id = ?", String.class, 2);
        System.out.println("name="   name);
    }

    @Test
    //统计个数
    public void demo4() {
        Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
        System.out.println("count="   count);
    }

    @Test
    //封装到一个对象
    public void demo5() {
        Account account = jdbcTemplate.queryForObject("select * from account where id = ?", new MyRowMapper(), 5);
        System.out.println(account);
    }

    @Test
    //查询多条记录
    public void demo6() {
        List<Account> list = jdbcTemplate.query("select * from account", new MyRowMapper());
        for (Account account : list) {
            System.out.println(account);
        }
    }

    class MyRowMapper implements RowMapper<Account> {

        @Override
        public Account mapRow(ResultSet resultSet, int i) throws SQLException {
            Account account = new Account();
            account.setId(resultSet.getInt("id"));
            account.setName(resultSet.getString("name"));
            account.setMoney(resultSet.getDouble("money"));
            return account;
        }
    }

}

Spring除了可以集成JDBC模板,还可以集成DBCP或者C3P0模板

执行结果就是在MySQL Workbench中查看

0 人点赞