Spring的jdbc模板3:完成CURD操作
测试类代码如下
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操作的更多相关文章
- 四、spring的JDBC模板和事务管理
Spring的JDBC模板 Spring是JavaEE开发的一站式框架,对各种持久化技术都提供了简单的模板 ORM持久化技术 模板类 JDBC org.springframework.jdbc.cor ...
- Spring的jdbc模板1
Spring是EE开发的一站式框架,有EE开发的每一层解决方案.Spring对持久层也提供了解决方案:ORM模块和jdbc模块,ORM模块在整合其他框架的时候使用 Spring提供了很多的模板用于简化 ...
- Java学习笔记43(Spring的jdbc模板)
在之前的学习中,我们执行sql语句,需要频繁的开流,关流比较麻烦,为了更加的简化代码,我们使用Spring 的jdbc模板jdbcTemplate来简化我们的代码量:需要导入的包有: 我们在之前的dr ...
- java框架之Spring(3)-JDBC模板使用&事务管理
下面内容使用到的 jar 包下载 JDBC模板使用 入门 1.导包,如要导入 Spring 的基本开发包.数据库驱动包.Spring 提供的 JDBC 模板包,如下: 2.测试: @Test publ ...
- Spring的jdbc模板2:使用开源的连接池
上篇简要介绍了如何在spring中配置默认的连接池和jdbc模板,这篇来介绍开源的连接池配置与属性引入 C3P0连接池配置: 引入jar包 配置c3p0连接池 <?xml version=&qu ...
- day39-Spring 18-Spring的JDBC模板:查询的操作
package cn.itcast.spring3.demo2; import java.sql.ResultSet; import java.sql.SQLException; import jav ...
- 十八 Spring的JDBC模板:引入外部属性文件
配置外部属性文件 配置文件里引入属性文件,两种方式 第一种: 第二种: 引入属性文件的值: 测试: <?xml version="1.0" encoding="UT ...
- Spring的JDBC模板
Spring对持久层技术支持 JDBC : org.springframework.jdbc.core.JdbcTemplate Hibernate3.0 : org.springframework. ...
- Spring之JDBC模板jdbcTemplate
要使用Jdbctemplate 对象来完成jdbc 操作.通常情况下,有三种种方式得到JdbcTemplate 对象. 第一种方式:我们可以在自己定义的DAO 实现类中注入一个Da ...
随机推荐
- ife2018 零基础学院 day 3
ife2018 零基础学院 第三天:让简历有点色彩 什么是CSS,CSS是如何工作的! 摘自CSS如何工作 什么是CSS CSS是一种用于向用户指定文档如何呈现的语言 - 它们如何被指定样式.布局等. ...
- WebApi HTTP Put 405 Method not allowed
总结列表: 1. There is already an open DataReader associated with this Connection which must be closed fi ...
- HTML+CSS基础(2)-HTML标签的简单介绍和网页注释
标签的语法和基本规则 1.语法"<xxx></xxx>",英文的"<>"将标签括起来,如例1. <!--例1--> ...
- [转]Virtualbox主机和虚拟机之间文件夹共享及双向拷贝(Windows<->Windows, Windows<->Linux)
本文转自:https://www.jb51.net/article/97271.htm 最近学习Virtualbox的一些知识,记录下,Virtualbox下如何实现主机和虚拟机之间文件夹共享及双向拷 ...
- CentOS 7.6安装桌面
# yum -y groups install "GNOME Desktop" # startx
- Python 字典(Dictionary) 基本操作
Python字典是一种可变容器模型,可存储任意类型对象:如字符串.数字.元组等.它以键值对(key-value)的形式存在,因此相当于Hashmap在python中的实现. §1. 创建字典 字典由 ...
- Https协议报错:com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl解决方法
旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/xuri24/article/details/82220333 所用应用服务器:JBoss服务 ...
- css 如何让背景图片拉伸填充避免重复显示
如何让背景图片拉伸填充,这个问题听起来似乎很简单.但是很遗憾的告诉大家.不是我们想的那么简单. 比如一个容器(body,div,span)中设定一个背景.这个背景的长宽值在css2.1之前是不能被修改 ...
- CSS效果:CSS3实现模拟select以及其中的三角形
如图实现这样的效果: html代码如下: <div class="select-bg"> <select class="select-green&quo ...
- Ambari架构源码解析
1. Ambari介绍 Apache Ambari是一种基于Web的工具,支持Apache Hadoop集群的供应.管理和监控.Ambari已支持大多数Hadoop组件,包括HDFS.MapReduc ...