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 ...
随机推荐
- 【译】微型ORM:PetaPoco
PetaPoco是一款适用于.Net 和Mono的微小.快速.单文件的微型ORM. PetaPoco有以下特色: 微小,没有依赖项……单个的C#文件可以方便的添加到任何项目中. 工作于严格的没有装饰的 ...
- 【转】10 个很有用的 jQuery 弹出层提示插件
模态对话框为网站用户提供了快速显示信息的方法,也可以用来提示错误.警告和确认等信息,这里介绍了 10 个弹出模态对话框插件. How to Create a jQuery Confirm Dialog ...
- 从dm_exec_query_stats系统表查询耗时的SQL语句
语句示例: s2.dbid , s1.total_worker_time / s1.execution_count AS [Avg CPU Time] , ( , ( ( THEN ( LEN(CON ...
- C#基础知识总结(六)
摘要 方法的总结:其中包含方法的解释.方法参数.方法返回值,方法重载,out.ref.in的用法一.C#面向过程方面的总结 顺序结构:程序按照程序代码的顺序执行 分支结构:if-else.switch ...
- vs2010打不开vs2017的.sln文件,出现错误提示 “选择的文件是解决方案文件 但是用此应用程序的较新版本创建的,无法打开”
解决方案: 1.复制下面这段语句 Microsoft Visual Studio Solution File, Format Version 11.00 # Visual Studio 2010 2. ...
- python基础学习(十三)函数进阶
目录 1. 函数参数和返回值的作用 1.1 无参数,无返回值 1.2 无参数,有返回值 1.3 有参数,无返回值 1.4 有参数,有返回值 2. 函数的返回值进阶 例子:显示当前的湿度和温度 例子:交 ...
- 【Linux命令】top命令
一.简介 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,常用于服务端性能分析. 二.使用 1.查看进程内线程情况 top -Hp 2556(2556为进程号)找 ...
- vue-cli中安装方法
源:http://www.cnblogs.com/jn1223/p/6656956.html vue-cli中安装方法 vue-cli脚手架模板是基于node下的npm来完成安装的所以首先需要安装 ...
- 小tips:JS之浅拷贝与深拷贝
浅拷贝: function extendCopy(p) { var c = {}; for (var i in p) { c[i] = p[i]; } return c; } 深拷贝: functio ...
- 【机器学习】Google机器学习工程的43条最佳实践
https://blog.csdn.net/ChenVast/article/details/81449509 本文档旨在帮助那些掌握机器学习基础知识的人从Google机器学习的最佳实践中获益.它提供 ...