新建一个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");
} }

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

Spring 中的 JDBCTemplate的更多相关文章

  1. Spring中的JDBCTemplate

    src\dayday\JDBCTestTest package dayday;import com.sun.org.apache.xalan.internal.xsltc.compiler.Templ ...

  2. (六)Spring 中的 JdbcTemplate

    目录 概念 配置数据库 创建 JdbcTemplate 对象 增删改查代码 概念 JdbcTemplate : 是 Spring 中对持久层(JDBC 技术)一个封装 : 使用起来和 Dbutuis ...

  3. Spring中的JdbcTemplate使用

    1.引出SpringJDBC的概念 在学习JDBC编程时我们会感觉到JDBC的操作是多么繁琐,那么当我们学习的Hibernate框架时,我们感觉到数据库的操作也变非常简单,提高了开发效率.但是当使用H ...

  4. Spring中的JdbcTemplate的使用

    一.jdbcTemplate的作用 它就是用于和数据库交互的,实现对表的crud.与dbutils相似 二.JdbcTemplate的使用 <dependency> <groupId ...

  5. Spring中的设计模式学习

    Spring提供了一种Template的设计哲学,包含了很多优秀的软件工程思想. 1. 简单工厂 又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一. ...

  6. Spring中的设计模式

    [Spring中的设计模式] http://www.uml.org.cn/j2ee/201301074.asp [详解设计模式在Spring中的应用]    [http://www.geek521.c ...

  7. spring 中的设计模式

    https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=2247485205&idx=1&sn=63455d2313776d ...

  8. Spring中使用的设计模式

    目录 Spring使用的设计模式 1.单例模式 2.原型模式 3.模板模式 4.观察者模式 5.工厂模式 简单工厂模式 工厂方法模式 6.适配器模式 7.装饰者模式 8.代理模式 9.策略模式   S ...

  9. 详解设计模式在Spring中的应用

    设计模式作为工作学习中的枕边书,却时常处于勤说不用的尴尬境地,也不是我们时常忘记,只是一直没有记忆. 今天,在IT学习者网站就设计模式的内在价值做一番探讨,并以spring为例进行讲解,只有领略了其设 ...

随机推荐

  1. MyBatis从入门到精通(第5章):5.4 Example 介绍

    jdk1.8.MyBatis3.4.6.MySQL数据库5.6.45.Eclipse Version: 2019-12 M2 (4.14.0) MyBatis从入门到精通(第5章):MyBatis代码 ...

  2. Java之多线程窗口卖票问题(Thread)

    /** * * 例子:创建三个窗口卖票,总票数为100张.使用继承Thread类的方式 * * 存在线程的安全问题,待解决. * */class Window extends Thread{ priv ...

  3. http协议笔记(不全)

    1.URL 统一资源定位系统 URL由三部分组成:资源类型.存放资源的主机域名.资源文件名.url是统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址 ...

  4. Maven--Eclipse maven相关配置

    选择自己安装的 Maven 版本: 更改配置文件路径,这里选择自己安装的 Maven 下的配置文件,方便配置及统一控制:

  5. POJ 1845 Sumdiv [素数分解 快速幂取模 二分求和等比数列]

    传送门:http://poj.org/problem?id=1845 大致题意: 求A^B的所有约数(即因子)之和,并对其取模 9901再输出. 解题基础: 1) 整数的唯一分解定理: 任意正整数都有 ...

  6. python 并发执行

    并发执行, 精简代码. 适用python2 和python3 # -*- encoding:utf-8 -*- from threading import Thread from multiproce ...

  7. 吴裕雄--天生自然 pythonTensorFlow自然语言处理:PTB 语言模型

    import numpy as np import tensorflow as tf # 1.设置参数. TRAIN_DATA = "F:\TensorFlowGoogle\\201806- ...

  8. elasticsearch-hadoop 扩展定制 官方包以支持 update upsert doc

    官方源码地址https://github.com/elastic/elasticsearch-hadoop 相关文档 https://www.elastic.co/guide/en/elasticse ...

  9. 用shell脚本生成at一次性的计划任务

    用shell生成一次性计划任务,这个任务就是执行另一个脚本 #!/bin count=`grep "sh /usr/local/sbin/iptables.sh" /var/spo ...

  10. sin之舞---蓝桥杯练习

    问题描述 最近FJ为他的奶牛们开设了数学分析课,FJ知道若要学好这门课,必须有一个好的三角函数基本功.所以他准备和奶牛们做一个“Sine之舞”的游戏,寓教于乐,提高奶牛们的计算能力. 不妨设 An=s ...