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");
} }
我们在用的时候最好把他写成一个类,要用到谁的时候就去调用就行了,我这里只是测试一下,就不多写了,写的简单不喜勿喷。
Spring 中的 JDBCTemplate的更多相关文章
- Spring中的JDBCTemplate
src\dayday\JDBCTestTest package dayday;import com.sun.org.apache.xalan.internal.xsltc.compiler.Templ ...
- (六)Spring 中的 JdbcTemplate
目录 概念 配置数据库 创建 JdbcTemplate 对象 增删改查代码 概念 JdbcTemplate : 是 Spring 中对持久层(JDBC 技术)一个封装 : 使用起来和 Dbutuis ...
- Spring中的JdbcTemplate使用
1.引出SpringJDBC的概念 在学习JDBC编程时我们会感觉到JDBC的操作是多么繁琐,那么当我们学习的Hibernate框架时,我们感觉到数据库的操作也变非常简单,提高了开发效率.但是当使用H ...
- Spring中的JdbcTemplate的使用
一.jdbcTemplate的作用 它就是用于和数据库交互的,实现对表的crud.与dbutils相似 二.JdbcTemplate的使用 <dependency> <groupId ...
- Spring中的设计模式学习
Spring提供了一种Template的设计哲学,包含了很多优秀的软件工程思想. 1. 简单工厂 又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一. ...
- Spring中的设计模式
[Spring中的设计模式] http://www.uml.org.cn/j2ee/201301074.asp [详解设计模式在Spring中的应用] [http://www.geek521.c ...
- spring 中的设计模式
https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=2247485205&idx=1&sn=63455d2313776d ...
- Spring中使用的设计模式
目录 Spring使用的设计模式 1.单例模式 2.原型模式 3.模板模式 4.观察者模式 5.工厂模式 简单工厂模式 工厂方法模式 6.适配器模式 7.装饰者模式 8.代理模式 9.策略模式 S ...
- 详解设计模式在Spring中的应用
设计模式作为工作学习中的枕边书,却时常处于勤说不用的尴尬境地,也不是我们时常忘记,只是一直没有记忆. 今天,在IT学习者网站就设计模式的内在价值做一番探讨,并以spring为例进行讲解,只有领略了其设 ...
随机推荐
- 面向对象 part2 属性的特性
6.1.1理解对象 创建自定义对象最简单的方式就是创建一个object实例.然后添加方法和实例 var person = new Object() person.name = "hi&quo ...
- 中国文化产业基金越来越多,但IP变现难题为何仍未解决?
自始至终,中国商界领域的态势就有一个很明显的特征--哪里是风口.哪里是热点,企业就会蜂拥而至并集体掘金.一直到决出胜负,或者把整个风口"做烂"才罢休.很典型的案例就是电商领域已经呈 ...
- 常用STL的常见用法
//#pragma comment(linker, "/STACK:1024000000,1024000000") //#pragma GCC optimize(2) //#inc ...
- look and say 外观数列的python实现
#look_and_say 外观数列 如果我们把 1 作为Look-and-say 数列的第一项,那么,它的前几项是这样的: 1, 11, 21, 1211, 111221, 312211, 1311 ...
- JS中的7种设计模式
第九章Refactoring to OOP Patterns 重构为OOP模式 7种设计模式: 1,模版方法模式(template method) 2,策略模式(strategy) 3,状态模式(st ...
- 使用DataSnap Server环境搭建注意的问题。
1.Data exploer 的MYSQL文件(Libmysql.dll)放到系统的system32目录即可
- 异常处理和UDP协议
一.什么是异常? 程序在运行过程中出现了不可预知的错误,并且该错误没对应的处理机制,那么就会以异常的形式表示出来, 造成的影响就是整个程序无法再正常的运行,抛出异常. 二.异常的结构: 1:异常的类型 ...
- Codeforces 1292A/1293C - NEKO's Maze Game
题目大意: 有一个2*n的图 NEKO#ΦωΦ要带领mimi们从(1,1)的点走到(2,n)的点 每次会操作一个点,从可以通过到不可以通过,不可以通过到可以通过 每操作一次要回答一次NEKO#ΦωΦ能 ...
- Android 自定义dialog类
首先定制style样式 styles.xml 加入自定义样式 <style name="CustomLoadingDialog"> <item name=&quo ...
- TreeviewEditor软件的安装和使用
TreeviewEditor是用VB6开发的一款Windows桌面程序,用户可以快速搭建树形结构,可以导出为Word文档. 支持节点的复制粘贴.节点的拖放. 下载地址:TreeviewEditor.r ...