测试类代码如下

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. [Linux] Linux的环境变量

    环境变量可以被系统,用户,shell以及其他程序来设定 登录系统后,系统读取/etc/profile 文件,设置环境变量,如果没有就跳过 检查主目录(/root)的.profile文件,推荐去这个文件 ...

  2. [angularjs] angularjs系列笔记(四)控制器

    Scope作用域 Scope作用域是应用在HTML视图和Js控制器之间的纽带 Scope是一个对象,有可用的属性和方法 根作用域 所有的应用都有一个$rootScope,它可以作用在ng-app指令包 ...

  3. 模拟获取post数据的方式

    使用下面两种方法可以获取post数据 .通过$HTTP_RAW_POST_DATA获取 $post=$GLOBALS['HTTP_RAW_POST_DATA']; 但需要修改相应的php.ini指令 ...

  4. 27.QT-QProgressBar动态实现多彩进度条(详解)

    如下图所示: 效果如下: (gif录制的动画效果不好,所以颜色有间隙) 介绍 通过qss实现,只需要一个多彩背景图,通过QImage获取颜色,然后来设置进度条,便可以实现动态多彩进度条(根据图片设定颜 ...

  5. Java_Properties

    Java_Properties类 Hashtable与HashMap区别 主要:Hashtable线程安全,同步,效率相对低下 HashMap线程不安全,异步,效率高 父类:Hashtable父类是D ...

  6. 异常:getHibernateFlushMode is not valid without active transaction; nested exception is org.hibernate.HibernateException: getHibernateFlushMode is not valid without active transaction getHibernateFlu

    场景: 在使用spring整合hibernate调用的HibernateTemplate时报错解决: 在spring配置文件中添加事务的配置 <bean id="hibernateTr ...

  7. JavaScript碎片—函数闭包(模拟面向对象)

    经过这几天的博客浏览,让我见识大涨,其中有一篇让我感触犹深,JavaScript语言本身是没有面向对象的,但是那些大神们却深深的模拟出来了面向对象,让我震撼不已.本篇博客就是在此基础上加上自己的认知, ...

  8. 继续封装个 Volley 组件

    本篇文章已授权微信公众号 dasu_Android(大苏)独家发布 前面已经封装了很多常用.基础的组件了:base-module, 包括了: crash 处理 常用工具类 apk 升级处理 log 组 ...

  9. css:Media Queries: How to target desktop, tablet and mobile?

    <!doctype html> <html> <head> <meta name="viewport" content="wid ...

  10. 获取邮箱的DNS和MX 工具类

    1.导入Maven  DNS  包: <dependency> <groupId>dnsjava</groupId> <artifactId>dnsja ...