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的更多相关文章

  1. Spring初学之spring的事务管理xml

    所有的java类都是用的上一篇文章:Spring初学之spring的事务管理 不同的是,这时xml配置事务,所以就要把java类中的那些关于spring的注解都删掉,然后在xml中配置,Applica ...

  2. Spring 初学笔记

    Spring 初学笔记: https://blog.csdn.net/weixin_35909255/article/category/7470388

  3. 【原】使用Spring自带的JdbcTemplate。

    使用Spring自带的JdbcTemplate,可以简化对数据库的操作,用起来十分方便.通过一下几个步骤的配置,即可以使用JdbcTemplate. (1)配置好Spring的数据源,加入mysql驱 ...

  4. Spring数据访问之JdbcTemplate

    Spring数据访问之JdbcTemplate 使用JdbcTemplate的基本操作步骤 1.引jar包

  5. SSM框架之Spring(5)JdbcTemplate及spring事务控制

    Spring(5)JdbcTemplate及spring事务控制 ##1.JdbcTmeplate 它是 spring 框架中提供的一个对象,是对原始 Jdbc API 对象的简单封装.spring ...

  6. Spring JDBC 框架使用JdbcTemplate 类的一个实例

    JDBC 框架概述 在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关闭数据库连接等.但 Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQ ...

  7. Spring之JDBC模板jdbcTemplate

    要使用Jdbctemplate 对象来完成jdbc 操作.通常情况下,有三种种方式得到JdbcTemplate 对象.           第一种方式:我们可以在自己定义的DAO 实现类中注入一个Da ...

  8. Spring MVC笔记 使用JdbcTemplate

    Spring提供了 JdbcTemplate 来封装数据库jdbc操作细节, 包括: 数据库连接[打开/关闭] ,异常转义 ,SQL执行 ,查询结果的转换, 其中体现了 模板模式 的设计模式思想. 使 ...

  9. Spring(四)-- JdbcTemplate、声明式事务

    1.Spring提供的一个操作数据库的技术JdbcTemplate,是对Jdbc的封装.语法风格非常接近DBUtils.   JdbcTemplate可以直接操作数据库,加快效率,而且学这个JdbcT ...

随机推荐

  1. 对 pthread 做的一个简陋封装

    参考自 pthreadcc 库的 ThreadBase 类 用法:继承该类,重写 execute 方法,调用父类的 launchThread 方法启动线程 Thread.h // // Thread. ...

  2. js获取滚动条的位置

    页面具有 DTD,或者说指定了 DOCTYPE 时,使用 document.documentElement. 页面不具有 DTD,或者说没有指定了 DOCTYPE,时,使用 document.body ...

  3. 如何理解docker镜像build中的上下文

    参考:https://yeasy.gitbooks.io/docker_practice/content/image/build.html 理解上线文概念非常重要,不然可能碰到一些奇怪的问题. 构建镜 ...

  4. [转]C#静态方法与非静态方法的比较

    http://wenku.baidu.com/view/4e1704084a7302768e9939e0.html C#的类中可以包含两种方法:C#静态方法与非静态方法.那么他们的定义有什么不同呢?他 ...

  5. 阿里云 如何减少备份使用量? mysql数据库的完整备份、差异备份、增量备份

    RDS for MySQL备份.SQL审计容量相关问题_MYSQL使用_技术运维问题_云数据库 RDS 版-阿里云 https://help.aliyun.com/knowledge_detail/4 ...

  6. a标签里面包含img标签,出现a标签的高度高于img标签好几个px

    问题:a标签里面包含img标签,出现a标签的高度高于img标签好几个px 解决: a元素下有一个匿名文本,这个文本外有一个匿名行级盒子,它有的默认vertical-align是baseline的,而且 ...

  7. 数据库之MySQL(二)

    数据表基本 1.创建表 create table 表名(   列名  类型  是否可以为空,   列名  类型  是否可以为空)ENGINE=InnoDB DEFAULT CHARSET=utf8 是 ...

  8. app开发制作会难吗?app开发好学吗?

    前面我们讲到了app是什么,APP是运行在智能手机的第三方应用程序,可以满足用户的不同需求.那么app开发制作会难吗?这个与产品的复杂度有很大的关系,复杂度包括业务逻辑多不多,业务模块多不多等,对于玩 ...

  9. 007-搭建框架-开发AOP框架

    一.代码地址 https://github.com/bjlhx15/smart-framework.git 二.代码编写 2.1.定义切面注解 增加Aspect注解 package com.lhx.s ...

  10. 004-ibus输入法,快捷键,浏览器

    一.输入法 用 root 身份在终端下,运行下面命令: yum install ibus-pinyin ibus ibus-gtk ibus-qt 使用im-chooser命令,选择ibus为默认输入 ...