Spring框架学习(3)spring中使用jdbc
内容源自:spring中使用jdbc
spring dao层中对jdbc进行了封装,使用模板模式的设计模式,通过ioc被动注入的方式将jdbcTemplate这个模板类注入到数据对象中,进行数据库操作。
我们要在一个类中进行CRUD操作(crud主要被用在描述软件系统中数据库或者持久层的基本操作功能。),首先要将jdbcTemplate这个模板类注入到数据对象类中,然后将DataSource这个类注入到jdbcTemplate,获取数据源。 这样数据对象类就可以通过jdbcTemplate类中的方法进行数据库操作了。
注意:这里需要导如spring jdbc的两个包和数据库驱动包
容器配置如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<!--
将CityDaoImpl、JdbcTemplate配置成ioc容器中的bean.
-->
<bean id="dao" class="com.etoak.dao.CityDaoImpl">
<property name="jt" ref="jt"></property>
</bean>
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
<!--
提供datasource数据源[接口] ~ bean
spring 内置了一个dataSource实现类DriverManageDataSource
-->
</bean>
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/yitu"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
dao:
package com.etoak.dao;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.JdbcTemplate;
import com.etoak.bean.City;
/**
* 使用jdbc方式对表进行CURD操作
* @author D_xiao
*
*/
public class CityDaoImpl {
private JdbcTemplate jt;
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}
/**
* JdbcTemplate 将连接数据库执行添加操作的流程
* 封装在update()中
* 增删改都是使用update方法
*/
public boolean addCity(City city){
String sql =" insert into city values(null,?,?)";
Object[] args = {city.getPid(),city.getName()};
int result = jt.update(sql,args); //result 执行该操作影响的数据量
return result==1; //影响一条 则添加成功
}
public boolean deleteCity(Integer id){
String sql = "delete from city where id="+id;
int result = jt.update(sql,id);
return result==1;
}
public boolean updateCity(City city){
String sql = "update city set pid=? , name=? where id = ?";
Object[] args = {city.getPid(),city.getName(),city.getId()};
int result = jt.update(sql,args);
return result==1;
}
/**查询单条数据
* 在使用queryForMap()查询单条数据时,
* 必须能够保证传入sql可以并且只能查询一条数据,否则会抛异常
*/
public Map selectCityById(Integer id){
String sql ="select * from city where id="+id;
Map map = jt.queryForMap(sql); //jdbc技术并非orm工具,并不能把直接查出来的关系型数据封装到对象,只能封装到map中
//key 字段名 value 字段值
return map;
}
/**
* 查询批量数据
*/
public List selectAllCitys(){
String sql = "select * from city";
List list = jt.queryForList(sql);
return list;
}
/**
* 查询数据量
*/
public int selectCityCount(){
String sql = "select count(*) from city";
return jt.queryForInt(sql);
}
/**
* 其他查询
*/
public List selectCityByPage(int start,int end){
String sql = "select * from city limit ?,?";
Object[] args = {start,end};
return jt.queryForList(sql,args);
}
}
实体:
package com.etoak.bean;
public class City {
private Integer id;
private Integer pid;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public City(Integer id, Integer pid, String name) {
super();
this.id = id;
this.pid = pid;
this.name = name;
}
public City() {
super();
}
}
Spring框架学习(3)spring中使用jdbc的更多相关文章
- Spring框架学习03——Spring Bean 的详解
1.Bean 的配置 Spring可以看做一个大型工厂,用于生产和管理Spring容器中的Bean,Spring框架支持XML和Properties两种格式的配置文件,在实际开发中常用XML格式的配置 ...
- Spring框架学习02——Spring IOC 详解
1.Spring IOC的基本概念 IOC(Inverse of Control)反转控制的概念,就是将原本在程序中手动创建对象的控制权,交由Spring框架管理.当某个Java对象(调用者)需要调用 ...
- Spring 框架学习(1)--Spring、Spring MVC扫盲
纸上得来终觉浅,绝知此事要躬行 文章大纲 什么是spring 传统Java web应用架构 更强的Java Web应用架构--MVC框架 Spring--粘合式框架 spring的内涵 spring核 ...
- Spring框架学习一
Spring框架学习,转自http://blog.csdn.net/lishuangzhe7047/article/details/20740209 Spring框架学习(一) 1.什么是Spring ...
- Spring框架学习1
AnonymouL 兴之所至,心之所安;尽其在我,顺其自然 新随笔 管理 Spring框架学习(一) 阅读目录 一. spring概述 核心容器: Spring 上下文: Spring AOP ...
- Spring框架学习总结(上)
目录 1.Spring的概述 2.Spring的入门(IOC) 3.Spring的工厂类 4.Spring的配置 5.Spring的属性注入 6.Spring的分模块开发的配置 @ 1.Spring的 ...
- Spring框架学习笔记(8)——spring boot+mybatis plus+mysql项目环境搭建
之前写的那篇Spring框架学习笔记(5)--Spring Boot创建与使用,发现有多小细节没有提及,,正好现在又学习了mybatis plus这款框架,打算重新整理一遍,并将细节说清楚 1.通过I ...
- spring框架学习(三)junit单元测试
spring框架学习(三)junit单元测试 单元测试不是头一次听说了,但只是听说从来没有用过.一个模块怎么测试呢,是不是得专门为一单元写一个测试程序,然后将测试单元代码拿过来测试? 我是这么想的.学 ...
- Spring框架学习之IOC(二)
Spring框架学习之IOC(二) 接着上一篇的内容,下面开始IOC基于注解装配相关的内容 在 classpath 中扫描组件 <context:component-scan> 特定组件包 ...
- Spring框架学习笔记(5)——Spring Boot创建与使用
Spring Boot可以更为方便地搭建一个Web系统,之后服务器上部署也较为方便 创建Spring boot项目 1. 使用IDEA创建项目 2. 修改groupid和artifact 3. 一路n ...
随机推荐
- 升级PIP源
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple django
- hdu5823
官方题解:直接状压dp就行了,f[S]表示点集S的色数,枚举子集转移(子集是独立集).这样是3^n的. 这样就可以过了……(独立集就是点互相没有连边) 学到了一个穷举子集的简便写法 for (int ...
- SPOJ - PHRASES K - Relevant Phrases of Annihilation
K - Relevant Phrases of Annihilation 题目大意:给你 n 个串,问你最长的在每个字符串中出现两次且不重叠的子串的长度. 思路:二分长度,然后将height分块,看是 ...
- Java之IO流的关闭
1.在finally中关闭流: OutputStream out = null; try { out = new FileOutputStream(""); // ...操作流代码 ...
- "The /usr/local directory is not writable."解决方法
sudo chown -R $(whoami) /usr/local brew prune
- 日志 log4net
先引入log4net 接着配置configuration文件 <?xml version="1.0"?><configuration> <system ...
- 《TDD》-火花
1,规定对天才来说多余,对蠢才来说无效,只对中间这一部分有用(我至今没见到过天才,蠢才到是不少) 2,设计上顿悟的火花一闪而过,没有规律可循.良好的测试无法保证你在需要的时候灵感乍现,但是给人信心的良 ...
- Linux文档类型
Linux下文档类型分为8种: section 名称 说明 1 用户命令 可有任何人启动的 2 系统调用 即有内核提供的函数 3 例程 即库函数 4 设备 即/dev目录下的特殊文件 5 文件格 ...
- Codeforces Round #425 (Div. 2) Misha, Grisha and Underground(LCA)
Misha, Grisha and Underground time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- POJ 3662 Telephone Lines (分层图)
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6785 Accepted: 2498 D ...