测试类代码如下

package zcc.spring_jdbc.demo2;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List; import javax.annotation.Resource; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import domain.Student; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext5.xml")
public class SpringDemo1 { @Resource(name = "jdbcTemplate")
private JdbcTemplate jdbcTemplate; @Test
/*
* 增加
*/
public void demo1() {
jdbcTemplate.update("insert into student values(?,?,?)", 8, "周司", "男"); } @Test
/*
* 删除
*/
public void demo2() {
jdbcTemplate.update("delete from student where id = ?", 2); } @Test
/*
* 修改
*/
public void demo3() {
jdbcTemplate.update("update student set name = ? where id = ? ", "李三", 3); } @Test
/*
* 查询字符串
*/
public void demo4() {
String name = jdbcTemplate.queryForObject("select name from student where id = ? ", String.class, 1);
System.out.println(name);
} @Test
/*
* 查询总数
*/
public void demo5() {
Long total = jdbcTemplate.queryForObject("select count(*) from student ", Long.class);
System.out.println(total);
} @Test
/*
* 查询单个对象
*/
public void demo6() {
Student student = jdbcTemplate.queryForObject("select * from student where id = ? ", new myRowMapper(), 1);
System.out.println(student.toString());
} @Test
/*
* 查询多个对象
*/
public void demo7() {
List<Student> list= jdbcTemplate.query("select * from student", new myRowMapper());
for(Student student:list) {
System.out.println(student.toString());
}
} /*
*数据封装
*/
class myRowMapper implements RowMapper<Student> {
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
return student;
}
}
}

applicationContext5.xml代码如下:

<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- =============配置spring内置的连接池================== -->
<!-- Springdemo1每一次都要New,交给spring管理之后只需要new一次 -->
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
===属性注入=== <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3307/test"></property>
<property name="username" value="root"></property> <property name="password"
value="123456"></property> </bean> --> <!-- ==============配置C3P0连接池=============== -->
<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property
name="jdbcUrl" value="jdbc:mysql://localhost:3307/test"></property> <property
name="user" value="root"></property> <property name="password" value="123456"></property>
</bean> --> <!-- ===============引入属性文件============= -->
<!-- 第一种方式通过一个bean标签引入的(很少) -->
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/> </bean> -->
<!-- 第二种方式,通过context标签引入 -->
<context:property-placeholder location="classpath:jdbc.properties" /> <!-- ==========配置c3p0连接池============ -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean> <!-- ==============配置jdbc模板================ -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<!-- ==属性注入== -->
<property name="dataSource" ref="dataSource"></property>
</bean> </beans>

jdbc.properties的代码如下

Spring的jdbc模板3:完成CURD操作的更多相关文章

  1. 四、spring的JDBC模板和事务管理

    Spring的JDBC模板 Spring是JavaEE开发的一站式框架,对各种持久化技术都提供了简单的模板 ORM持久化技术 模板类 JDBC org.springframework.jdbc.cor ...

  2. Spring的jdbc模板1

    Spring是EE开发的一站式框架,有EE开发的每一层解决方案.Spring对持久层也提供了解决方案:ORM模块和jdbc模块,ORM模块在整合其他框架的时候使用 Spring提供了很多的模板用于简化 ...

  3. Java学习笔记43(Spring的jdbc模板)

    在之前的学习中,我们执行sql语句,需要频繁的开流,关流比较麻烦,为了更加的简化代码,我们使用Spring 的jdbc模板jdbcTemplate来简化我们的代码量:需要导入的包有: 我们在之前的dr ...

  4. java框架之Spring(3)-JDBC模板使用&事务管理

    下面内容使用到的 jar 包下载 JDBC模板使用 入门 1.导包,如要导入 Spring 的基本开发包.数据库驱动包.Spring 提供的 JDBC 模板包,如下: 2.测试: @Test publ ...

  5. Spring的jdbc模板2:使用开源的连接池

    上篇简要介绍了如何在spring中配置默认的连接池和jdbc模板,这篇来介绍开源的连接池配置与属性引入 C3P0连接池配置: 引入jar包 配置c3p0连接池 <?xml version=&qu ...

  6. day39-Spring 18-Spring的JDBC模板:查询的操作

    package cn.itcast.spring3.demo2; import java.sql.ResultSet; import java.sql.SQLException; import jav ...

  7. 十八 Spring的JDBC模板:引入外部属性文件

    配置外部属性文件 配置文件里引入属性文件,两种方式 第一种: 第二种: 引入属性文件的值: 测试: <?xml version="1.0" encoding="UT ...

  8. Spring的JDBC模板

    Spring对持久层技术支持 JDBC : org.springframework.jdbc.core.JdbcTemplate Hibernate3.0 : org.springframework. ...

  9. Spring之JDBC模板jdbcTemplate

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

随机推荐

  1. C# 委托 事件

    一:什么叫委托 通过反射发现,委托其实是一个类,继承自System.MulticastDelegate,但是System.MulticastDelegate这个类是特殊类,不能被继承 二:委托的声明 ...

  2. 被遗忘的C结构体打包技术

    今天看到的一篇文章,记录一下:https://github.com/ludx/The-Lost-Art-of-C-Structure-Packing 失传的C结构体打包技艺 作者:Eric S. Ra ...

  3. R语言实战(二)——数据分析基础知识

    一.R中数据结构 1.数据集 通常是由数据构成的一个矩形数组,行 表示 观测(记录.示例),列 表示 变量(字段.属性) 2.R中的数据结构 3.向量 c()可以用来创建向量 > a <- ...

  4. EF Code First 导航属性 与外键(转载)

    EF Code First 导航属性 与外键 一对多关系 项目中最常用到的就是一对多关系了.Code First对一对多关系也有着很好的支持.很多情况下我们都不需要特意的去配置,Code First就 ...

  5. [leetcode]1007. 行相等的最少多米诺旋转

    在一排多米诺骨牌中,A[i] 和 B[i] 分别代表第 i 个多米诺骨牌的上半部分和下半部分.(一个多米诺是两个从 1 到 6 的数字同列平铺形成的 —— 该平铺的每一半上都有一个数字.) 我们可以旋 ...

  6. 6.3 OrderBy 优化

    1. 创建实例 create table tblA( age int, birth TIMESTAMP not null ); insert into tblA(age,birth) values(2 ...

  7. Promise是Monad吗?

    译者按: 近年来,函数式语言的特性都被其它语言学过去了. 原文: Functional Computational Thinking — What is a monad? 译者: Fundebug 为 ...

  8. Session的原理,大型网站中Session方面应注意什么?

    一.Session和Cookie的区别Session是在服务器端保持会话数据的一种方法(通常用于pc端网站保持登录状态,手机端通常会使用token方式实现),存储在服务端. Cookie是在客户端保持 ...

  9. 【软工神话】第五篇(Beta收官)

    前言:这应该是最后一章了,故事虽然到这就结束了,但现实里还要继续下去,希望在很久的以后来回顾时,能因自己学生时代有这样的经历而欣慰. 说明:故事中的人物均是化名,故事情节经过些许加工,故事情节并没有针 ...

  10. es6 语法 (Promise)

    { // 基本定义 let ajax = function(callback) { console.log('执行'); //先输出 1 执行 setTimeout(function() { call ...