Spring 中的 JDBCTemplate

 新建一个java工程 写好spring配置文件,直接上代码

<?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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd xsi:schemaLocation=”http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.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.xsd">

    <!-- 加载配置文件 -->

    <context:property-placeholder location="db.properties"></context:property-placeholder>
    
    <!-- 导入C3P0数据源 -->
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.Username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="jdbcUrl" value="${jdbc.Url}"></property>
        <property name="driverClass" value="${jdbc.Driver}"></property>
    </bean>
    
    <!-- 配置Spring JdbcTemplate -->
    <bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    
</beans>

不知道什么意思可以看上面的注解,

接下来我们写好我们数据库的基本配置文件

jdbc.Driver=com.mysql.jdbc.Driver
jdbc.Username=root
jdbc.password=root
jdbc.Url=JDBC:mysql://127.0.0.1:3306/mydata

这里的话是配合上面c3p0数据库使用的,以便可以成功读取到数据库。

接下来我们再写一个实体类,后面用到查询数据的时候会用到实体类。

package com.xiaojiang.template;

public class MyJDBCData {
    
    private int id;
    private String name;
    private int age;
    private String sex;
    
    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 int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "MyJDBCData [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]";
    }
    

}

数据库和数据表自己去新建,然后再根据自己创建的字段名来写这个实体类。

接下来我们上测试代码

package com.xiaojiang.template;

import static org.junit.jupiter.api.Assertions.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

class jdbcTest {
    
    private String sql []  = {
            "insert into attributes(id,name,age,sex) values(?,?,?,?)",
            "update attributes set name = ?  where id = ?",
            "delete from attributes where id = ?",
            "select *from attributes where id = ?",
            "select *from attributes where id >= ?",
            "select count(id) attributes from attributes"
            };
    
    private  ApplicationContext ac = null;
    private  JdbcTemplate p = null;
    
    {
        ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        p = (JdbcTemplate) ac.getBean("JdbcTemplate");
    }
    
    //查询多条数据
    @Test
    public void testQueryForList()
    {
        RowMapper<MyJDBCData> rowMapper = new BeanPropertyRowMapper<>(MyJDBCData.class);
        List<MyJDBCData> data = p.query(sql[4], rowMapper,1);
        System.out.println(data);
    }
    
    //获取数据库指定的数据,并得到一个对象
    @Test
    public void testQueryForObject()
    {
        //把查询结果转换成一个实体
        RowMapper<MyJDBCData> rowMapper = new BeanPropertyRowMapper<>(MyJDBCData.class);
        MyJDBCData data = p.queryForObject(sql[3],rowMapper,1);
        System.out.println(data.toString());
    }
    
    //批量增加数据
    @Test 
    public void testbatchData()
    {
        List<Object[]> batchArgs = new ArrayList<>();
        batchArgs.add(new Object[]{"1","测试1","18","男"});
        batchArgs.add(new Object[]{"2","测试2","19","男"});
        batchArgs.add(new Object[] {"3","测试3","20","女"});
        p.batchUpdate(sql[0], batchArgs);
    }
    //查询单个列的值,做统计查询
    @Test
    public void testQueryForObject1()
    {
        Long count = p.queryForObject(sql[5],Long.class);
        System.out.println(count);
    }
    
    //执行单条 INSERT UPDATE DELETE
    @Test 
    public void testInsert()
    {
        p.update(sql[0],"1","小江","18","男");
    }
    //修改
    @Test
    public void testUpdate()
    {
        p.update(sql[1],"小红",1);
    }
    //删除
    @Test
    public void testDelete()
    {
        p.update(sql[2],1);
    }

    @Test
    void test() {
        fail("Not yet implemented");
    }

}

我们在用的时候最好把他写成一个类,要用到谁的时候就去调用就行了,我这里只是测试一下,就不多写了,写的简单不喜勿喷。

posted @ 2018-07-23 01:03  小江。  阅读(270)  评论(0编辑  收藏  举报