Spring初学之使用JdbcTemplate
Spring中使用JdbcTemplate、JdbcDaoSupport和NamedParameterJdbcTemplate来操作数据库,但是JdbcTemplate最常用,最易用。
jdbc.properties:
user=root
password=123
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc\:mysql\:///spring?encoding\=UFT-8
initPoolSize=5
maxPoolSize=20
ApplicationContext.xml中导入配置文件和配置dataSouce:
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="initialPoolSize" value="${initPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean>
test0410.java(属性和数据库中的字段对应):
package spring.jdbc;
public class test0410 {
private Integer uuid;
private String name;
private Integer age;
public Integer getUuid() {
return uuid;
}
public void setUuid(Integer uuid) {
this.uuid = uuid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "test0410 [uuid=" + uuid + ", name=" + name + ", age=" + age + "]";
}
}
test0410Dao.java:
package spring.jdbc; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository; @Repository
public class test0410Dao { @Autowired
private JdbcTemplate jdbcTemplate; public test0410 get(Integer id){
String sql="select id uuid,name,age from test0410 where id=?";
RowMapper<test0410> rowMapper=new BeanPropertyRowMapper<test0410>(test0410.class); test0410 test10410=jdbcTemplate.queryForObject(sql,rowMapper,1);
return test10410;
}
}
ApplicationContext.xml中配置自动扫描和JdbcTemplate:
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="spring.jdbc"></context:component-scan> <!-- 配置spring 的JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
测试类:
package spring.jdbc.test; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import javax.sql.DataSource; import org.junit.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; import spring.jdbc.test0410;
import spring.jdbc.test0410Dao; public class jdbcTest { private ApplicationContext ctx=null;
private JdbcTemplate jdbcTemplate;
{
ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
jdbcTemplate= (JdbcTemplate) ctx.getBean("jdbcTemplate");
} @Test
public void testDao(){
test0410Dao test04101=(test0410Dao) ctx.getBean("test0410Dao");
System.out.println(test04101.get(1));
} /**
* 获取单个列的值 或做统计查询
* 使用queryForObject(String sql, Class<Long> requiredType)
*/
@Test
public void testQueryForObject2(){
String sql="select count(id) from test0410";
long count=jdbcTemplate.queryForObject(sql, Long.class);
System.out.println(count);
} /**
* 查到实体类的集合
* 注意调用的不是queryForList方法
*/
@Test
public void testQueryForList(){
String sql="select id uuid,name,age from test0410 where id>?";
RowMapper<test0410> teMapper=new BeanPropertyRowMapper<test0410>(test0410.class);
List<test0410> test0410s=jdbcTemplate.query(sql, teMapper,2);
System.out.println(test0410s);
}
/**
* 从数据库中获取一条记录,实际得到一个对象
* 注意:不是调用queryForObject(String sql, Class<test0410> requiredType, Object... args)方法
* 而需要调用queryForObject(String sql, RowMapper<test0410> rowMapper, Object... args)
* 其中的rowMapper指定如何去映射结果集 的行,常用的实现类为BeanPropertyRowMapper
*
* 使用Sql中的列的别名和类的属性名映射,例如:id uuid
*
* 不支持级联属性
*/
@Test
public void testQueryForObject(){
String sql="select id uuid,name,age from test0410 where id=?";
RowMapper<test0410> rowMapper=new BeanPropertyRowMapper<test0410>(test0410.class); test0410 test10410=jdbcTemplate.queryForObject(sql,rowMapper,1);
System.out.println(test10410);
} /**
* 执行批量更新:update ,insert ,delete
* 最后一个参数是一个Object[]的List集合。
*/
@Test
public void testBatchUpdate(){
String sql="insert into test0410(id,name,age) values (?,?,?)";
List<Object[]> batchArgs=new ArrayList<Object[]>();
batchArgs.add(new Object[]{2,"aaa",23});
batchArgs.add(new Object[]{3,"bbb",24});
batchArgs.add(new Object[]{4,"ccc",25}); jdbcTemplate.batchUpdate(sql, batchArgs);
} /**
* 执行update ,insert ,delete
*/
@Test
public void testUptate(){
String sql="update test0410 set name=? where id=? ";
jdbcTemplate.update(sql,"lyj",1);
} @Test
public void TestDateSource() throws SQLException{
DataSource dataSource= (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource.getConnection());
}
}
Spring初学之使用JdbcTemplate的更多相关文章
- Spring初学之spring的事务管理xml
所有的java类都是用的上一篇文章:Spring初学之spring的事务管理 不同的是,这时xml配置事务,所以就要把java类中的那些关于spring的注解都删掉,然后在xml中配置,Applica ...
- Spring 初学笔记
Spring 初学笔记: https://blog.csdn.net/weixin_35909255/article/category/7470388
- 【原】使用Spring自带的JdbcTemplate。
使用Spring自带的JdbcTemplate,可以简化对数据库的操作,用起来十分方便.通过一下几个步骤的配置,即可以使用JdbcTemplate. (1)配置好Spring的数据源,加入mysql驱 ...
- Spring数据访问之JdbcTemplate
Spring数据访问之JdbcTemplate 使用JdbcTemplate的基本操作步骤 1.引jar包
- SSM框架之Spring(5)JdbcTemplate及spring事务控制
Spring(5)JdbcTemplate及spring事务控制 ##1.JdbcTmeplate 它是 spring 框架中提供的一个对象,是对原始 Jdbc API 对象的简单封装.spring ...
- Spring JDBC 框架使用JdbcTemplate 类的一个实例
JDBC 框架概述 在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关闭数据库连接等.但 Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQ ...
- Spring之JDBC模板jdbcTemplate
要使用Jdbctemplate 对象来完成jdbc 操作.通常情况下,有三种种方式得到JdbcTemplate 对象. 第一种方式:我们可以在自己定义的DAO 实现类中注入一个Da ...
- Spring MVC笔记 使用JdbcTemplate
Spring提供了 JdbcTemplate 来封装数据库jdbc操作细节, 包括: 数据库连接[打开/关闭] ,异常转义 ,SQL执行 ,查询结果的转换, 其中体现了 模板模式 的设计模式思想. 使 ...
- Spring(四)-- JdbcTemplate、声明式事务
1.Spring提供的一个操作数据库的技术JdbcTemplate,是对Jdbc的封装.语法风格非常接近DBUtils. JdbcTemplate可以直接操作数据库,加快效率,而且学这个JdbcT ...
随机推荐
- .net framework 4.5 在Visual studio 2015中丢失
解决办法:从另一台C:\Program Files(x86)\Reference Assemblies\Microsoft\.NetFramework 成功的环境中copy .net4.5 文件夹到错 ...
- 1282 时钟(最小表示法+hash)
1282 时钟 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N个时钟,每个时钟有M个指针,P个刻度.时钟是圆形的,P个刻度均分整 ...
- onload事件,解决不能在head写代码
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="con ...
- <block/> 并不是一个组件,它仅仅是一个包装元素,不会在页面中做任何渲染,只接受控制属性
<block/> 并不是一个组件,它仅仅是一个包装元素,不会在页面中做任何渲染,只接受控制属性
- CSS让一个元素一闪一闪的
.heart{ animation:heart 1s ease infinite; } @keyframes heart { 0% {opacity:0.1;} 100%{;} }
- Hadoop createSnapshot和deleteSnapshot命令
概述 HDFS快照是文件系统的只读时间点副本. 可以对文件系统的子树或整个文件系统进行快照. 快照的一些常见用例是数据备份,防止用户错误和灾难恢复.HDFS快照的实现是高效的: 快照创建是即时的:成本 ...
- 0201-开始使用Spring Cloud实战微服务准备工作
1.Spring Cloud是什么 基于spring boot,之上快速构建分布式系统的工具集 服务注册和发现[eureka.Consul.Zookeeper].服务负载均衡[Ribbon,feign ...
- Python2 socket TCPServer 多线程并发 超时关闭
在阿里云上测试过,可以直接使用. 用IP和端口发送数据,会返回echo:+接收到的数据 #coding=utf-8 import socket import threading,getopt,sys, ...
- css字体样式
css字体样式(Font Style) 属性 css文本样式 序号 中文说明 标记语法 1 字体样式 {font:font-style font-variant font-weight f ...
- Python 7 多线程及进程
进程与线程: 进程的概念: 1.程序的执行实例称为进程. 2.每个进程都提供执行程序所需的资源.一个进程有一个虚拟地址空间.可执行代码.对系统对象的开放句柄.一个安全上下文.一个独特的进程标识符.环境 ...