Spring入门第二十四课
Spring对JDBC的支持
直接看代码:
db.properties
jdbc.user=root
jdbc.password=logan123
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/selective-courses-system jdbc.initPoolSize=5
jdbc.maxPoolSize=10
applicationContext.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"
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-4.3.xsd"> <!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/> <!-- 配置C3P0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property> <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean> <!-- 配置Spring的JDBCTemplate -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
package logan.study.spring.jdbc;
public class Student {
String student_id;
String student_name;
String card_id;
String student_class;
String sex;
String password;
String perovince;
String address;
String tel;
String interests;
public String getStudent_id() {
return student_id;
}
public void setStudent_id(String student_id) {
this.student_id = student_id;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
public String getCard_id() {
return card_id;
}
public void setCard_id(String card_id) {
this.card_id = card_id;
}
public String getStudent_class() {
return student_class;
}
public void setStudent_class(String student_class) {
this.student_class = student_class;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPerovince() {
return perovince;
}
public void setPerovince(String perovince) {
this.perovince = perovince;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getInterests() {
return interests;
}
public void setInterests(String interests) {
this.interests = interests;
}
@Override
public String toString() {
return "Student [student_id=" + student_id + ", student_name=" + student_name + ", card_id=" + card_id
+ ", student_class=" + student_class + ", sex=" + sex + ", password=" + password + ", perovince="
+ perovince + ", address=" + address + ", tel=" + tel + ", interests=" + interests + "]";
}
}
package logan.study.spring.jdbc; import static org.junit.Assert.*; 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; public class JDBCTest { private ApplicationContext ctx = null;
private JdbcTemplate jdbcTemplate; {
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
} @Test
public void testDataSource() throws SQLException {
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource.getConnection());
}
/**
* 获取单个列的值,或者做统计查询
*/
@Test
public void testQueryForObject2(){
String sql = "SELECT count(*) FROM student_info";
long count = jdbcTemplate.queryForObject(sql, Long.class);
System.out.println(count);
} /**
* 查到实体类的集合
*/
@Test
public void testQueryForList(){
String sql = "SELECT student_id,student_name,card_id,class student_class,sex,password,perovince,tel,interests from student_info where student_id > ?";
RowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);
List<Student> students = jdbcTemplate.query(sql, rowMapper,5);
System.out.println(students);
}
/**
* 从数据库中获取一条记录,实际得到对应的一个对象
* 注意不是调用queryForObject(String sql,Class<Student> requiredType,Object... args)方法!
* 而是调用queryForObject(String sql,RowMapper<Student> rowMapper,Object... args)方法!
* 1.其中的RowMapper指定如何去映射结果集的行,常用的实现类为BeanPropertyRowMapper
* 2.使用SQL中列的别名完成列名和类的属性名的映射。例如class student_class
* 3.不支持级联框架,JdbcTemplate到底是一个JDBC的小工具,而不是ORM框架。
*/
@Test
public void testQueryForObject(){
String sql = "SELECT student_id,student_name,card_id,class student_class,sex,password,perovince,tel,interests from student_info where student_id=?";
RowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);
Student student = jdbcTemplate.queryForObject(sql, rowMapper,1);
System.out.println(student);
}
/**
* 执行批量更新,批量INSERT,UPDATE,DELETE
*
*/
@Test
public void testBatchUpdate(){
String sql = "INSERT INTO student_info (student_id,student_name) VALUES(?,?)";
List<Object[]> pss = new ArrayList<Object[]>();
pss.add(new Object[]{"009","AA"});
pss.add(new Object[]{"010","BB"});
pss.add(new Object[]{"011","CC"});
pss.add(new Object[]{"012","DD"});
pss.add(new Object[]{"013","EE"});
pss.add(new Object[]{"014","FF"});
pss.add(new Object[]{"015","GG"});
jdbcTemplate.batchUpdate(sql, pss); }
/**
* 执行INSERT,UPDATE,DELETE
*/
@Test
public void testUpdate(){
String sql = "UPDATE student_info SET student_name = ? WHERE student_id = ?";
jdbcTemplate.update(sql,"小黑","002");
} }
不推荐使用继承JdbcDaoSupport
Spring入门第二十四课的更多相关文章
- Spring入门第二十六课
Spring中的事务管理 事务简介 事务管理是企业级应用程序开发中必不可少的技术,用来确保数据的完整性和一致性. 事务就是一系列的动作,他们被当做一个单独的工作单元,这些动作要么全部完成,要么全部不起 ...
- Spring入门第二十九课
事务的隔离级别,回滚,只读,过期 当同一个应用程序或者不同应用程序中的多个事务在同一个数据集上并发执行时,可能会出现许多意外的问题. 并发事务所导致的问题可以分为下面三种类型: -脏读 -不可重复读 ...
- Spring入门第二十八课
事务的传播行为 当事务方法被另一个事务方法调用时,必须指定事务应该如何传播,例如:方法可能继续在现有事务中运行,也可能开启一个新的事务,并在自己的事务中运行. 事务的传播行为可以由传播属性指定.Spr ...
- Spring入门第二十五课
使用具名参数 直接看代码: db.properties jdbc.user=root jdbc.password=logan123 jdbc.driverClass=com.mysql.jdbc.Dr ...
- Spring入门第二十二课
重用切面表达式 我们有的时候在切面里面有多个函数,大部分函数的切入点都是一样的,所以我们可以声明切入点表达式,来重用. package logan.study.aop.impl; public int ...
- NeHe OpenGL教程 第二十四课:扩展
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- Spring入门第二十课
返回通知,异常通知,环绕通知 看代码: package logan.study.aop.impl; public interface ArithmeticCalculator { int add(in ...
- 第二十四课:jQuery.event.remove,dispatch的源码解读
本课还是来讲解一下jQuery是如何实现它的事件系统的.这一课我们先来讲一下jQuery.event.remove的源码解读. remove方法的目的是,根据用户传参,找到事件队列,从里面把匹配的ha ...
- 潭州课堂25班:Ph201805201 django 项目 第二十四课 文章主页 多级评论数据库设计 ,后台代码完成 (课堂笔记)
加载新闻评论功能 1.分析 业务处理流程: 判断前端传的新闻id是否为空,是否为整数.是否不存在 请求方法:GET url定义:'/news/<int:news_id>' 请求参数:url ...
随机推荐
- hid_info函数分析
昨天博文<linux下无线鼠标驱动执行流程>中有一行输出信息很让我迷惑,如下所示: [ :1D57: Mouse [HID Wireless Mouse HID Wireless Mous ...
- php之定义大字符串数据时使用定界符来标识
在定义大字符串数据时,通常使用定界符来标识,这种方式能保留文本中的格式,如文本中的换行.定界符使用格式如下. <<<identifier 格式化文本 identifier 其中,符号 ...
- 单机版 RedisPoolUtil({基本操作封装工具类})【一】
<!--集成的RedisJAR--> <!--引入jedis需的jar包--> <dependency> <groupId>redis.clients& ...
- POJ 1577 Falling Leaves(二叉搜索树)
思路:当时学长讲了之后,似乎有点思路----------就是倒着建一个 二叉搜索树 代码1:超时 详见超时原因 #include<iostream> #include<cstrin ...
- python-socket2
UDP,服务端 #! /usr/bin/env python #coding=utf-8 import socket #创建socket,指定ipv4,udp类型 s = socket.socket( ...
- CodeForces - 1017F. The Neutral Zone (数学+Bitset存素数+素数筛优化)
Notice: unusual memory limit! After the war, destroyed cities in the neutral zone were restored. And ...
- 2017-2018-1 20179203《Linux内核原理与分析》第二周作业
攥写人:李鹏举 学号:20179203 ( 原创作品转载请注明出处) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/US ...
- ACM学习历程—51NOD1028 大数乘法V2(FFT)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028 题目大意就是求两个大数的乘法. 但是用普通的大数乘法,这 ...
- 杂项-Log:NLog
ylbtech-杂项-Log:NLog NLog是一个基于.NET平台编写的类库,我们可以使用NLog在应用程序中添加极为完善的跟踪调试代码. NLog是一个简单灵活的.NET日志记录类库.通过使用N ...
- CentOS配置LDAP服务器
环境:centos 5.8 安装: 1.yum安装oepnldap.openldap-servers.openldap-clients.openldap-devel [root@hao-linux ~ ...